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.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;
44 public class AAIServiceTree {
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;
52 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAIServiceTree.class);
54 public static final Tree<AaiRelationship> AAI_TREE_PATHS =
55 new Tree<>(new AaiRelationship(NodeType.SERVICE_INSTANCE));
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));
68 public static List<AAIServiceTree.AaiRelationship> toAaiRelationshipList(NodeType... types) {
69 return Stream.of(types).map(AAIServiceTree.AaiRelationship::new).collect(Collectors.toList());
73 public AAIServiceTree(
74 AAITreeNodeBuilder aaiTreeNodeBuilder,
75 AAITreeNodesEnricher aaiTreeNodesEnricher,
76 AAITreeConverter aaiTreeConverter,
77 VidService sdcService,
78 ExecutorService executorService
80 this.aaiTreeNodeBuilder = aaiTreeNodeBuilder;
81 this.aaiTreeNodesEnricher = aaiTreeNodesEnricher;
82 this.aaiTreeConverter = aaiTreeConverter;
83 this.sdcService = sdcService;
84 this.executorService = executorService;
87 List<AAITreeNode> buildAAITreeForUniqueResource(String getUrl, NodeType nodeType) {
88 return buildAAITreeForUniqueResourceFromCustomQuery(getUrl, null, HttpMethod.GET, nodeType);
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);
96 public List<AAITreeNode> buildAAITree(String url, String payload, HttpMethod method, Tree<AaiRelationship> pathsToSearch, boolean enrichWithModelVersion) {
98 ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
100 List<AAITreeNode> aaiTreeNodes = fetchAAITree(url, payload, method, pathsToSearch, nodesAccumulator);
102 if (enrichWithModelVersion) {
103 aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
109 public ServiceInstance getServiceInstanceTopology(String globalCustomerId, String serviceType, String serviceInstanceId) {
111 String getURL = "business/customers/customer/" +
112 globalCustomerId + "/service-subscriptions/service-subscription/" +
113 serviceType + "/service-instances/service-instance/" + serviceInstanceId;
115 //Used later to get the nodes UUID
116 ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = createNodesAccumulator();
118 AAITreeNode aaiTree = fetchAAITree(getURL, null, HttpMethod.GET, AAI_TREE_PATHS, nodesAccumulator).get(0);
120 //Populate nodes with model-name & model-version (from aai)
121 aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator);
123 final ServiceModel serviceModel = sdcService.getServiceModelOrThrow(aaiTree.getModelVersionId());
125 //Populate nodes with model-customization-name (from sdc model)
126 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel);
128 aaiTreeNodesEnricher.enrichVfModulesWithModelCustomizationNameFromOtherVersions(nodesAccumulator, aaiTree.getModelInvariantId());
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 public static class AaiRelationship {
168 public final String type;
170 public AaiRelationship(String type) {
174 public AaiRelationship(NodeType nodeType) {
175 this.type = nodeType.getType();
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);
187 public int hashCode() {
188 return Objects.hash(type);