ResourceReserveStepCmd extends StepCommand { private void createNetworkConstraints( VmEntity entity, com.vmware.photon.controller.resource.gen.Vm vm) throws InvalidVmNetworksSpecException, NetworkNotFoundException, StepNotFoundException { if (vm.getResource_constraints() != null && vm.getResource_constraints().stream().anyMatch(r -> r.getType() == ResourceConstraintType.NETWORK)) { return; } List<String> networks = getNetworks(entity); for (String network : networks) { ResourceConstraint resourceConstraint = new ResourceConstraint(); if (!this.useVirtualNetwork) { resourceConstraint.setType(ResourceConstraintType.NETWORK); for (String portGroup : networkBackend.toApiRepresentation(network).getPortGroups()) { resourceConstraint.addToValues(portGroup); } } else { resourceConstraint.setType(ResourceConstraintType.VIRTUAL_NETWORK); String logicalSwitchId = getLogicalSwitchId(network); resourceConstraint.addToValues(logicalSwitchId); } vm.addToResource_constraints(resourceConstraint); } } ResourceReserveStepCmd(TaskCommand taskCommand,
                                StepBackend stepBackend,
                                StepEntity step,
                                DiskBackend diskBackend,
                                VmBackend vmBackend,
                                NetworkBackend networkBackend,
                                FlavorBackend flavorBackend,
                                Boolean useVirtualNetwork);  static final String LOGICAL_SWITCH_ID; static final String VIRTUAL_NETWORK_ID; static final String VM_ID; }
@Test public void testCreateNetworkConstraints() throws Throwable { String networkId = "n1"; Subnet subnet = new Subnet(); subnet.setId(networkId); subnet.setPortGroups(ImmutableList.of("P1", "P2")); subnet.setState(SubnetState.READY); subnet.setName("public"); when(networkBackend.toApiRepresentation(networkId)).thenReturn(subnet); vm.setNetworks(ImmutableList.of(networkId)); PlacementTask placementTask = generateResourcePlacementList(); placementTask.resource.getPlacement_list().addToPlacements( generateResourcePlacement(ResourcePlacementType.NETWORK, networkId)); Operation placementOperation = new Operation().setBody(placementTask); when(photonControllerXenonRestClient.post(any(), placementTaskCaptor.capture(), anyLong())) .thenReturn(placementOperation); when(hostClient.reserve(any(Resource.class), eq(SUCCESSFUL_GENERATION))).thenReturn(SUCCESSFUL_RESERVE_RESPONSE); TaskEntity task = mock(TaskEntity.class); doReturn(task).when(taskCommand).getTask(); ResourceReserveStepCmd command = getVmReservationCommand(); command.setInfrastructureEntity(vm); command.execute(); List<ResourceConstraint> resourceConstraints = placementTaskCaptor.getValue().resource.getVm() .getResource_constraints(); assertThat(resourceConstraints.size(), is(1)); ResourceConstraint resourceConstraint = resourceConstraints.get(0); assertThat(resourceConstraint.getType(), is(ResourceConstraintType.NETWORK)); assertThat(resourceConstraint.getValues().size(), is(2)); assertThat(resourceConstraint.getValues().get(0), is("P1")); assertThat(resourceConstraint.getValues().get(1), is("P2")); }