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.InvalidDependencyModelException;
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;
42 public class DependencyModelParser {
44 private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelParser.class);
46 private static final String PROPERTIES = "properties";
47 private static final String ACTIVE_ACTIVE = "Active-Active";
48 private static final String ACTIVE_PASSIVE = "Active-Passive";
49 private static final String HIGH_AVAILABLITY = "high_availablity";
50 private static final String MANDATORY = "mandatory";
51 private static final String TOPOLOGY_TEMPLATE = "topology_template";
52 private static final String RELATIONSHIP="relationship";
54 private static Map<String, String> dependencyMap;
56 Map<String, String> dependencyTypeMappingMap =new HashMap<>();
57 dependencyTypeMappingMap.put("geo-activeactive", ACTIVE_ACTIVE);
58 dependencyTypeMappingMap.put("geo-activestandby", ACTIVE_PASSIVE);
59 dependencyTypeMappingMap.put("local-activeactive", ACTIVE_ACTIVE);
60 dependencyTypeMappingMap.put("local-activestandby", ACTIVE_PASSIVE);
61 dependencyMap = Collections.unmodifiableMap(dependencyTypeMappingMap);
64 public VnfcDependencyModel generateDependencyModel(String vnfModel,String vnfType) throws InvalidDependencyModelException {
65 Set<Node<Vnfc>> dependencies = new HashSet<>();
66 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
68 String resilienceType;
69 String prefix = "org.onap.resource.vfc."+vnfType+".abstract.nodes.";
71 ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
73 if(root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
74 throw new InvalidDependencyModelException("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 fullvnfcType = itretor.next().getValue().get("type").textValue();
83 String vnfcType= getQualifiedVnfcType(fullvnfcType);
84 String type = yamlNode.get("type").textValue();
85 type = type.substring(0,type.lastIndexOf('.')+1);
86 if(type.concat(vnfcType).toLowerCase().startsWith(prefix.concat(vnfcType).toLowerCase())) {
88 if(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
89 resilienceType = ACTIVE_ACTIVE;
91 resilienceType = dependencyMap.get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
94 if(yamlNode.get(PROPERTIES).findValue(MANDATORY) == null || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty()) {
97 mandatory = yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
99 String[] parentList = getDependencyArray(yamlNode,nodeTemplateNode);
100 Node<Vnfc> vnfcNode = getNode(dependencies, vnfcType);
101 if (vnfcNode != null) {
102 logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
103 if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
104 logger.debug("Updating resilience type, dependencies and mandatory attribute for VNFC type: " + vnfcType);
105 vnfcNode.getChild().setResilienceType(resilienceType);
106 if (parentList != null && parentList.length > 0) {
107 addDependencies(dependencies, vnfcNode, parentList);
109 vnfcNode.getChild().setMandatory(mandatory);
113 logger.debug("Creating dependency node for : " + vnfcType);
114 vnfcNode = new Node<>(createVnfc(mandatory, resilienceType, vnfcType));
115 if (parentList != null && parentList.length > 0)
116 addDependencies(dependencies, vnfcNode, parentList);
117 logger.debug("Adding VNFC to dependency model : " + vnfcNode);
118 dependencies.add(vnfcNode);
122 } catch (IOException e) {
123 logger.error("Error parsing dependency model : " + vnfModel);
124 logger.error("Error message : " + e);
125 throw new InvalidDependencyModelException("Error parsing dependency model. " + e.getMessage());
127 return new VnfcDependencyModel(dependencies);
130 private Vnfc createVnfc(boolean mandatory, String resilienceType, String vnfcType) {
131 Vnfc vnfc = new Vnfc();
132 vnfc.setMandatory(mandatory);
133 vnfc.setResilienceType(resilienceType);
134 vnfc.setVnfcType(vnfcType);
138 private String getQualifiedVnfcType(String fullvnfcType) {
139 return fullvnfcType.substring(fullvnfcType.lastIndexOf('.')+1,fullvnfcType.length());
142 private void addDependencies(Set<Node<Vnfc>> nodes, Node node, String[] parentList) {
143 for (String type : parentList) {
144 String parentType = getVnfcType(type);
145 Node<Vnfc> parentNode = getNode(nodes, parentType);
146 if (parentNode != null) {
147 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
148 node.addParent(parentNode.getChild());
150 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
151 parentNode = new Node<>(createVnfc(false,null,parentType));
152 node.addParent(parentNode.getChild());
153 logger.debug("Adding VNFC to dependency model : " + parentNode);
154 nodes.add(parentNode);
159 private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode) throws InvalidDependencyModelException {
160 JsonNode requirementsNode = node.get("requirements");
161 Set<String> dependencyList = new HashSet<>();
162 if(requirementsNode!=null) {
163 for (JsonNode internalNode : requirementsNode) {
164 //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
165 if (nodeNullCheck(internalNode) &&"tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
166 && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()) || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()))) {
167 if(internalNode.findValue("node") != null) {
168 String nodeName = internalNode.findValue("node").asText();
169 String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
170 dependencyList.add(getQualifiedVnfcType(fullVnfcName));
172 throw new InvalidDependencyModelException("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
176 return dependencyList.toArray(new String[0]);
178 return new String[0];
182 private boolean nodeNullCheck(JsonNode internalNode) {
183 return internalNode.get("dependency") != null && internalNode.findValue("capability") != null && internalNode.findValue(RELATIONSHIP) != null;
186 private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
187 Iterator itr = nodes.iterator();
189 while (itr.hasNext()) {
190 node = (Node<Vnfc>) itr.next();
191 if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
198 private String getVnfcType(String type) {
199 return type.substring(type.lastIndexOf('.') + 1, type.length());