Search in sources :

Example 31 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class AzureTestUtil method createDefaultNicStates.

/*
     * NOTE: It is highly recommended to keep this method in sync with its AWS counterpart:
     * TestAWSSetupUtils.createAWSNicStates
     */
public static List<NetworkInterfaceState> createDefaultNicStates(VerificationHost host, ComputeState computeHost, EndpointState endpointState, Set<String> networkRGLinks, Set<String> sgRGLinks, AzureNicSpecs azureNicSpecs, String azureVMName) throws Throwable {
    // Create network state.
    NetworkState networkState;
    {
        networkState = new NetworkState();
        networkState.id = azureNicSpecs.network.name;
        networkState.name = azureNicSpecs.network.name;
        networkState.subnetCIDR = azureNicSpecs.network.cidr;
        networkState.authCredentialsLink = endpointState.authCredentialsLink;
        networkState.endpointLink = endpointState.documentSelfLink;
        networkState.endpointLinks = new HashSet<>();
        networkState.endpointLinks.add(endpointState.documentSelfLink);
        networkState.tenantLinks = endpointState.tenantLinks;
        networkState.resourcePoolLink = computeHost.resourcePoolLink;
        networkState.groupLinks = networkRGLinks;
        networkState.regionId = azureNicSpecs.network.zoneId;
        networkState.instanceAdapterReference = UriUtils.buildUri(host, DEFAULT_INSTANCE_ADAPTER_REFERENCE);
        networkState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_vnet.name());
        networkState.computeHostLink = endpointState.computeHostLink;
        networkState = TestUtils.doPost(host, networkState, NetworkState.class, UriUtils.buildUri(host, NetworkService.FACTORY_LINK));
    }
    // Create NIC states.
    List<NetworkInterfaceState> nics = new ArrayList<>();
    for (int i = 0; i < azureNicSpecs.nicSpecs.size(); i++) {
        NicSpec nicSpec = azureNicSpecs.nicSpecs.get(i);
        // Create subnet state per NIC.
        SubnetState subnetState;
        {
            subnetState = new SubnetState();
            subnetState.id = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.name = azureNicSpecs.nicSpecs.get(i).subnetSpec.name;
            subnetState.subnetCIDR = azureNicSpecs.nicSpecs.get(i).subnetSpec.cidr;
            subnetState.zoneId = azureNicSpecs.nicSpecs.get(i).subnetSpec.zoneId;
            subnetState.networkLink = networkState.documentSelfLink;
            subnetState.endpointLink = endpointState.documentSelfLink;
            subnetState.endpointLinks = new HashSet<>();
            subnetState.endpointLinks.add(endpointState.documentSelfLink);
            subnetState.computeHostLink = endpointState.computeHostLink;
            subnetState.tenantLinks = endpointState.tenantLinks;
            subnetState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, azure_subnet.name());
            subnetState = TestUtils.doPost(host, subnetState, SubnetState.class, UriUtils.buildUri(host, SubnetService.FACTORY_LINK));
        }
        // Create security group state
        SecurityGroupState securityGroupState;
        {
            securityGroupState = new SecurityGroupState();
            securityGroupState.name = AZURE_SECURITY_GROUP_NAME + "-" + azureVMName;
            securityGroupState.authCredentialsLink = endpointState.authCredentialsLink;
            securityGroupState.endpointLink = endpointState.documentSelfLink;
            securityGroupState.endpointLinks = new HashSet<>();
            securityGroupState.endpointLinks.add(endpointState.documentSelfLink);
            securityGroupState.computeHostLink = endpointState.computeHostLink;
            securityGroupState.tenantLinks = endpointState.tenantLinks;
            securityGroupState.groupLinks = sgRGLinks;
            securityGroupState.regionId = "regionId";
            securityGroupState.resourcePoolLink = "/link/to/rp";
            securityGroupState.instanceAdapterReference = new URI("http://instanceAdapterReference");
            {
                Rule ssh = new Rule();
                ssh.name = "ssh-in";
                ssh.protocol = "tcp";
                ssh.ipRangeCidr = "0.0.0.0/0";
                ssh.ports = "22";
                securityGroupState.ingress = Collections.singletonList(ssh);
            }
            {
                Rule out = new Rule();
                out.name = "out";
                out.protocol = "tcp";
                out.ipRangeCidr = "0.0.0.0/0";
                out.ports = SecurityGroupService.ALL_PORTS;
                securityGroupState.egress = Collections.singletonList(out);
            }
            securityGroupState = TestUtils.doPost(host, securityGroupState, SecurityGroupState.class, UriUtils.buildUri(host, SecurityGroupService.FACTORY_LINK));
        }
        // Create NIC description.
        NetworkInterfaceDescription nicDescription;
        {
            nicDescription = new NetworkInterfaceDescription();
            nicDescription.id = "nicDesc" + i;
            nicDescription.name = generateName("nicDesc" + i);
            nicDescription.deviceIndex = i;
            nicDescription.assignPublicIpAddress = azureNicSpecs.assignPublicIpAddress;
            nicDescription.tenantLinks = endpointState.tenantLinks;
            nicDescription.endpointLink = endpointState.documentSelfLink;
            nicDescription.endpointLinks = new HashSet<>();
            nicDescription.endpointLinks.add(endpointState.documentSelfLink);
            nicDescription.computeHostLink = endpointState.computeHostLink;
            nicDescription.assignment = nicSpec.getIpAssignment();
            // if staticIp is null, it will be assigned automatically by DHCP.
            nicDescription.address = nicSpec.ip();
            nicDescription = TestUtils.doPost(host, nicDescription, NetworkInterfaceDescription.class, UriUtils.buildUri(host, NetworkInterfaceDescriptionService.FACTORY_LINK));
        }
        NetworkInterfaceState nicState = new NetworkInterfaceState();
        nicState.id = "nic" + i;
        nicState.name = generateName("nic" + i);
        nicState.deviceIndex = nicDescription.deviceIndex;
        nicState.networkInterfaceDescriptionLink = nicDescription.documentSelfLink;
        nicState.subnetLink = subnetState.documentSelfLink;
        nicState.networkLink = subnetState.networkLink;
        nicState.tenantLinks = endpointState.tenantLinks;
        nicState.endpointLink = endpointState.documentSelfLink;
        nicState.endpointLinks = new HashSet<>();
        nicState.endpointLinks.add(endpointState.documentSelfLink);
        nicState.computeHostLink = endpointState.computeHostLink;
        if (nicSpec.getIpAssignment() == IpAssignment.STATIC) {
            // There is a rule in:
            // \photon-model\photon-model\src\main\java\com\vmware\photon\controller\model\resources\NetworkInterfaceService.java::validateState()
            // // which will throws java.lang.IllegalArgumentException: both networkLink and IP
            // cannot be set
            nicState.networkLink = null;
        }
        if (i == 0) {
            // Attach security group only on the primary nic.
            nicState.securityGroupLinks = Collections.singletonList(securityGroupState.documentSelfLink);
        }
        nicState.tagLinks = Collections.singleton(TagsUtil.newTagState(TAG_KEY_TYPE, azure_net_interface.name(), false, endpointState.tenantLinks).documentSelfLink);
        nicState = TestUtils.doPost(host, nicState, NetworkInterfaceState.class, UriUtils.buildUri(host, NetworkInterfaceService.FACTORY_LINK));
        nics.add(nicState);
    }
    return nics;
}
Also used : NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ArrayList(java.util.ArrayList) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) URI(java.net.URI) NicSpec(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.AzureNicSpecs.NicSpec) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Rule(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState.Rule) HashSet(java.util.HashSet)

Example 32 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method addNewInterfaceState.

/**
 * Add new interface state to compute network interface links
 * @param service
 * @param enumerationProgress Enumeration progress
 * @param state Compute state
 * @param id The id of distributed virtual port or opaque network
 * @param mode Either using distributed virtual port or opaque network
 * @param docType The document class type
 * @param type The type
 * @param ipAddress The primary ip address of this interface
 */
static <T> Operation addNewInterfaceState(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeState state, String id, InterfaceStateMode mode, Class<? extends ServiceDocument> docType, Class<T> type, String ipAddress) {
    String fieldKey;
    String fieldValue;
    switch(mode) {
        case INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT:
            {
                fieldKey = CustomProperties.MOREF;
                fieldValue = TYPE_PORTGROUP + ":" + id;
                break;
            }
        case INTERFACE_STATE_WITH_OPAQUE_NETWORK:
            {
                fieldKey = NsxProperties.OPAQUE_NET_ID;
                fieldValue = id;
                break;
            }
        default:
            {
                service.logFine(() -> String.format("invalid mode when creating compute state with " + "network interface links: [%s]", mode));
                return null;
            }
    }
    Operation operation = queryByPortGroupIdOrByOpaqueNetworkId(service, enumerationProgress, fieldKey, fieldValue, docType).setCompletion((o, e) -> {
        if (e != null) {
            service.logWarning(() -> String.format("Error processing queryByPortGroupIdOrByOpaqueNetworkId for id: [%s]," + "error: [%s]", fieldValue, e.toString()));
            return;
        }
        QueryTask task = o.getBody(QueryTask.class);
        if (task.results != null && !task.results.documentLinks.isEmpty()) {
            T netState = VsphereEnumerationHelper.convertOnlyResultToDocument(task.results, type);
            NetworkInterfaceState iface = null;
            if (netState instanceof SubnetState) {
                SubnetState subnetState = (SubnetState) netState;
                iface = createNewInterfaceState(service, id, subnetState.networkLink, subnetState.documentSelfLink, ipAddress);
            } else if (netState instanceof NetworkState) {
                NetworkState networkState = (NetworkState) netState;
                iface = createNewInterfaceState(service, id, null, networkState.documentSelfLink, ipAddress);
            }
            if (iface != null) {
                state.networkInterfaceLinks.add(iface.documentSelfLink);
            }
        } else {
            service.logFine(() -> String.format("Will not add nic with id: [%s]", fieldValue));
        }
    });
    return operation;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 33 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method processFoundNetwork.

public static void processFoundNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net, MoRefKeyedMap<NetworkOverlay> allNetworks) {
    if (net.getParentSwitch() != null) {
        // portgroup: create subnet
        QueryTask task = queryForSubnet(enumerationProgress, net, net.getParentSwitch());
        withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
            if (result.documentLinks.isEmpty()) {
                createNewSubnet(service, enumerationProgress, net, allNetworks.get(net.getParentSwitch()).getDvsUuid());
            } else {
                SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
                updateSubnet(service, oldDocument, enumerationProgress, net, true);
            }
        });
    } else {
        // DVS or opaque network
        QueryTask task = queryForNetwork(enumerationProgress, net);
        withTaskResults(service, task, result -> {
            if (result.documentLinks.isEmpty()) {
                createNewNetwork(service, enumerationProgress, net);
            } else {
                NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
                updateNetwork(service, oldDocument, enumerationProgress, net);
            }
        });
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 34 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method handleNetworkChanges.

/**
 * Handles incremental changes on networks.
 *
 * @param service             the incremental service
 * @param networks            the filtered network overlays
 * @param enumerationProgress the context for enumeration.
 * @param client              the enumeration client
 */
public static void handleNetworkChanges(VSphereIncrementalEnumerationService service, MoRefKeyedMap<NetworkOverlay> networks, EnumerationProgress enumerationProgress, EnumerationClient client) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    enumerationProgress.expectNetworkCount(networks.values().size());
    for (NetworkOverlay netOverlay : networks.values()) {
        // if a network | DVS | DV port group is added.
        if (ObjectUpdateKind.ENTER == netOverlay.getObjectUpdateKind()) {
            // if DV port group is added
            if (netOverlay.getParentSwitch() != null) {
                // check if the parent switch is present as part of this enumeration
                NetworkOverlay parentSwitch = networks.get(netOverlay.getParentSwitch());
                String dvsUUID;
                if (parentSwitch == null) {
                    // retrieve the uuid from vCenter
                    dvsUUID = client.getUUIDForDVS(netOverlay);
                } else {
                    dvsUUID = parentSwitch.getDvsUuid();
                }
                createNewSubnet(service, enumerationProgress, netOverlay, dvsUUID);
            } else {
                createNewNetwork(service, enumerationProgress, netOverlay);
            }
        } else {
            // if DV port group is changed
            if (netOverlay.getId().getType().equals(VimNames.TYPE_PORTGROUP)) {
                ManagedObjectReference parentSwitch = netOverlay.getParentSwitch();
                // if parent is not retrieved, Retrieve it.
                if (null == parentSwitch && ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                    parentSwitch = client.getParentSwitchForDVPortGroup(netOverlay.getId());
                }
                QueryTask task = queryForSubnet(enumerationProgress, netOverlay, parentSwitch);
                withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
                    if (!result.documentLinks.isEmpty()) {
                        SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
                        if (ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                            updateSubnet(service, oldDocument, enumerationProgress, netOverlay, false);
                        } else {
                            // DV port group has been removed. Remove the subnet state document.
                            deleteNetwork(service, enumerationProgress, netOverlay, oldDocument);
                        }
                    } else {
                        enumerationProgress.getNetworkTracker().track();
                    }
                });
            } else {
                // DVS or Opaque network.
                QueryTask task = queryForNetwork(enumerationProgress, netOverlay);
                VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                    if (!result.documentLinks.isEmpty()) {
                        NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
                        if (ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                            updateNetwork(service, oldDocument, enumerationProgress, netOverlay);
                        } else {
                            deleteNetwork(service, enumerationProgress, netOverlay, oldDocument);
                        }
                    } else {
                        enumerationProgress.getNetworkTracker().track();
                    }
                });
            }
        }
    }
    try {
        enumerationProgress.getNetworkTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for networks!", e);
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 35 with NetworkState

use of com.vmware.photon.controller.model.resources.NetworkService.NetworkState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method makeNetworkStateFromResults.

private static NetworkState makeNetworkStateFromResults(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    ComputeStateWithDescription parent = enumerationProgress.getParent();
    NetworkState state = new NetworkState();
    state.documentSelfLink = NetworkService.FACTORY_LINK + "/" + service.getHost().nextUUID();
    state.id = state.name = net.getName();
    state.endpointLink = enumerationProgress.getRequest().endpointLink;
    AdapterUtils.addToEndpointLinks(state, enumerationProgress.getRequest().endpointLink);
    state.regionId = enumerationProgress.getRegionId();
    state.resourcePoolLink = request.resourcePoolLink;
    state.adapterManagementReference = request.adapterManagementReference;
    state.authCredentialsLink = parent.description.authCredentialsLink;
    URI ref = parent.description.instanceAdapterReference;
    state.instanceAdapterReference = AdapterUriUtil.buildAdapterUri(ref.getPort(), VSphereUriPaths.DVS_NETWORK_SERVICE);
    CustomProperties custProp = CustomProperties.of(state).put(CustomProperties.MOREF, net.getId()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.TYPE, net.getId().getType());
    if (net.getSummary() instanceof OpaqueNetworkSummary) {
        OpaqueNetworkSummary ons = (OpaqueNetworkSummary) net.getSummary();
        custProp.put(NsxProperties.OPAQUE_NET_ID, ons.getOpaqueNetworkId());
        custProp.put(NsxProperties.OPAQUE_NET_TYPE, ons.getOpaqueNetworkType());
    }
    if (net.getId().getType().equals(VimNames.TYPE_DVS)) {
        // dvs'es have a stable link
        state.documentSelfLink = buildStableDvsLink(net.getId(), request.endpointLink);
        custProp.put(DvsProperties.DVS_UUID, net.getDvsUuid());
    }
    VsphereEnumerationHelper.populateResourceStateWithAdditionalProps(state, enumerationProgress.getVcUuid());
    return state;
}
Also used : OpaqueNetworkSummary(com.vmware.vim25.OpaqueNetworkSummary) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) URI(java.net.URI)

Aggregations

NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)43 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)23 Operation (com.vmware.xenon.common.Operation)15 URI (java.net.URI)14 ArrayList (java.util.ArrayList)13 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)10 HashMap (java.util.HashMap)10 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)9 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)9 UriUtils (com.vmware.xenon.common.UriUtils)9 Query (com.vmware.xenon.services.common.QueryTask.Query)9 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)8 HashSet (java.util.HashSet)8 List (java.util.List)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7 DeferredResult (com.vmware.xenon.common.DeferredResult)7 StatelessService (com.vmware.xenon.common.StatelessService)7 Map (java.util.Map)7