2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.dependencymanager.helper;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModel;
35 import org.onap.appc.dg.objects.Node;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.domainmodel.Vnfc;
39 import java.io.IOException;
43 public class DependencyModelParser {
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelParser.class);
46 private static Map<String, String> dependencyMap;
47 private static final String PROPERTIES = "properties";
48 private static final String ACTIVE_ACTIVE = "Active-Active";
49 private static final String ACTIVE_PASSIVE = "Active-Passive";
50 private static final String HIGH_AVAILABLITY = "high_availablity";
51 private static final String MANDATORY = "mandatory";
52 private static final String TOPOLOGY_TEMPLATE = "topology_template";
55 Map<String, String> dependencyTypeMappingMap =new HashMap<>();
56 dependencyTypeMappingMap.put("geo-activeactive", ACTIVE_ACTIVE);
57 dependencyTypeMappingMap.put("geo-activestandby", ACTIVE_PASSIVE);
58 dependencyTypeMappingMap.put("local-activeactive", ACTIVE_ACTIVE);
59 dependencyTypeMappingMap.put("local-activestandby", ACTIVE_PASSIVE);
60 dependencyMap = Collections.unmodifiableMap(dependencyTypeMappingMap);
63 public VnfcDependencyModel generateDependencyModel(String vnfModel,String vnfType) {
64 Set<Node<Vnfc>> dependencies = new HashSet<>();
65 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
67 String resilienceType;
68 String prefix = "org.openecomp.resource.vfc."+vnfType+".abstract.nodes.";
70 ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
72 if(root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
73 throw new InvalidDependencyModel("Dependency model is missing 'topology_template' or 'node_templates' elements");
76 JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
77 JsonNode nodeTemplateNode = topologyTemplateNode.get("node_templates");
78 Iterator<Map.Entry<String, JsonNode>> itretor = nodeTemplateNode.fields();
79 for (JsonNode yamlNode : nodeTemplateNode) {
80 logger.debug("Processing node: " + yamlNode);
81 String fullvnfcType = itretor.next().getValue().get("type").textValue();
82 String vnfcType= getQualifiedVnfcType(fullvnfcType);
83 String type = yamlNode.get("type").textValue();
84 type = type.substring(0,type.lastIndexOf(".")+1);
85 if(type.concat(vnfcType).toLowerCase().startsWith(prefix.concat(vnfcType).toLowerCase())) {
87 if(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
88 resilienceType = ACTIVE_ACTIVE;
90 resilienceType = dependencyMap.get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
93 if(yamlNode.get(PROPERTIES).findValue(MANDATORY) == null || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty()) {
96 mandatory = yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
98 String[] parentList = getDependencyArray(yamlNode,nodeTemplateNode);
99 Node<Vnfc> vnfcNode = getNode(dependencies, vnfcType);
100 if (vnfcNode != null) {
101 logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
102 if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
103 logger.debug("Updating resilience type, dependencies and mandatory attribute for VNFC type: " + vnfcType);
104 vnfcNode.getChild().setResilienceType(resilienceType);
105 if (parentList != null && parentList.length > 0) {
106 addDependencies(dependencies, vnfcNode, parentList);
108 vnfcNode.getChild().setMandatory(mandatory);
112 logger.debug("Creating dependency node for : " + vnfcType);
113 vnfcNode = new Node<>(new Vnfc(vnfcType, resilienceType, null, mandatory));
114 if (parentList != null && parentList.length > 0)
115 addDependencies(dependencies, vnfcNode, parentList);
116 logger.debug("Adding VNFC to dependency model : " + vnfcNode);
117 dependencies.add(vnfcNode);
121 } catch (IOException e) {
122 logger.error("Error parsing dependency model : " + vnfModel);
123 logger.error("Error message : " + e);
124 throw new InvalidDependencyModel("Error parsing dependency model. " + e.getMessage());
126 return new VnfcDependencyModel(dependencies);
129 private String getQualifiedVnfcType(String fullvnfcType) {
130 return fullvnfcType.substring(fullvnfcType.lastIndexOf(".")+1,fullvnfcType.length());
133 private void addDependencies(Set<Node<Vnfc>> nodes, Node node, String[] parentList) {
134 for (String type : parentList) {
135 String parentType = getVnfcType(type);
136 Node<Vnfc> parentNode = getNode(nodes, parentType);
137 if (parentNode != null) {
138 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
139 node.addParent(parentNode.getChild());
141 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
142 parentNode = new Node<>(new Vnfc(parentType, null));
143 node.addParent(parentNode.getChild());
144 logger.debug("Adding VNFC to dependency model : " + parentNode);
145 nodes.add(parentNode);
150 private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode) {
151 JsonNode requirementsNode = node.get("requirements");
152 List<String> dependencyList = new ArrayList();
153 if(requirementsNode!=null) {
154 for (JsonNode internalNode : requirementsNode) {
155 //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
156 if (nodeNullCheck(internalNode) &&"tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
157 && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue("relationship").asText()) || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("relationship").asText()))) {
158 if(internalNode.findValue("node") != null) {
159 String nodeName = internalNode.findValue("node").asText();
160 String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
161 dependencyList.add(getQualifiedVnfcType(fullVnfcName));
163 throw new InvalidDependencyModel("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
167 return dependencyList.toArray(new String[0]);
169 return new String[0];
173 private boolean nodeNullCheck(JsonNode internalNode) {
174 return internalNode.get("dependency") != null && internalNode.findValue("capability") != null && internalNode.findValue("relationship") != null;
177 private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
178 Iterator itr = nodes.iterator();
180 while (itr.hasNext()) {
181 node = (Node<Vnfc>) itr.next();
182 if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
189 private String getVnfcType(String type) {
190 return type.substring(type.lastIndexOf('.') + 1, type.length());