Merging in bug fixes
[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
72             if(root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
73                 throw new InvalidDependencyModel("Dependency model is missing 'topology_template' or  'node_templates' elements");
74             }
75
76             JsonNode topologyTemplateNode = root.get(TOPOLOGY_TEMPLATE);
77             JsonNode nodeTemplateNode = topologyTemplateNode.get("node_templates");
78             Iterator<Map.Entry<String, JsonNode>> itretor  = nodeTemplateNode.fields();
79             for (JsonNode yamlNode : nodeTemplateNode) {
80                 logger.debug("Processing node: " + yamlNode);
81                 String fullvnfcType = itretor.next().getValue().get("type").textValue();
82                 String vnfcType= getQualifiedVnfcType(fullvnfcType);
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,nodeTemplateNode);
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 String getQualifiedVnfcType(String fullvnfcType) {
130         return fullvnfcType.substring(fullvnfcType.lastIndexOf(".")+1,fullvnfcType.length());
131     }
132
133     private void addDependencies(Set<Node<Vnfc>> nodes, Node node, String[] parentList) {
134         for (String type : parentList) {
135             String parentType = getVnfcType(type);
136             Node<Vnfc> parentNode = getNode(nodes, parentType);
137             if (parentNode != null) {
138                 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
139                 node.addParent(parentNode.getChild());
140             } else {
141                 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
142                 parentNode = new Node<>(new Vnfc(parentType, null));
143                 node.addParent(parentNode.getChild());
144                 logger.debug("Adding VNFC to dependency model : " + parentNode);
145                 nodes.add(parentNode);
146             }
147         }
148     }
149
150     private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode) {
151         JsonNode requirementsNode = node.get("requirements");
152         List<String> dependencyList  = new ArrayList();
153         if(requirementsNode!=null) {
154             for (JsonNode internalNode : requirementsNode) {
155                 //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
156                 if (nodeNullCheck(internalNode) &&"tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
157                         && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue("relationship").asText()) || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("relationship").asText()))) {
158                     if(internalNode.findValue("node") != null) {
159                         String nodeName = internalNode.findValue("node").asText();
160                         String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
161                         dependencyList.add(getQualifiedVnfcType(fullVnfcName));
162                     }else{
163                         throw new InvalidDependencyModel("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
164                     }
165                 }
166             }
167             return  dependencyList.toArray(new String[0]);
168         }else{
169             return new String[0];
170         }
171     }
172
173     private boolean nodeNullCheck(JsonNode internalNode) {
174         return internalNode.get("dependency") != null && internalNode.findValue("capability") != null && internalNode.findValue("relationship") != null;
175     }
176
177     private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
178         Iterator itr = nodes.iterator();
179         Node<Vnfc> node;
180         while (itr.hasNext()) {
181             node = (Node<Vnfc>) itr.next();
182             if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
183                 return node;
184             }
185         }
186         return null;
187     }
188
189     private String getVnfcType(String type) {
190         return type.substring(type.lastIndexOf('.') + 1, type.length());
191     }
192
193 }