Changed to unmaintained
[appc.git] / appc-dg / appc-dg-shared / appc-dg-dependency-model / src / main / java / org / onap / appc / dg / dependencymanager / helper / DependencyModelParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.dependencymanager.helper;
26
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 java.io.IOException;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Iterator;
38 import java.util.Map;
39 import java.util.Set;
40 import org.apache.commons.lang3.StringUtils;
41 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
42 import org.onap.appc.dg.objects.Node;
43 import org.onap.appc.dg.objects.VnfcDependencyModel;
44 import org.onap.appc.domainmodel.Vnfc;
45
46 public class DependencyModelParser {
47
48     private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelParser.class);
49
50     private static final String PROPERTIES = "properties";
51     private static final String ACTIVE_ACTIVE = "Active-Active";
52     private static final String ACTIVE_PASSIVE = "Active-Passive";
53     private static final String HIGH_AVAILABLITY = "high_availablity"; // Deprecated original spelling
54     private static final String HIGH_AVAILABILITY = "high_availability";
55     private static final String MANDATORY = "mandatory";
56     private static final String TOPOLOGY_TEMPLATE = "topology_template";
57     private static final String RELATIONSHIP = "relationship";
58
59     private static Map<String, String> dependencyMap;
60
61     static {
62         Map<String, String> dependencyTypeMappingMap = new HashMap<>();
63         dependencyTypeMappingMap.put("geo-activeactive", ACTIVE_ACTIVE);
64         dependencyTypeMappingMap.put("geo-activestandby", ACTIVE_PASSIVE);
65         dependencyTypeMappingMap.put("local-activeactive", ACTIVE_ACTIVE);
66         dependencyTypeMappingMap.put("local-activestandby", ACTIVE_PASSIVE);
67         dependencyMap = Collections.unmodifiableMap(dependencyTypeMappingMap);
68     }
69
70     public VnfcDependencyModel generateDependencyModel(String vnfModel, String vnfType)
71             throws InvalidDependencyModelException {
72         Set<Node<Vnfc>> dependencies = new HashSet<>();
73         ObjectMapper mapper = getMapper();
74         boolean mandatory;
75         String resilienceType;
76         String prefix = "org.onap.resource.vfc." + vnfType + ".abstract.nodes.";
77         try {
78             ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
79
80             JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
81             JsonNode nodeTemplateNode;
82
83             if (topologyTemplateNode == null
84                     || (nodeTemplateNode = topologyTemplateNode.get("node_templates")) == null) {
85                 throw new InvalidDependencyModelException(
86                         "Dependency model is missing 'topology_template' or 'node_templates' elements");
87             }
88
89             Iterator<Map.Entry<String, JsonNode>> iterator = nodeTemplateNode.fields();
90             for (JsonNode yamlNode : nodeTemplateNode) {
91                 logger.debug("Processing node: " + yamlNode);
92                 String fullvnfcType = iterator.next().getValue().get("type").textValue();
93                 String vnfcType = getQualifiedVnfcType(fullvnfcType);
94                 String type = yamlNode.get("type").textValue();
95                 type = type.substring(0, type.lastIndexOf('.') + 1);
96                 if (type.toLowerCase().startsWith(prefix.toLowerCase())) {
97
98                     resilienceType = resolveResilienceType(yamlNode);
99                     mandatory = resolveMandatory(yamlNode);
100                     String[] parentList = getDependencyArray(yamlNode, nodeTemplateNode);
101                     Node<Vnfc> vnfcNode = getNode(dependencies, vnfcType);
102                     if (vnfcNode != null) {
103                         //This code appears to be unreachable
104                         logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
105                         if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
106                             logger.debug("Updating resilience type, "
107                                     + "dependencies and mandatory attribute for VNFC type: " + vnfcType);
108                             vnfcNode.getChild().setResilienceType(resilienceType);
109                             tryFillNode(dependencies, parentList, vnfcNode);
110                             vnfcNode.getChild().setMandatory(mandatory);
111                         }
112                     } else {
113                         logger.debug("Creating dependency node for: " + vnfcType);
114                         vnfcNode = new Node<>(createVnfc(mandatory, resilienceType, vnfcType));
115                         tryFillNode(dependencies, parentList, vnfcNode);
116                         logger.debug("Adding VNFC to dependency model: " + vnfcNode);
117                         dependencies.add(vnfcNode);
118                     }
119                 }
120             }
121         } catch (IOException e) {
122             logger.error("Error parsing dependency model: " + vnfModel);
123             logger.error("Error message: " + e);
124             throw new InvalidDependencyModelException("Error parsing dependency model. " + e.getMessage());
125         }
126         return new VnfcDependencyModel(dependencies);
127     }
128
129     private void tryFillNode(Set<Node<Vnfc>> dependencies, String[] parentList, Node<Vnfc> vnfcNode) {
130         if (parentList.length > 0) {
131             fillNode(dependencies, vnfcNode, parentList);
132         }
133     }
134
135     private boolean resolveMandatory(JsonNode yamlNode) {
136         return !mandatoryDoesNotExist(yamlNode) && yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
137     }
138
139     private boolean mandatoryDoesNotExist(JsonNode yamlNode) {
140         return yamlNode.get(PROPERTIES).findValue(MANDATORY) == null
141                 || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty();
142     }
143
144     private String resolveResilienceType(JsonNode yamlNode) {
145         String resilienceType;
146         // If "high_availability" is present then use the correctly spelled property name
147         if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY) != null &&
148                 !yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY).asText().isEmpty()) {
149             resilienceType = dependencyMap
150                     .get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABILITY).textValue());
151         }
152         // The property name "high_availability" was misspelled in the code so leaving in the code to check for the
153         // incorrectly spelled version to avoid breaking existing configurations using the misspelled version
154         else if (yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null
155                 || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
156             resilienceType = ACTIVE_ACTIVE;
157         } else {
158             resilienceType = dependencyMap
159                     .get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
160         }
161         return resilienceType;
162     }
163
164     private Vnfc createVnfc(boolean mandatory, String resilienceType, String vnfcType) {
165         Vnfc vnfc = new Vnfc();
166         vnfc.setMandatory(mandatory);
167         vnfc.setResilienceType(resilienceType);
168         vnfc.setVnfcType(vnfcType);
169         return vnfc;
170     }
171
172     private String getQualifiedVnfcType(String fullvnfcType) {
173         return fullvnfcType.substring(fullvnfcType.lastIndexOf('.') + 1, fullvnfcType.length());
174     }
175
176     private void fillNode(Set<Node<Vnfc>> nodes, Node<Vnfc> node, String[] parentList) {
177         for (String type : parentList) {
178             String parentType = getVnfcType(type);
179             Node<Vnfc> parentNode = getNode(nodes, parentType);
180             if (parentNode != null) {
181                 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list");
182                 node.addParent(parentNode.getChild());
183             } else {
184                 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC");
185                 parentNode = new Node<>(createVnfc(false, null, parentType));
186                 node.addParent(parentNode.getChild());
187                 logger.debug("Adding VNFC to dependency model: " + parentNode);
188                 nodes.add(parentNode);
189             }
190         }
191     }
192
193     private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode)
194             throws InvalidDependencyModelException {
195         JsonNode requirementsNode = node.get("requirements");
196         Set<String> dependencyList = new HashSet<>();
197         if (requirementsNode != null) {
198             for (JsonNode internalNode : requirementsNode) {
199                 /* TODO: In this release we are supporting both relationship = tosca.capabilities.Node
200                    and relationship = tosca.relationships.DependsOn
201                    we need to remove one of them in next release post confirming with SDC team */
202                 if (verifyNode(internalNode)) {
203                     parseDependencyModel(node, nodeTemplateNode, dependencyList, internalNode);
204                 }
205             }
206             return dependencyList.toArray(new String[0]);
207         } else {
208             return new String[0];
209         }
210     }
211
212     private void parseDependencyModel(JsonNode node, JsonNode nodeTemplateNode, Set<String> dependencyList,
213             JsonNode internalNode) throws InvalidDependencyModelException {
214
215         if (internalNode.findValue("node") != null) {
216             String nodeName = internalNode.findValue("node").asText();
217             String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
218             dependencyList.add(getQualifiedVnfcType(fullVnfcName));
219         } else {
220             throw new InvalidDependencyModelException(
221                     "Error parsing dependency model. Dependent Node not found for " + node.get("type"));
222         }
223     }
224
225     private boolean verifyNode(JsonNode internalNode) {
226         return nodeNullCheck(internalNode)
227                 && "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
228                 && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText())
229                     || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()));
230     }
231
232     private boolean nodeNullCheck(JsonNode internalNode) {
233         return internalNode.get("dependency") != null && internalNode.findValue("capability") != null
234                 && internalNode.findValue(RELATIONSHIP) != null;
235     }
236
237     protected Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
238         Iterator<Node<Vnfc>> itr = nodes.iterator();
239         Node<Vnfc> node;
240         while (itr.hasNext()) {
241             node = itr.next();
242             if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
243                 return node;
244             }
245         }
246         return null;
247     }
248     private String getVnfcType(String type) {
249         return type.substring(type.lastIndexOf('.') + 1, type.length());
250     }
251
252     protected ObjectMapper getMapper() {
253         return new ObjectMapper(new YAMLFactory());
254     }
255 }