Unable to delete old service instances created of old ASDC service
[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 static java.util.stream.Collectors.toList;
25
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Objects;
30 import java.util.UUID;
31 import javax.servlet.http.HttpServletRequest;
32 import org.apache.commons.text.StrSubstitutor;
33 import org.onap.vid.asdc.AsdcCatalogException;
34 import org.onap.vid.exceptions.GenericUncheckedException;
35 import org.onap.vid.model.ServiceModel;
36 import org.onap.vid.model.VidNotions;
37 import org.onap.vid.model.aaiTree.AAITreeNode;
38 import org.onap.vid.model.aaiTree.NodeType;
39 import org.onap.vid.properties.Features;
40 import org.onap.vid.services.AAIServiceTree;
41 import org.onap.vid.services.VidService;
42 import org.onap.vid.utils.Tree;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.togglz.core.manager.FeatureManager;
50
51
52 @RestController
53 @RequestMapping(AaiServiceInstanceStandardQueryController.AAI_STANDARD_QUERY)
54 public class AaiServiceInstanceStandardQueryController extends VidRestrictedBaseController {
55
56     static final String AAI_STANDARD_QUERY = "aai/standardQuery";
57     private static final String SERVICE_INSTANCE_URI_TEMPLATE = "" +
58             "business/customers/customer/${global-customer-id}" +
59             "/service-subscriptions/service-subscription/${service-type}" +
60             "/service-instances/service-instance/${service-instance-id}";
61
62     private final FeatureManager featureManager;
63     private final VidService sdcService;
64     private final AAIServiceTree aaiServiceTree;
65
66     @Autowired
67     public AaiServiceInstanceStandardQueryController(FeatureManager featureManager,
68                                                      VidService sdcService, AAIServiceTree aaiServiceTree) {
69         this.featureManager = featureManager;
70         this.sdcService = sdcService;
71         this.aaiServiceTree = aaiServiceTree;
72     }
73
74     @RequestMapping(value = "vlansByNetworks", method = RequestMethod.GET)
75     public VlansByNetworksHierarchy getNetworksToVlansByServiceInstance(HttpServletRequest request,
76                                                                            @RequestParam("sdcModelUuid") UUID sdcModelUuid,
77                                                                            @RequestParam("globalCustomerId") String globalCustomerId,
78                                                                            @RequestParam("serviceType") String serviceType,
79                                                                            @RequestParam("serviceInstanceId") String serviceInstanceId
80     ) throws AsdcCatalogException {
81         if (!featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)) {
82             return new VlansByNetworksHierarchy();
83         }
84
85         if (!isModelOf5g(sdcModelUuid)) {
86             return new VlansByNetworksHierarchy();
87         }
88
89         Tree<AAIServiceTree.AaiRelationship> pathsToSearch = new Tree<>(new AAIServiceTree.AaiRelationship(NodeType.SERVICE_INSTANCE));
90         pathsToSearch.addPath(AAIServiceTree.toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.NETWORK, NodeType.VLAN_TAG));
91         pathsToSearch.addPath(AAIServiceTree.toAaiRelationshipList(NodeType.NETWORK, NodeType.VLAN_TAG));
92
93         AAITreeNode aaiTree = aaiServiceTree.buildAAITree(getServiceInstanceUri(globalCustomerId, serviceType, serviceInstanceId),
94                 null, HttpMethod.GET, pathsToSearch, false).get(0);
95
96         // translate to response's format
97         return new VlansByNetworksHierarchy(
98             aaiTree.getChildren().stream()
99                 .filter(child -> child.getType() == NodeType.NETWORK)
100                 .map(this::translateNetworksFormat)
101                 .collect(toList()),
102
103             aaiTree.getChildren().stream()
104                     .filter(child -> child.getType() == NodeType.GENERIC_VNF)
105                     .map(vnf -> new VnfVlansByNetworks(vnf.getId(),
106                                     vnf.getChildren().stream()
107                                             .map(this::translateNetworksFormat)
108                                             .collect(toList())
109                             ))
110                     .collect(toList())
111         );
112     }
113
114     private String getServiceInstanceUri(String globalCustomerId, String serviceType, String serviceInstanceId) {
115         return new StrSubstitutor(ImmutableMap.of(
116                 "global-customer-id", globalCustomerId,
117                 "service-type", serviceType,
118                 "service-instance-id", serviceInstanceId
119         )).replace(SERVICE_INSTANCE_URI_TEMPLATE);
120
121     }
122
123     private NetworksToVlans translateNetworksFormat(AAITreeNode networkWithVlan) {
124         return new NetworksToVlans(
125                 networkWithVlan.getId(),
126                 networkWithVlan.getName(),
127                 Objects.toString(networkWithVlan.getAdditionalProperties().get("network-type"), null),
128                 networkWithVlan.getOrchestrationStatus(),
129                 networkWithVlan.getChildren().stream().map(
130                         vlan -> new NetworksToVlans.Vlan(
131                                 Objects.toString(vlan.getAdditionalProperties().get("vlan-id-outer"), null)
132                         )
133                 ).collect(toList())
134         );
135     }
136
137     protected boolean isModelOf5g(UUID sdcModelUuid) throws AsdcCatalogException {
138         final ServiceModel serviceModel = sdcService.getService(sdcModelUuid.toString());
139         if (serviceModel == null) {
140             throw new GenericUncheckedException("Internal error while fetching Service Model: " + sdcModelUuid);
141         }
142         if (serviceModel.getService() == null || serviceModel.getService().getVidNotions() == null) {
143             return false;
144         }
145
146         VidNotions.ModelCategory serviceModelCategory = serviceModel.getService().getVidNotions().getModelCategory();
147         return (serviceModelCategory == VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL) ||
148                 (serviceModelCategory == VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL);
149     }
150
151     protected static class VlansByNetworksHierarchy {
152         public final Collection<NetworksToVlans> serviceNetworks;
153         public final Collection<VnfVlansByNetworks> vnfNetworks;
154
155         public VlansByNetworksHierarchy() {
156             this(Collections.emptySet(), Collections.emptySet());
157         }
158
159         public VlansByNetworksHierarchy(Collection<NetworksToVlans> serviceNetworks, Collection<VnfVlansByNetworks> vnfNetworks) {
160             this.serviceNetworks = serviceNetworks;
161             this.vnfNetworks = vnfNetworks;
162         }
163     }
164
165     protected static class VnfVlansByNetworks {
166         public final String vnfId;
167         public final Collection<NetworksToVlans> networks;
168
169         public VnfVlansByNetworks(String vnfId, Collection<NetworksToVlans> networks) {
170             this.vnfId = vnfId;
171             this.networks = networks;
172         }
173     }
174
175     protected static class NetworksToVlans {
176         public final String networkId;
177         public final String name;
178         public final String nodeType;
179         public final String nodeStatus;
180         public final Collection<Vlan> vlans;
181
182         private NetworksToVlans(String networkId, String name, String nodeType, String nodeStatus, Collection<Vlan> vlans) {
183             this.networkId = networkId;
184             this.name = name;
185             this.nodeType = nodeType;
186             this.nodeStatus = nodeStatus;
187             this.vlans = vlans;
188         }
189
190         private static class Vlan {
191             public final String vlanIdOuter;
192
193             private Vlan(String vlanIdOuter) {
194                 this.vlanIdOuter = vlanIdOuter;
195             }
196         }
197
198     }
199 }