Search in sources :

Example 6 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method querySubnetStates.

/**
 * Query subnet states stored in the local document store based on the retrieved azure subnets.
 */
private void querySubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
    if (context.subnets == null || context.subnets.isEmpty()) {
        handleSubStage(context, next);
        return;
    }
    logFine(() -> "Query Subnet States from local document store.");
    Builder qBuilder = Query.Builder.create().addKindFieldClause(SubnetState.class).addInClause(SubnetState.FIELD_NAME_ID, context.subnets.keySet());
    QueryByPages<SubnetState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), SubnetState.class, context.parentCompute.tenantLinks, null, /* endpoint */
    context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    queryLocalStates.queryDocuments(subnet -> context.subnetStates.put(subnet.id, subnet.documentSelfLink)).whenComplete(thenHandleSubStage(context, next));
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 7 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method createUpdateSubnetStates.

/**
 * Create new subnet states or updates matching subnet states with the actual state in Azure.
 */
private void createUpdateSubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
    if (context.subnets.size() == 0) {
        logFine(() -> "No network states available for update.");
        handleSubStage(context, next);
        return;
    }
    Stream<Operation> operations = context.subnets.keySet().stream().map(subnetId -> {
        SubnetStateWithParentVNetId subnetStateWithParentVNetId = context.subnets.get(subnetId);
        SubnetState subnetState = subnetStateWithParentVNetId.subnetState;
        if (!context.subnetStates.containsKey(subnetId)) {
            // set internal tags as tagLinks for subnets to be newly created.
            setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
        } else {
            // for already existing subnets, add internal tags only if missing
            if (subnetState.tagLinks == null || subnetState.tagLinks.isEmpty()) {
                setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
            } else {
                context.subnetInternalTagLinksSet.stream().filter(tagLink -> !subnetState.tagLinks.contains(tagLink)).map(tagLink -> subnetState.tagLinks.add(tagLink)).collect(Collectors.toSet());
            }
        }
        // Update networkLink with "latest" (either created or updated)
        // NetworkState.documentSelfLink
        NetworkState networkState = context.networkStates.get(subnetStateWithParentVNetId.parentVNetId);
        if (networkState != null) {
            subnetState.networkLink = networkState.documentSelfLink;
        } else {
            logWarning(() -> String.format("Network state corresponding to subnet with" + " name [%s] was not found. Network Link is left empty.", subnetState.name));
        }
        if (StringUtils.isEmpty(subnetState.endpointLink)) {
            subnetState.endpointLink = context.request.endpointLink;
        }
        subnetState.computeHostLink = context.parentCompute.documentSelfLink;
        AdapterUtils.addToEndpointLinks(subnetState, context.request.endpointLink);
        return context.subnetStates.containsKey(subnetId) ? // Update case
        Operation.createPatch(this, context.subnetStates.get(subnetId)).setBody(subnetState) : // Create case.
        Operation.createPost(getHost(), SubnetService.FACTORY_LINK).setBody(subnetState);
    });
    OperationJoin.create(operations).setCompletion((ops, failures) -> {
        if (failures != null) {
            // We don't want to fail the whole data collection if some of the
            // operation fails.
            failures.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
        }
        // Process successful operations.
        ops.values().stream().filter(operation -> failures != null && !failures.containsKey(operation.getId())).filter(operation -> operation.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
            SubnetState subnetState = operation.getBody(SubnetState.class);
            context.subnets.get(subnetState.id).subnetState = subnetState;
        });
        if (context.enumNextPageLink != null) {
            logFine(() -> "Fetch next page of Virtual Networks from Azure.");
            handleSubStage(context, NetworkEnumStages.GET_VNETS);
            return;
        }
        logFine(() -> "Finished updating network states");
        handleSubStage(context, next);
    }).sendWith(this);
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) 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 8 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method getSubnets.

/**
 * Based on the retrieved page of Virtual Network resources from azure, compose the structures
 * holding Azure Subnets data.
 */
private void getSubnets(NetworkEnumContext context, NetworkEnumStages next) {
    logFine(() -> "Enumerating Subnets from Azure.");
    context.virtualNetworks.values().forEach(virtualNetwork -> {
        if (virtualNetwork.properties.subnets != null) {
            virtualNetwork.properties.subnets.forEach(subnet -> {
                SubnetState subnetState = buildSubnetState(subnet, context.parentCompute.tenantLinks, context.request.endpointLink, virtualNetwork.location, context.parentCompute.documentSelfLink);
                SubnetStateWithParentVNetId subnetStateWithParentVNetId = new SubnetStateWithParentVNetId(virtualNetwork.id, subnetState);
                context.addSubnet(subnet, subnetStateWithParentVNetId);
            });
        }
    });
    handleSubStage(context, next);
}
Also used : SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 9 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class NetworkDeviceBackingFactoryTest method testGetDvsPortBackingInfoWithPortGroupKey.

/**
 * NSX-V test case when only port group key is present
 *
 * Exception as
 */
@Test(expected = IllegalArgumentException.class)
public void testGetDvsPortBackingInfoWithPortGroupKey() {
    this.subnet = new SubnetState();
    this.subnet.customProperties = new HashMap<>();
    List<String> portGroupIds = new ArrayList<>();
    portGroupIds.add(UUID.randomUUID().toString());
    portGroupIds.add(UUID.randomUUID().toString());
    this.subnet.customProperties.put(DvsProperties.PORT_GROUP_KEY, Utils.toJson(portGroupIds));
    // will throw NPE here as the querytargetrequest is null
    VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory.getNetworkDeviceBackingInfo(this.subnet);
    assertTrue(deviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo);
    VirtualEthernetCardDistributedVirtualPortBackingInfo distributedVirtualPortBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) deviceBackingInfo;
    DistributedVirtualSwitchPortConnection port = distributedVirtualPortBackingInfo.getPort();
    assertNotNull(port);
    // TODO: mock get DVS switch call to vSphere and match the result
    // for now we are just using the expected exception as without UUID the backing info
    // cannot be set.
    assertNull(port.getSwitchUuid());
    assertEquals(this.subnet.customProperties.get(DvsProperties.PORT_GROUP_KEY), port.getPortgroupKey());
}
Also used : VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Test(org.junit.Test)

Example 10 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class NetworkDeviceBackingFactoryTest method testUnknownNetworkTypeBackingInfo.

@Test
public void testUnknownNetworkTypeBackingInfo() {
    this.subnet = new SubnetState();
    this.subnet.customProperties = new HashMap<>();
    assertNull(NetworkDeviceBackingFactory.getNetworkDeviceBackingInfo(null));
}
Also used : SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Test(org.junit.Test)

Aggregations

SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)61 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)22 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)16 Operation (com.vmware.xenon.common.Operation)14 Query (com.vmware.xenon.services.common.QueryTask.Query)12 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)11 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)11 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)11 URI (java.net.URI)11 DeferredResult (com.vmware.xenon.common.DeferredResult)10 HashMap (java.util.HashMap)10 List (java.util.List)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 StatelessService (com.vmware.xenon.common.StatelessService)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)8 UriUtils (com.vmware.xenon.common.UriUtils)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7