Topology tree: extract AAITreeNodesEnricher out of AAIServiceTree
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / AAITreeNodesEnricher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2020 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.stream.Collectors.toSet;
24 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.google.common.collect.ImmutableList;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.Set;
33 import javax.inject.Inject;
34 import javax.ws.rs.core.Response;
35 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.onap.vid.aai.AaiClientInterface;
37 import org.onap.vid.asdc.parser.ServiceModelInflator;
38 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
39 import org.onap.vid.model.ServiceModel;
40 import org.onap.vid.model.aaiTree.AAITreeNode;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class AAITreeNodesEnricher {
45
46     private final AaiClientInterface aaiClient;
47
48     private final ServiceModelInflator serviceModelInflator;
49
50     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAITreeNodesEnricher.class);
51
52     @Inject
53     public AAITreeNodesEnricher(
54         AaiClientInterface aaiClient,
55         ServiceModelInflator serviceModelInflator
56     ) {
57         this.aaiClient = aaiClient;
58         this.serviceModelInflator = serviceModelInflator;
59     }
60
61     void enrichNodesWithModelCustomizationName(Collection<AAITreeNode> nodes, ServiceModel serviceModel) {
62         final Map<String, Names> customizationNameByVersionId = serviceModelInflator.toNamesByVersionId(serviceModel);
63
64         nodes.forEach(node -> {
65             final Names names = customizationNameByVersionId.get(node.getModelVersionId());
66             if (names != null) {
67                 node.setKeyInModel(names.getModelKey());
68                 node.setModelCustomizationName(names.getModelCustomizationName());
69             }
70         });
71     }
72
73     public void enrichNodesWithModelVersionAndModelName(Collection<AAITreeNode> nodes) {
74
75         Collection<String> invariantIDs = getModelInvariantIds(nodes);
76
77         Map<String, String> modelVersionByModelVersionId = new HashMap<>();
78         Map<String, String> modelNameByModelVersionId = new HashMap<>();
79
80         JsonNode models = getModels(aaiClient, invariantIDs);
81         if (models!=null) {
82             for (JsonNode model : models) {
83                 JsonNode modelVersions = model.get("model-vers").get("model-ver");
84                 for (JsonNode modelVersion : modelVersions) {
85                     final String modelVersionId = modelVersion.get("model-version-id").asText();
86                     modelVersionByModelVersionId.put(modelVersionId, modelVersion.get("model-version").asText());
87                     modelNameByModelVersionId.put(modelVersionId, modelVersion.get("model-name").asText());
88                 }
89             }
90         }
91
92         nodes.forEach(node -> {
93             node.setModelVersion(modelVersionByModelVersionId.get(node.getModelVersionId()));
94             node.setModelName(modelNameByModelVersionId.get(node.getModelVersionId()));
95         });
96
97     }
98
99     private JsonNode getModels(AaiClientInterface aaiClient, Collection<String> invariantIDs) {
100         Response response = aaiClient.getVersionByInvariantId(ImmutableList.copyOf(invariantIDs));
101         try {
102             JsonNode responseJson = JACKSON_OBJECT_MAPPER.readTree(response.readEntity(String.class));
103             return responseJson.get("model");
104         } catch (Exception e) {
105             LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to getVersionByInvariantId from A&AI", e);
106         }
107         return JACKSON_OBJECT_MAPPER.createObjectNode();
108     }
109
110     private Set<String> getModelInvariantIds(Collection<AAITreeNode> nodes) {
111         return nodes.stream()
112                 .map(AAITreeNode::getModelInvariantId)
113                 .filter(Objects::nonNull)
114                 .collect(toSet());
115     }
116
117 }