8f56f7795f23ac0bbf4a0b8373af8d226cbead43
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / AaiServiceInstanceStandardQueryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23
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;
42
43 import javax.servlet.http.HttpServletRequest;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.UUID;
47
48 import static java.util.stream.Collectors.toList;
49
50
51 @RestController
52 @RequestMapping(AaiServiceInstanceStandardQueryController.AAI_STANDARD_QUERY)
53 public class AaiServiceInstanceStandardQueryController extends VidRestrictedBaseController {
54
55     public static final String AAI_STANDARD_QUERY = "aai/standardQuery";
56
57     private final ServiceInstanceStandardQuery serviceInstanceStandardQuery;
58     private final FeatureManager featureManager;
59     private final VidService sdcService;
60
61     @Autowired
62     public AaiServiceInstanceStandardQueryController(FeatureManager featureManager, ServiceInstanceStandardQuery serviceInstanceStandardQuery, VidService sdcService) {
63         this.featureManager = featureManager;
64         this.serviceInstanceStandardQuery = serviceInstanceStandardQuery;
65         this.sdcService = sdcService;
66     }
67
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();
77         }
78
79         if (!isModelOf5g(sdcModelUuid)) {
80             return new VlansByNetworksHierarchy();
81         }
82
83         final ServiceInstance serviceInstance =
84                 serviceInstanceStandardQuery.fetchServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
85
86         Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> l3NetworksWithVlansForVnfForService =  fetchVnfsForService(serviceInstance);
87         Multival<ServiceInstance, Multival<Network, Vlan>> l3NetworksWithVlansForService = fetchNetworksForService(serviceInstance);
88
89         // translate to response's format
90         return new VlansByNetworksHierarchy(
91                 l3NetworksWithVlansForService.getValues().stream().map(this::translateNetworksFormat
92                     ).collect(toList()),
93
94                 l3NetworksWithVlansForVnfForService.getValues().stream().map(vnfWithNetworks ->
95                         new VnfVlansByNetworks(vnfWithNetworks.getKey().getVnfId(),
96                                 vnfWithNetworks.getValues().stream().map(this::translateNetworksFormat
97                                 ).collect(toList())
98                         )
99                 ).collect(toList())
100         );
101     }
102
103     private Multival<ServiceInstance, Multival<Vnf, Multival<Network, Vlan>>> fetchVnfsForService(ServiceInstance serviceInstance) {
104         final Multival<ServiceInstance, Vnf> vnfsForService =
105                 serviceInstanceStandardQuery.fetchRelatedVnfs(serviceInstance);
106
107         final Multival<ServiceInstance, Multival<Vnf, Network>> vnfsWithL3NetworksForService =
108                 vnfsForService.mapEachVal(vnf -> serviceInstanceStandardQuery.fetchRelatedL3Networks("vnf", vnf));
109
110         return  vnfsWithL3NetworksForService.mapEachVal(vnfMulti->
111                         vnfMulti.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags)
112                 );
113
114     }
115
116     private Multival<ServiceInstance, Multival<Network, Vlan>> fetchNetworksForService(ServiceInstance serviceInstance) {
117         final Multival<ServiceInstance, Network> l3NetworksForService =
118                 serviceInstanceStandardQuery.fetchRelatedL3Networks("service", serviceInstance);
119
120         return l3NetworksForService.mapEachVal(serviceInstanceStandardQuery::fetchRelatedVlanTags);
121     }
122
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())
131                 ).collect(toList())
132         );
133     }
134
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);
139         }
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);
143     }
144
145     protected static class VlansByNetworksHierarchy {
146         public final Collection<NetworksToVlans> serviceNetworks;
147         public final Collection<VnfVlansByNetworks> vnfNetworks;
148
149         public VlansByNetworksHierarchy() {
150             this(Collections.emptySet(), Collections.emptySet());
151         }
152
153         public VlansByNetworksHierarchy(Collection<NetworksToVlans> serviceNetworks, Collection<VnfVlansByNetworks> vnfNetworks) {
154             this.serviceNetworks = serviceNetworks;
155             this.vnfNetworks = vnfNetworks;
156         }
157     }
158
159     protected static class VnfVlansByNetworks {
160         public final String vnfId;
161         public final Collection<NetworksToVlans> networks;
162
163         public VnfVlansByNetworks(String vnfId, Collection<NetworksToVlans> networks) {
164             this.vnfId = vnfId;
165             this.networks = networks;
166         }
167     }
168
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;
175
176         private NetworksToVlans(String networkId, String name, String nodeType, String nodeStatus, Collection<Vlan> vlans) {
177             this.networkId = networkId;
178             this.name = name;
179             this.nodeType = nodeType;
180             this.nodeStatus = nodeStatus;
181             this.vlans = vlans;
182         }
183
184         private static class Vlan {
185             public final String vlanIdInner;
186
187             private Vlan(String vlanIdInner) {
188                 this.vlanIdInner = vlanIdInner;
189             }
190         }
191
192     }
193 }