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