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