2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.controller;
24 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Network;
25 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.ServiceInstance;
26 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vlan;
27 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vnf;
28 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
29 import org.onap.vid.asdc.AsdcCatalogException;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.vid.model.ServiceModel;
32 import org.onap.vid.model.VidNotions;
33 import org.onap.vid.properties.Features;
34 import org.onap.vid.services.VidService;
35 import org.onap.vid.utils.Multival;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.web.bind.annotation.RestController;
41 import org.togglz.core.manager.FeatureManager;
43 import javax.servlet.http.HttpServletRequest;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.UUID;
48 import static java.util.stream.Collectors.toList;
52 @RequestMapping(AaiServiceInstanceStandardQueryController.AAI_STANDARD_QUERY)
53 public class AaiServiceInstanceStandardQueryController extends VidRestrictedBaseController {
55 public static final String AAI_STANDARD_QUERY = "aai/standardQuery";
57 private final ServiceInstanceStandardQuery serviceInstanceStandardQuery;
58 private final FeatureManager featureManager;
59 private final VidService sdcService;
62 public AaiServiceInstanceStandardQueryController(FeatureManager featureManager, ServiceInstanceStandardQuery serviceInstanceStandardQuery, VidService sdcService) {
63 this.featureManager = featureManager;
64 this.serviceInstanceStandardQuery = serviceInstanceStandardQuery;
65 this.sdcService = sdcService;
68 @RequestMapping(value = "vlansByNetworks", method = RequestMethod.GET)
69 public VlansByNetworksHierarchy getNetworksToVlansByServiceInstance(HttpServletRequest request,
70 @RequestParam("sdcModelUuid") UUID sdcModelUuid,
71 @RequestParam("globalCustomerId") String globalCustomerId,
72 @RequestParam("serviceType") String serviceType,
73 @RequestParam("serviceInstanceId") String serviceInstanceId
74 ) throws AsdcCatalogException {
75 if (!featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)) {
76 return new VlansByNetworksHierarchy();
79 if (!isModelOf5g(sdcModelUuid)) {
80 return new VlansByNetworksHierarchy();
83 final ServiceInstance serviceInstance =
84 serviceInstanceStandardQuery.fetchServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
86 Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> l3NetworksWithVlansForVnfForService = fetchVnfsForService(serviceInstance);
87 Multival<ServiceInstance, Multival<Network, Vlan>> l3NetworksWithVlansForService = fetchNetworksForService(serviceInstance);
89 // translate to response's format
90 return new VlansByNetworksHierarchy(
91 l3NetworksWithVlansForService.getValues().stream().map(this::translateNetworksFormat
94 l3NetworksWithVlansForVnfForService.getValues().stream().map(vnfWithNetworks ->
95 new VnfVlansByNetworks(vnfWithNetworks.getKey().getVnfId(),
96 vnfWithNetworks.getValues().stream().map(this::translateNetworksFormat
103 private Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> fetchVnfsForService(ServiceInstance serviceInstance) {
104 final Multival<ServiceInstance, Vnf> vnfsForService =
105 serviceInstanceStandardQuery.fetchRelatedVnfs(serviceInstance);
107 final Multival<ServiceInstance, Multival<Vnf, Network>> vnfsWithL3NetworksForService =
108 vnfsForService.mapEachVal(vnf -> serviceInstanceStandardQuery.fetchRelatedL3Networks("vnf", vnf));
110 return vnfsWithL3NetworksForService.mapEachVal(vnfMulti->
111 vnfMulti.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags)
116 private Multival<ServiceInstance, Multival<Network, Vlan>> fetchNetworksForService(ServiceInstance serviceInstance) {
117 final Multival<ServiceInstance, Network> l3NetworksForService =
118 serviceInstanceStandardQuery.fetchRelatedL3Networks("service", serviceInstance);
120 return l3NetworksForService.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags);
123 private NetworksToVlans translateNetworksFormat(Multival<Network, Vlan> networkWithVlan) {
124 return new NetworksToVlans(
125 networkWithVlan.getKey().getNetworkId(),
126 networkWithVlan.getKey().getNetworkName(),
127 networkWithVlan.getKey().getNetworkType(),
128 networkWithVlan.getKey().getOrchestrationStatus(),
129 networkWithVlan.getValues().stream().map(
130 vlan -> new NetworksToVlans.Vlan(vlan.getVlanIdInner())
135 protected boolean isModelOf5g(UUID sdcModelUuid) throws AsdcCatalogException {
136 final ServiceModel serviceModel = sdcService.getService(sdcModelUuid.toString());
137 if (serviceModel == null) {
138 throw new GenericUncheckedException("Internal error while fetching Service Model: " + sdcModelUuid);
140 VidNotions.ModelCategory serviceModelCategory = serviceModel.getService().getVidNotions().getModelCategory();
141 return serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL) ||
142 serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL);
145 protected static class VlansByNetworksHierarchy {
146 public final Collection<NetworksToVlans> serviceNetworks;
147 public final Collection<VnfVlansByNetworks> vnfNetworks;
149 public VlansByNetworksHierarchy() {
150 this(Collections.emptySet(), Collections.emptySet());
153 public VlansByNetworksHierarchy(Collection<NetworksToVlans> serviceNetworks, Collection<VnfVlansByNetworks> vnfNetworks) {
154 this.serviceNetworks = serviceNetworks;
155 this.vnfNetworks = vnfNetworks;
159 protected static class VnfVlansByNetworks {
160 public final String vnfId;
161 public final Collection<NetworksToVlans> networks;
163 public VnfVlansByNetworks(String vnfId, Collection<NetworksToVlans> networks) {
165 this.networks = networks;
169 protected static class NetworksToVlans {
170 public final String networkId;
171 public final String name;
172 public final String nodeType;
173 public final String nodeStatus;
174 public final Collection<Vlan> vlans;
176 private NetworksToVlans(String networkId, String name, String nodeType, String nodeStatus, Collection<Vlan> vlans) {
177 this.networkId = networkId;
179 this.nodeType = nodeType;
180 this.nodeStatus = nodeStatus;
184 private static class Vlan {
185 public final String vlanIdInner;
187 private Vlan(String vlanIdInner) {
188 this.vlanIdInner = vlanIdInner;