2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import static java.util.Comparator.comparing;
24 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
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;
46 public class AAIServiceTree {
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;
54 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAIServiceTree.class);
56 public static final Tree<AaiRelationship> AAI_TREE_PATHS =
57 new Tree<>(new AaiRelationship(NodeType.SERVICE_INSTANCE));
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));
70 public static List<AAIServiceTree.AaiRelationship> toAaiRelationshipList(NodeType... types) {
71 return Stream.of(types).map(AAIServiceTree.AaiRelationship::new).collect(Collectors.toList());
75 public AAIServiceTree(
76 AAITreeNodeBuilder aaiTreeNodeBuilder,
77 AAITreeNodesEnricher aaiTreeNodesEnricher,
78 AAITreeConverter aaiTreeConverter,
79 VidService sdcService,
80 ExecutorService executorService
82 this.aaiTreeNodeBuilder = aaiTreeNodeBuilder;
83 this.aaiTreeNodesEnricher = aaiTreeNodesEnricher;
84 this.aaiTreeConverter = aaiTreeConverter;
85 this.sdcService = sdcService;
86 this.executorService = executorService;
89 List<AAITreeNode> buildAAITreeForUniqueResource(String getUrl, NodeType nodeType) {
90 return buildAAITreeForUniqueResourceFromCustomQuery(getUrl, null, HttpMethod.GET, nodeType);
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);
98 public List<AAITreeNode> buildAAITree(String url, String payload, HttpMethod method, Tree<AaiRelationship> pathsToSearch, boolean enrichWithModelVersion) {
100 ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
102 List<AAITreeNode> aaiTreeNodes = fetchAAITree(url, payload, method, pathsToSearch, nodesAccumulator);
104 if (enrichWithModelVersion) {
105 aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
111 public ServiceInstance getServiceInstanceTopology(String globalCustomerId, String serviceType, String serviceInstanceId) {
113 String getURL = "business/customers/customer/" +
114 globalCustomerId + "/service-subscriptions/service-subscription/" +
115 serviceType + "/service-instances/service-instance/" + serviceInstanceId;
117 //Used later to get the nodes UUID
118 ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
120 AAITreeNode aaiTree = fetchAAITree(getURL, null, HttpMethod.GET, AAI_TREE_PATHS, nodesAccumulator).get(0);
122 //Populate nodes with model-name & model-version (from aai)
123 aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
125 final ServiceModel serviceModel = getServiceModel(aaiTree.getModelVersionId());
127 //Populate nodes with model-customization-name (from sdc model)
128 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel);
130 return aaiTreeConverter.convertTreeToUIModel(aaiTree, globalCustomerId, serviceType, getInstantiationType(serviceModel), getInstanceRole(serviceModel), getInstanceType(serviceModel));
133 private String getInstanceType(ServiceModel serviceModel){
134 if (serviceModel != null && serviceModel.getService() != null) {
135 return serviceModel.getService().getServiceType();
140 private String getInstanceRole(ServiceModel serviceModel) {
141 if (serviceModel != null && serviceModel.getService() != null) {
142 return serviceModel.getService().getServiceRole();
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);
154 private ConcurrentSkipListSet<AAITreeNode> createNodesAccumulator() {
155 return new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
158 private String getInstantiationType(ServiceModel serviceModel) {
159 if (serviceModel.getService() != null && serviceModel.getService().getInstantiationType() != null) {
160 return serviceModel.getService().getInstantiationType();
166 private ServiceModel getServiceModel(String modelVersionId) {
168 final ServiceModel serviceModel = sdcService.getService(modelVersionId);
169 if (serviceModel == null) {
170 throw new GenericUncheckedException("Model version '" + modelVersionId + "' not found");
173 } catch (AsdcCatalogException e) {
174 throw new GenericUncheckedException("Exception while loading model version '" + modelVersionId + "'", e);
178 public static class AaiRelationship {
180 public final String type;
182 public AaiRelationship(String type) {
186 public AaiRelationship(NodeType nodeType) {
187 this.type = nodeType.getType();
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);
199 public int hashCode() {
200 return Objects.hash(type);