Applying license changes to all files
[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  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.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 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;
38
39 import java.io.IOException;
40 import java.util.*;
41
42
43 public class DependencyModelParser {
44
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";
53
54     static {
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);
61     }
62
63     public VnfcDependencyModel generateDependencyModel(String vnfModel,String vnfType) {
64         Set<Node<Vnfc>> dependencies = new HashSet<>();
65         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
66         boolean mandatory;
67         String resilienceType;
68         String prefix = "org.openecomp.resource.vfc."+vnfType+".abstract.nodes.";
69         try {
70             ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
71             logger.debug("VNF Model after parsing: " + root);
72
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");
75             }
76
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())) {
86
87                     if(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
88                         resilienceType = ACTIVE_ACTIVE;
89                     }else {
90                         resilienceType = dependencyMap.get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
91                     }
92
93                     if(yamlNode.get(PROPERTIES).findValue(MANDATORY) == null || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty()) {
94                         mandatory = false;
95                     }else {
96                         mandatory = yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
97                     }
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);
107                             }
108                             vnfcNode.getChild().setMandatory(mandatory);
109                         }
110
111                     } else {
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);
118                     }
119                 }
120             }
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());
125         }
126         return new VnfcDependencyModel(dependencies);
127     }
128
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());
136             } else {
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);
142             }
143         }
144     }
145
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());
155                     }else{
156                         throw new InvalidDependencyModel("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
157                     }
158                 }
159             }
160             return  dependencyList.toArray(new String[0]);
161         }else{
162             return new String[0];
163         }
164     }
165
166     private boolean nodeNullCheck(JsonNode internalNode) {
167         return internalNode.get("dependency") != null && internalNode.get("capability") != null && internalNode.get("relationship") != null;
168     }
169
170     private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
171         Iterator itr = nodes.iterator();
172         Node<Vnfc> node;
173         while (itr.hasNext()) {
174             node = (Node<Vnfc>) itr.next();
175             if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
176                 return node;
177             }
178         }
179         return null;
180     }
181
182     private String getVnfcType(String type) {
183         return type.substring(type.lastIndexOf('.') + 1, type.length());
184     }
185
186 }