Topology tree: enrich vfModules data from other versions of same model
[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.model.ServiceModel;
36 import org.onap.vid.model.aaiTree.AAITreeNode;
37 import org.onap.vid.model.aaiTree.NodeType;
38 import org.onap.vid.model.aaiTree.ServiceInstance;
39 import org.onap.vid.utils.Tree;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class AAIServiceTree {
45
46     private final AAITreeNodeBuilder aaiTreeNodeBuilder;
47     private final AAITreeNodesEnricher aaiTreeNodesEnricher;
48     private final AAITreeConverter aaiTreeConverter;
49     private final VidService sdcService;
50     private final ExecutorService executorService;
51
52     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAIServiceTree.class);
53
54     public static final Tree<AaiRelationship> AAI_TREE_PATHS =
55             new Tree<>(new AaiRelationship(NodeType.SERVICE_INSTANCE));
56
57     static {
58         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.VOLUME_GROUP));
59         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.VF_MODULE));
60         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.NETWORK, NodeType.VPN_BINDING));
61         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.NETWORK, NodeType.VPN_BINDING));
62         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.INSTANCE_GROUP, NodeType.GENERIC_VNF));
63         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.COLLECTION_RESOURCE, NodeType.INSTANCE_GROUP));
64         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.CONFIGURATION, NodeType.NETWORK, NodeType.VPN_BINDING));
65         AAI_TREE_PATHS.addPath(toAaiRelationshipList(NodeType.CONFIGURATION, NodeType.VPN_BINDING));
66     }
67
68     public static List<AAIServiceTree.AaiRelationship> toAaiRelationshipList(NodeType... types) {
69         return Stream.of(types).map(AAIServiceTree.AaiRelationship::new).collect(Collectors.toList());
70     }
71
72     @Inject
73     public AAIServiceTree(
74         AAITreeNodeBuilder aaiTreeNodeBuilder,
75         AAITreeNodesEnricher aaiTreeNodesEnricher,
76         AAITreeConverter aaiTreeConverter,
77         VidService sdcService,
78         ExecutorService executorService
79     ) {
80         this.aaiTreeNodeBuilder = aaiTreeNodeBuilder;
81         this.aaiTreeNodesEnricher = aaiTreeNodesEnricher;
82         this.aaiTreeConverter = aaiTreeConverter;
83         this.sdcService = sdcService;
84         this.executorService = executorService;
85     }
86
87     List<AAITreeNode> buildAAITreeForUniqueResource(String getUrl, NodeType nodeType) {
88         return buildAAITreeForUniqueResourceFromCustomQuery(getUrl, null, HttpMethod.GET, nodeType);
89     }
90
91     List<AAITreeNode> buildAAITreeForUniqueResourceFromCustomQuery(String url, String payload, HttpMethod method, NodeType nodeType) {
92         Tree<AAIServiceTree.AaiRelationship> pathsToSearch = new Tree<>(new AAIServiceTree.AaiRelationship(nodeType));
93         return buildAAITree(url, payload, method, pathsToSearch, false);
94     }
95
96     public List<AAITreeNode> buildAAITree(String url, String payload, HttpMethod method, Tree<AaiRelationship> pathsToSearch, boolean enrichWithModelVersion) {
97
98         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
99
100         List<AAITreeNode> aaiTreeNodes = fetchAAITree(url, payload, method, pathsToSearch, nodesAccumulator);
101
102         if (enrichWithModelVersion) {
103             aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
104         }
105
106         return aaiTreeNodes;
107     }
108
109     public ServiceInstance getServiceInstanceTopology(String globalCustomerId, String serviceType, String serviceInstanceId) {
110
111         String getURL = "business/customers/customer/" +
112                 globalCustomerId + "/service-subscriptions/service-subscription/" +
113                 serviceType + "/service-instances/service-instance/" + serviceInstanceId;
114
115         //Used later to get the nodes UUID
116         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
117
118         AAITreeNode aaiTree = fetchAAITree(getURL, null, HttpMethod.GET, AAI_TREE_PATHS, nodesAccumulator).get(0);
119
120         //Populate nodes with model-name & model-version (from aai)
121         aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
122
123         final ServiceModel serviceModel = sdcService.getServiceModelOrThrow(aaiTree.getModelVersionId());
124
125         //Populate nodes with model-customization-name (from sdc model)
126         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel);
127
128         aaiTreeNodesEnricher.enrichVfModulesWithModelCustomizationNameFromOtherVersions(nodesAccumulator, aaiTree.getModelInvariantId());
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     public static class AaiRelationship {
167
168         public final String type;
169
170         public AaiRelationship(String type) {
171             this.type = type;
172         }
173
174         public AaiRelationship(NodeType nodeType) {
175             this.type = nodeType.getType();
176         }
177
178         @Override
179         public boolean equals(Object o) {
180             if (this == o) return true;
181             if (!(o instanceof AaiRelationship)) return false;
182             AaiRelationship that = (AaiRelationship) o;
183             return Objects.equals(type, that.type);
184         }
185
186         @Override
187         public int hashCode() {
188             return Objects.hash(type);
189         }
190     }
191 }