Topology tree: extract AAITreeNodesEnricher out of AAIServiceTree
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / AAIServiceTree.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.services;
22
23 import static java.util.Comparator.comparing;
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
25
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.concurrent.ConcurrentSkipListSet;
29 import java.util.concurrent.ExecutorService;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import javax.inject.Inject;
33 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.onap.vid.aai.util.AAITreeConverter;
35 import org.onap.vid.asdc.AsdcCatalogException;
36 import org.onap.vid.exceptions.GenericUncheckedException;
37 import org.onap.vid.model.ServiceModel;
38 import org.onap.vid.model.aaiTree.AAITreeNode;
39 import org.onap.vid.model.aaiTree.NodeType;
40 import org.onap.vid.model.aaiTree.ServiceInstance;
41 import org.onap.vid.utils.Tree;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class AAIServiceTree {
47
48     private final AAITreeNodeBuilder aaiTreeNodeBuilder;
49     private final AAITreeNodesEnricher aaiTreeNodesEnricher;
50     private final AAITreeConverter aaiTreeConverter;
51     private final VidService sdcService;
52     private final ExecutorService executorService;
53
54     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAIServiceTree.class);
55
56     public static final Tree<AaiRelationship> AAI_TREE_PATHS =
57             new Tree<>(new AaiRelationship(NodeType.SERVICE_INSTANCE));
58
59     static {
60         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.VOLUME_GROUP));
61         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.VF_MODULE));
62         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.NETWORK, NodeType.VPN_BINDING));
63         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.NETWORK, NodeType.VPN_BINDING));
64         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.INSTANCE_GROUP, NodeType.GENERIC_VNF));
65         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.COLLECTION_RESOURCE, NodeType.INSTANCE_GROUP));
66         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.CONFIGURATION, NodeType.NETWORK, NodeType.VPN_BINDING));
67         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.CONFIGURATION, NodeType.VPN_BINDING));
68     }
69
70     public static List<AAIServiceTree.AaiRelationship> toAaiRelationshipList(NodeType... types) {
71         return Stream.of(types).map(AAIServiceTree.AaiRelationship::new).collect(Collectors.toList());
72     }
73
74     @Inject
75     public AAIServiceTree(
76         AAITreeNodeBuilder aaiTreeNodeBuilder,
77         AAITreeNodesEnricher aaiTreeNodesEnricher,
78         AAITreeConverter aaiTreeConverter,
79         VidService sdcService,
80         ExecutorService executorService
81     ) {
82         this.aaiTreeNodeBuilder = aaiTreeNodeBuilder;
83         this.aaiTreeNodesEnricher = aaiTreeNodesEnricher;
84         this.aaiTreeConverter = aaiTreeConverter;
85         this.sdcService = sdcService;
86         this.executorService = executorService;
87     }
88
89     List<AAITreeNode> buildAAITreeForUniqueResource(String getUrl, NodeType nodeType) {
90         return buildAAITreeForUniqueResourceFromCustomQuery(getUrl, null, HttpMethod.GET, nodeType);
91     }
92
93     List<AAITreeNode> buildAAITreeForUniqueResourceFromCustomQuery(String url, String payload, HttpMethod method, NodeType nodeType) {
94         Tree<AAIServiceTree.AaiRelationship> pathsToSearch = new Tree<>(new AAIServiceTree.AaiRelationship(nodeType));
95         return buildAAITree(url, payload, method, pathsToSearch, false);
96     }
97
98     public List<AAITreeNode> buildAAITree(String url, String payload, HttpMethod method, Tree<AaiRelationship> pathsToSearch, boolean enrichWithModelVersion) {
99
100         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
101
102         List<AAITreeNode> aaiTreeNodes = fetchAAITree(url, payload, method, pathsToSearch, nodesAccumulator);
103
104         if (enrichWithModelVersion) {
105             aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
106         }
107
108         return aaiTreeNodes;
109     }
110
111     public ServiceInstance getServiceInstanceTopology(String globalCustomerId, String serviceType, String serviceInstanceId) {
112
113         String getURL = "business/customers/customer/" +
114                 globalCustomerId + "/service-subscriptions/service-subscription/" +
115                 serviceType + "/service-instances/service-instance/" + serviceInstanceId;
116
117         //Used later to get the nodes UUID
118         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
119
120         AAITreeNode aaiTree = fetchAAITree(getURL, null, HttpMethod.GET, AAI_TREE_PATHS, nodesAccumulator).get(0);
121
122         //Populate nodes with model-name & model-version (from aai)
123         aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
124
125         final ServiceModel serviceModel = getServiceModel(aaiTree.getModelVersionId());
126
127         //Populate nodes with model-customization-name (from sdc model)
128         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel);
129
130         return aaiTreeConverter.convertTreeToUIModel(aaiTree, globalCustomerId, serviceType, getInstantiationType(serviceModel), getInstanceRole(serviceModel), getInstanceType(serviceModel));
131     }
132
133     private String getInstanceType(ServiceModel serviceModel){
134         if (serviceModel != null && serviceModel.getService() != null) {
135             return serviceModel.getService().getServiceType();
136         }
137         return "";
138     }
139
140     private String getInstanceRole(ServiceModel serviceModel) {
141         if (serviceModel != null && serviceModel.getService() != null) {
142             return serviceModel.getService().getServiceRole();
143         }
144         return "";
145     }
146
147     private List<AAITreeNode> fetchAAITree(String url, String payload, HttpMethod method, Tree<AaiRelationship> pathsToSearch,
148                                            ConcurrentSkipListSet<AAITreeNode> nodesAccumulator) {
149         return aaiTreeNodeBuilder.buildNode(NodeType.fromString(pathsToSearch.getRootValue().type),
150                 url, payload, method, defaultIfNull(nodesAccumulator, createNodesAccumulator()),
151                 executorService, pathsToSearch);
152     }
153
154     private ConcurrentSkipListSet<AAITreeNode> createNodesAccumulator() {
155         return new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
156     }
157
158     private String getInstantiationType(ServiceModel serviceModel) {
159         if (serviceModel.getService() != null && serviceModel.getService().getInstantiationType() != null) {
160             return serviceModel.getService().getInstantiationType();
161         } else {
162             return null;
163         }
164     }
165
166     private ServiceModel getServiceModel(String modelVersionId) {
167         try {
168             final ServiceModel serviceModel = sdcService.getService(modelVersionId);
169             if (serviceModel == null) {
170                 throw new GenericUncheckedException("Model version '" + modelVersionId + "' not found");
171             }
172             return serviceModel;
173         } catch (AsdcCatalogException e) {
174             throw new GenericUncheckedException("Exception while loading model version '" + modelVersionId + "'", e);
175         }
176     }
177
178     public static class AaiRelationship {
179
180         public final String type;
181
182         public AaiRelationship(String type) {
183             this.type = type;
184         }
185
186         public AaiRelationship(NodeType nodeType) {
187             this.type = nodeType.getType();
188         }
189
190         @Override
191         public boolean equals(Object o) {
192             if (this == o) return true;
193             if (!(o instanceof AaiRelationship)) return false;
194             AaiRelationship that = (AaiRelationship) o;
195             return Objects.equals(type, that.type);
196         }
197
198         @Override
199         public int hashCode() {
200             return Objects.hash(type);
201         }
202     }
203 }