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.openecomp.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.openecomp.appc.dg.flowbuilder.exception.InvalidDependencyModel;
35 import org.openecomp.appc.dg.objects.Node;
36 import org.openecomp.appc.dg.objects.VnfcDependencyModel;
37 import org.openecomp.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);
71 logger.debug("VNF Model after parsing: " + root);
73 if(root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
74 throw new InvalidDependencyModel("Dependency model is missing 'topology_template' or 'node_templates' elements");
77 JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
78 JsonNode nodeTemplateNode = topologyTemplateNode.get("node_templates");
79 Iterator<Map.Entry<String, JsonNode>> itretor = nodeTemplateNode.fields();
80 for (JsonNode yamlNode : nodeTemplateNode) {
81 logger.debug("Processing node: " + yamlNode);
82 String vnfcType = itretor.next().getKey();
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);
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 void addDependencies(Set<Node<Vnfc>> nodes, Node node, String[] parentList) {
130 for (String type : parentList) {
131 String parentType = getVnfcType(type);
132 Node<Vnfc> parentNode = getNode(nodes, parentType);
133 if (parentNode != null) {
134 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
135 node.addParent(parentNode.getChild());
137 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
138 parentNode = new Node<>(new Vnfc(parentType, null));
139 node.addParent(parentNode.getChild());
140 logger.debug("Adding VNFC to dependency model : " + parentNode);
141 nodes.add(parentNode);
146 private String[] getDependencyArray(JsonNode node) {
147 JsonNode requirementsNode = node.get("requirements");
148 List<String> dependencyList = new ArrayList();
149 if(requirementsNode!=null) {
150 for (JsonNode internalNode : requirementsNode) {
151 if (nodeNullCheck(internalNode) &&"tosca.capabilities.Node".equalsIgnoreCase(internalNode.get("capability").asText())
152 && "tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.get("relationship").asText())) {
153 if(internalNode.get("node") != null) {
154 dependencyList.add(internalNode.get("node").asText());
156 throw new InvalidDependencyModel("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
160 return dependencyList.toArray(new String[0]);
162 return new String[0];
166 private boolean nodeNullCheck(JsonNode internalNode) {
167 return internalNode.get("dependency") != null && internalNode.get("capability") != null && internalNode.get("relationship") != null;
170 private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
171 Iterator itr = nodes.iterator();
173 while (itr.hasNext()) {
174 node = (Node<Vnfc>) itr.next();
175 if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
182 private String getVnfcType(String type) {
183 return type.substring(type.lastIndexOf('.') + 1, type.length());