2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dg.dependencymanager.helper;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
33 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
34 import java.io.IOException;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
41 import org.apache.commons.lang3.StringUtils;
42 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
43 import org.onap.appc.dg.objects.Node;
44 import org.onap.appc.dg.objects.VnfcDependencyModel;
45 import org.onap.appc.domainmodel.Vnfc;
47 public class DependencyModelParser {
49 private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelParser.class);
51 private static final String PROPERTIES = "properties";
52 private static final String ACTIVE_ACTIVE = "Active-Active";
53 private static final String ACTIVE_PASSIVE = "Active-Passive";
54 private static final String HIGH_AVAILABLITY = "high_availablity";
55 private static final String HIGH_AVAILABILITY = "high_availability";
56 private static final String MANDATORY = "mandatory";
57 private static final String TOPOLOGY_TEMPLATE = "topology_template";
58 private static final String RELATIONSHIP = "relationship";
60 private static Map<String, String> dependencyMap;
63 Map<String, String> dependencyTypeMappingMap = new HashMap<>();
64 dependencyTypeMappingMap.put("geo-activeactive", ACTIVE_ACTIVE);
65 dependencyTypeMappingMap.put("geo-activestandby", ACTIVE_PASSIVE);
66 dependencyTypeMappingMap.put("local-activeactive", ACTIVE_ACTIVE);
67 dependencyTypeMappingMap.put("local-activestandby", ACTIVE_PASSIVE);
68 dependencyMap = Collections.unmodifiableMap(dependencyTypeMappingMap);
71 public VnfcDependencyModel generateDependencyModel(String vnfModel, String vnfType)
72 throws InvalidDependencyModelException {
73 Set<Node<Vnfc>> dependencies = new HashSet<>();
74 ObjectMapper mapper = getMapper();
76 String resilienceType;
77 String prefix = "org.onap.resource.vfc." + vnfType + ".abstract.nodes.";
79 ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
81 if (root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
82 throw new InvalidDependencyModelException(
83 "Dependency model is missing 'topology_template' or 'node_templates' elements");
86 JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
87 JsonNode nodeTemplateNode = topologyTemplateNode.get("node_templates");
88 Iterator<Map.Entry<String, JsonNode>> iterator = nodeTemplateNode.fields();
89 for (JsonNode yamlNode : nodeTemplateNode) {
90 logger.debug("Processing node: " + yamlNode);
91 String fullvnfcType = iterator.next().getValue().get("type").textValue();
92 String vnfcType = getQualifiedVnfcType(fullvnfcType);
93 String type = yamlNode.get("type").textValue();
94 type = type.substring(0, type.lastIndexOf('.') + 1);
95 if (type.concat(vnfcType).toLowerCase().startsWith(prefix.concat(vnfcType).toLowerCase())) {
97 resilienceType = resolveResilienceType(yamlNode);
98 mandatory = resolveMandatory(yamlNode);
99 String[] parentList = getDependencyArray(yamlNode, nodeTemplateNode);
100 Node<Vnfc> vnfcNode = getNode(dependencies, vnfcType);
101 if (vnfcNode != null) {
102 //This code appears to be unreachable
103 logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
104 if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
105 logger.debug("Updating resilience type, "
106 + "dependencies and mandatory attribute for VNFC type: " + vnfcType);
107 vnfcNode.getChild().setResilienceType(resilienceType);
108 tryFillNode(dependencies, parentList, vnfcNode);
109 vnfcNode.getChild().setMandatory(mandatory);
112 logger.debug("Creating dependency node for : " + vnfcType);
113 vnfcNode = new Node<>(createVnfc(mandatory, resilienceType, vnfcType));
114 tryFillNode(dependencies, parentList, vnfcNode);
115 logger.debug("Adding VNFC to dependency model : " + vnfcNode);
116 dependencies.add(vnfcNode);
120 } catch (IOException e) {
121 logger.error("Error parsing dependency model : " + vnfModel);
122 logger.error("Error message : " + e);
123 throw new InvalidDependencyModelException("Error parsing dependency model. " + e.getMessage());
125 return new VnfcDependencyModel(dependencies);
128 private void tryFillNode(Set<Node<Vnfc>> dependencies, String[] parentList, Node<Vnfc> vnfcNode) {
129 if (parentList.length > 0) {
130 fillNode(dependencies, vnfcNode, parentList);
134 private boolean resolveMandatory(JsonNode yamlNode) {
135 return !mandatoryDoesNotExist(yamlNode) && yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
138 private boolean mandatoryDoesNotExist(JsonNode yamlNode) {
139 return yamlNode.get(PROPERTIES).findValue(MANDATORY) == null ||
140 yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty();
143 private String resolveResilienceType(JsonNode yamlNode) {
144 String resilienceType;
145 // If "high_availability" is present then use the correctly spelled property name
146 if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY) != null &&
147 !yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY).asText().isEmpty()) {
148 resilienceType = dependencyMap
149 .get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY).textValue());
151 // The property name "high_availability" was misspelled in the code so leaving in the code to check for the
152 // incorrectly spelled version to avoid breaking existing configurations using the misspelled version
153 else if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null ||
154 yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
155 resilienceType = ACTIVE_ACTIVE;
157 resilienceType = dependencyMap
158 .get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
160 return resilienceType;
163 private Vnfc createVnfc(boolean mandatory, String resilienceType, String vnfcType) {
164 Vnfc vnfc = new Vnfc();
165 vnfc.setMandatory(mandatory);
166 vnfc.setResilienceType(resilienceType);
167 vnfc.setVnfcType(vnfcType);
171 private String getQualifiedVnfcType(String fullvnfcType) {
172 return fullvnfcType.substring(fullvnfcType.lastIndexOf('.') + 1, fullvnfcType.length());
175 private void fillNode(Set<Node<Vnfc>> nodes, Node<Vnfc> node, String[] parentList) {
176 for (String type : parentList) {
177 String parentType = getVnfcType(type);
178 Node<Vnfc> parentNode = getNode(nodes, parentType);
179 if (parentNode != null) {
180 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
181 node.addParent(parentNode.getChild());
183 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
184 parentNode = new Node<>(createVnfc(false, null, parentType));
185 node.addParent(parentNode.getChild());
186 logger.debug("Adding VNFC to dependency model : " + parentNode);
187 nodes.add(parentNode);
192 private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode)
193 throws InvalidDependencyModelException {
194 JsonNode requirementsNode = node.get("requirements");
195 Set<String> dependencyList = new HashSet<>();
196 if (requirementsNode != null) {
197 for (JsonNode internalNode : requirementsNode) {
198 //TODO : In this release we are supporting both relationship = tosca.capabilities.Node and relationship =tosca.relationships.DependsOn we need to remove one of them in next release post confirming with SDC team
199 if (verifyNode(internalNode)) {
200 parseDependencyModel(node, nodeTemplateNode, dependencyList, internalNode);
203 return dependencyList.toArray(new String[0]);
205 return new String[0];
209 private void parseDependencyModel(JsonNode node, JsonNode nodeTemplateNode, Set<String> dependencyList,
210 JsonNode internalNode) throws InvalidDependencyModelException {
212 if (internalNode.findValue("node") != null) {
213 String nodeName = internalNode.findValue("node").asText();
214 String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
215 dependencyList.add(getQualifiedVnfcType(fullVnfcName));
217 throw new InvalidDependencyModelException(
218 "Error parsing dependency model. " + "Dependent Node not found for " + node.get("type"));
222 private boolean verifyNode(JsonNode internalNode) {
223 return nodeNullCheck(internalNode) &&
224 "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText()) &&
225 ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()) ||
226 "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()));
229 private boolean nodeNullCheck(JsonNode internalNode) {
230 return internalNode.get("dependency") != null && internalNode.findValue("capability") != null
231 && internalNode.findValue(RELATIONSHIP) != null;
234 protected Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
235 Iterator<Node<Vnfc>> itr = nodes.iterator();
237 while (itr.hasNext()) {
239 if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
245 private String getVnfcType(String type) {
246 return type.substring(type.lastIndexOf('.') + 1, type.length());
249 protected ObjectMapper getMapper() {
250 return new ObjectMapper(new YAMLFactory());