2 * ============LICENSE_START=======================================================
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
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.stream.Collectors.toSet;
24 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.google.common.collect.ImmutableList;
28 import java.util.Collection;
29 import java.util.HashMap;
31 import java.util.Objects;
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;
44 public class AAITreeNodesEnricher {
46 private final AaiClientInterface aaiClient;
48 private final ServiceModelInflator serviceModelInflator;
50 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAITreeNodesEnricher.class);
53 public AAITreeNodesEnricher(
54 AaiClientInterface aaiClient,
55 ServiceModelInflator serviceModelInflator
57 this.aaiClient = aaiClient;
58 this.serviceModelInflator = serviceModelInflator;
61 void enrichNodesWithModelCustomizationName(Collection<AAITreeNode> nodes, ServiceModel serviceModel) {
62 final Map<String, Names> customizationNameByVersionId = serviceModelInflator.toNamesByVersionId(serviceModel);
64 nodes.forEach(node -> {
65 final Names names = customizationNameByVersionId.get(node.getModelVersionId());
67 node.setKeyInModel(names.getModelKey());
68 node.setModelCustomizationName(names.getModelCustomizationName());
73 public void enrichNodesWithModelVersionAndModelName(Collection<AAITreeNode> nodes) {
75 Collection<String> invariantIDs = getModelInvariantIds(nodes);
77 Map<String, String> modelVersionByModelVersionId = new HashMap<>();
78 Map<String, String> modelNameByModelVersionId = new HashMap<>();
80 JsonNode models = getModels(aaiClient, invariantIDs);
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());
92 nodes.forEach(node -> {
93 node.setModelVersion(modelVersionByModelVersionId.get(node.getModelVersionId()));
94 node.setModelName(modelNameByModelVersionId.get(node.getModelVersionId()));
99 private JsonNode getModels(AaiClientInterface aaiClient, Collection<String> invariantIDs) {
100 Response response = aaiClient.getVersionByInvariantId(ImmutableList.copyOf(invariantIDs));
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);
107 return JACKSON_OBJECT_MAPPER.createObjectNode();
110 private Set<String> getModelInvariantIds(Collection<AAITreeNode> nodes) {
111 return nodes.stream()
112 .map(AAITreeNode::getModelInvariantId)
113 .filter(Objects::nonNull)