Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / AaiServiceInstanceStandardQueryController.java
1 package org.onap.vid.controller;
2
3
4 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Network;
5 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.ServiceInstance;
6 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vlan;
7 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vnf;
8 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
9 import org.onap.vid.asdc.AsdcCatalogException;
10 import org.onap.vid.exceptions.GenericUncheckedException;
11 import org.onap.vid.model.ServiceModel;
12 import org.onap.vid.model.VidNotions;
13 import org.onap.vid.properties.Features;
14 import org.onap.vid.services.VidService;
15 import org.onap.vid.utils.Multival;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.RequestParam;
20 import org.springframework.web.bind.annotation.RestController;
21 import org.togglz.core.manager.FeatureManager;
22
23 import javax.servlet.http.HttpServletRequest;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.UUID;
27
28 import static java.util.stream.Collectors.toList;
29
30
31 @RestController
32 @RequestMapping(AaiServiceInstanceStandardQueryController.AAI_STANDARD_QUERY)
33 public class AaiServiceInstanceStandardQueryController extends VidRestrictedBaseController {
34
35     public static final String AAI_STANDARD_QUERY = "aai/standardQuery";
36
37     private final ServiceInstanceStandardQuery serviceInstanceStandardQuery;
38     private final FeatureManager featureManager;
39     private final VidService sdcService;
40
41     @Autowired
42     public AaiServiceInstanceStandardQueryController(FeatureManager featureManager, ServiceInstanceStandardQuery serviceInstanceStandardQuery, VidService sdcService) {
43         this.featureManager = featureManager;
44         this.serviceInstanceStandardQuery = serviceInstanceStandardQuery;
45         this.sdcService = sdcService;
46     }
47
48     @RequestMapping(value = "vlansByNetworks", method = RequestMethod.GET)
49     public VlansByNetworksHierarchy getNetworksToVlansByServiceInstance(HttpServletRequest request,
50                                                                            @RequestParam("sdcModelUuid") UUID sdcModelUuid,
51                                                                            @RequestParam("globalCustomerId") String globalCustomerId,
52                                                                            @RequestParam("serviceType") String serviceType,
53                                                                            @RequestParam("serviceInstanceId") String serviceInstanceId
54     ) throws AsdcCatalogException {
55         if (!featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)) {
56             return new VlansByNetworksHierarchy();
57         }
58
59         if (!isModelOf5g(sdcModelUuid)) {
60             return new VlansByNetworksHierarchy();
61         }
62
63         final ServiceInstance serviceInstance =
64                 serviceInstanceStandardQuery.fetchServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
65
66         Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> l3NetworksWithVlansForVnfForService =  fetchVnfsForService(serviceInstance);
67         Multival<ServiceInstance, Multival<Network, Vlan>> l3NetworksWithVlansForService = fetchNetworksForService(serviceInstance);
68
69         // translate to response's format
70         return new VlansByNetworksHierarchy(
71                 l3NetworksWithVlansForService.getValues().stream().map(this::translateNetworksFormat
72                     ).collect(toList()),
73
74                 l3NetworksWithVlansForVnfForService.getValues().stream().map(vnfWithNetworks ->
75                         new VnfVlansByNetworks(vnfWithNetworks.getKey().getVnfId(),
76                                 vnfWithNetworks.getValues().stream().map(this::translateNetworksFormat
77                                 ).collect(toList())
78                         )
79                 ).collect(toList())
80         );
81     }
82
83     private Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> fetchVnfsForService(ServiceInstance serviceInstance) {
84         final Multival<ServiceInstance, Vnf> vnfsForService =
85                 serviceInstanceStandardQuery.fetchRelatedVnfs(serviceInstance);
86
87         final Multival<ServiceInstance, Multival<Vnf, Network>> vnfsWithL3NetworksForService =
88                 vnfsForService.mapEachVal(vnf -> serviceInstanceStandardQuery.fetchRelatedL3Networks("vnf", vnf));
89
90         return  vnfsWithL3NetworksForService.mapEachVal(vnfMulti->
91                         vnfMulti.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags)
92                 );
93
94     }
95
96     private Multival<ServiceInstance, Multival<Network, Vlan>> fetchNetworksForService(ServiceInstance serviceInstance) {
97         final Multival<ServiceInstance, Network> l3NetworksForService =
98                 serviceInstanceStandardQuery.fetchRelatedL3Networks("service", serviceInstance);
99
100         return l3NetworksForService.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags);
101     }
102
103     private NetworksToVlans translateNetworksFormat(Multival<Network, Vlan> networkWithVlan) {
104         return new NetworksToVlans(
105                 networkWithVlan.getKey().getNetworkId(),
106                 networkWithVlan.getKey().getNetworkName(),
107                 networkWithVlan.getKey().getNetworkType(),
108                 networkWithVlan.getKey().getOrchestrationStatus(),
109                 networkWithVlan.getValues().stream().map(
110                         vlan -> new NetworksToVlans.Vlan(vlan.getVlanIdInner())
111                 ).collect(toList())
112         );
113     }
114
115     protected boolean isModelOf5g(UUID sdcModelUuid) throws AsdcCatalogException {
116         final ServiceModel serviceModel = sdcService.getService(sdcModelUuid.toString());
117         if (serviceModel == null) {
118             throw new GenericUncheckedException("Internal error while fetching Service Model: " + sdcModelUuid);
119         }
120         VidNotions.ModelCategory serviceModelCategory = serviceModel.getService().getVidNotions().getModelCategory();
121         return serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL) ||
122                 serviceModelCategory.equals(VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL);
123     }
124
125     protected static class VlansByNetworksHierarchy {
126         public final Collection<NetworksToVlans> serviceNetworks;
127         public final Collection<VnfVlansByNetworks> vnfNetworks;
128
129         public VlansByNetworksHierarchy() {
130             this(Collections.emptySet(), Collections.emptySet());
131         }
132
133         public VlansByNetworksHierarchy(Collection<NetworksToVlans> serviceNetworks, Collection<VnfVlansByNetworks> vnfNetworks) {
134             this.serviceNetworks = serviceNetworks;
135             this.vnfNetworks = vnfNetworks;
136         }
137     }
138
139     protected static class VnfVlansByNetworks {
140         public final String vnfId;
141         public final Collection<NetworksToVlans> networks;
142
143         public VnfVlansByNetworks(String vnfId, Collection<NetworksToVlans> networks) {
144             this.vnfId = vnfId;
145             this.networks = networks;
146         }
147     }
148
149     protected static class NetworksToVlans {
150         public final String networkId;
151         public final String name;
152         public final String nodeType;
153         public final String nodeStatus;
154         public final Collection<Vlan> vlans;
155
156         private NetworksToVlans(String networkId, String name, String nodeType, String nodeStatus, Collection<Vlan> vlans) {
157             this.networkId = networkId;
158             this.name = name;
159             this.nodeType = nodeType;
160             this.nodeStatus = nodeStatus;
161             this.vlans = vlans;
162         }
163
164         private static class Vlan {
165             public final String vlanIdInner;
166
167             private Vlan(String vlanIdInner) {
168                 this.vlanIdInner = vlanIdInner;
169             }
170         }
171
172     }
173 }