Updated SDC listener and dependent bundles
[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 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.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 org.apache.commons.lang3.StringUtils;
34 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
35 import org.onap.appc.dg.objects.Node;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.domainmodel.Vnfc;
38
39 import java.io.IOException;
40 import java.util.*;
41
42 public class DependencyModelParser {
43
44     private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelParser.class);
45
46     private static final String PROPERTIES = "properties";
47     private static final String ACTIVE_ACTIVE = "Active-Active";
48     private static final String ACTIVE_PASSIVE = "Active-Passive";
49     private static final String HIGH_AVAILABLITY = "high_availablity";
50     private static final String MANDATORY = "mandatory";
51     private static final String TOPOLOGY_TEMPLATE = "topology_template";
52     private static final String RELATIONSHIP="relationship";
53
54     private static Map<String, String> dependencyMap;
55     static {
56         Map<String, String> dependencyTypeMappingMap =new HashMap<>();
57         dependencyTypeMappingMap.put("geo-activeactive", ACTIVE_ACTIVE);
58         dependencyTypeMappingMap.put("geo-activestandby", ACTIVE_PASSIVE);
59         dependencyTypeMappingMap.put("local-activeactive", ACTIVE_ACTIVE);
60         dependencyTypeMappingMap.put("local-activestandby", ACTIVE_PASSIVE);
61         dependencyMap = Collections.unmodifiableMap(dependencyTypeMappingMap);
62     }
63
64     public VnfcDependencyModel generateDependencyModel(String vnfModel,String vnfType) throws InvalidDependencyModelException {
65         Set<Node<Vnfc>> dependencies = new HashSet<>();
66         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
67         boolean mandatory;
68         String resilienceType;
69         String prefix = "org.onap.resource.vfc."+vnfType+".abstract.nodes.";
70         try {
71             ObjectNode root = (ObjectNode) mapper.readTree(vnfModel);
72
73             if(root.get(TOPOLOGY_TEMPLATE) == null || root.get(TOPOLOGY_TEMPLATE).get("node_templates") == null) {
74                 throw new InvalidDependencyModelException("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 fullvnfcType = itretor.next().getValue().get("type").textValue();
83                 String vnfcType= getQualifiedVnfcType(fullvnfcType);
84                 String type = yamlNode.get("type").textValue();
85                 type = type.substring(0,type.lastIndexOf('.')+1);
86                 if(type.concat(vnfcType).toLowerCase().startsWith(prefix.concat(vnfcType).toLowerCase())) {
87
88                     if(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY) == null || yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).asText().isEmpty()) {
89                         resilienceType = ACTIVE_ACTIVE;
90                     }else {
91                         resilienceType = dependencyMap.get(yamlNode.get(PROPERTIES).findValue(HIGH_AVAILABLITY).textValue());
92                     }
93
94                     if(yamlNode.get(PROPERTIES).findValue(MANDATORY) == null || yamlNode.get(PROPERTIES).findValue(MANDATORY).asText().isEmpty()) {
95                         mandatory = false;
96                     }else {
97                         mandatory = yamlNode.get(PROPERTIES).findValue(MANDATORY).booleanValue();
98                     }
99                     String[] parentList = getDependencyArray(yamlNode,nodeTemplateNode);
100                     Node<Vnfc> vnfcNode = getNode(dependencies, vnfcType);
101                     if (vnfcNode != null) {
102                         logger.debug("Dependency node already exists for vnfc Type: " + vnfcType);
103                         if (StringUtils.isEmpty(vnfcNode.getChild().getResilienceType())) {
104                             logger.debug("Updating resilience type, dependencies and mandatory attribute for VNFC type: " + vnfcType);
105                             vnfcNode.getChild().setResilienceType(resilienceType);
106                             if (parentList != null && parentList.length > 0) {
107                                 addDependencies(dependencies, vnfcNode, parentList);
108                             }
109                             vnfcNode.getChild().setMandatory(mandatory);
110                         }
111
112                     } else {
113                         logger.debug("Creating dependency node for  : " + vnfcType);
114                         vnfcNode = new Node<>(createVnfc(mandatory, resilienceType, vnfcType));
115                         if (parentList != null && parentList.length > 0)
116                             addDependencies(dependencies, vnfcNode, parentList);
117                         logger.debug("Adding VNFC to dependency model : " + vnfcNode);
118                         dependencies.add(vnfcNode);
119                     }
120                 }
121             }
122         } catch (IOException e) {
123             logger.error("Error parsing dependency model : " + vnfModel);
124             logger.error("Error message : " + e);
125             throw new InvalidDependencyModelException("Error parsing dependency model. " + e.getMessage());
126         }
127         return new VnfcDependencyModel(dependencies);
128     }
129
130     private Vnfc createVnfc(boolean mandatory, String resilienceType, String vnfcType) {
131         Vnfc vnfc = new Vnfc();
132         vnfc.setMandatory(mandatory);
133         vnfc.setResilienceType(resilienceType);
134         vnfc.setVnfcType(vnfcType);
135         return vnfc;
136     }
137
138     private String getQualifiedVnfcType(String fullvnfcType) {
139         return fullvnfcType.substring(fullvnfcType.lastIndexOf('.')+1,fullvnfcType.length());
140     }
141
142     private void addDependencies(Set<Node<Vnfc>> nodes, Node node, String[] parentList) {
143         for (String type : parentList) {
144             String parentType = getVnfcType(type);
145             Node<Vnfc> parentNode = getNode(nodes, parentType);
146             if (parentNode != null) {
147                 logger.debug("VNFC already exists for VNFC type: " + parentType + ". Adding it to parent list ");
148                 node.addParent(parentNode.getChild());
149             } else {
150                 logger.debug("VNFC does not exist for VNFC type: " + parentType + ". Creating new VNFC ");
151                 parentNode = new Node<>(createVnfc(false,null,parentType));
152                 node.addParent(parentNode.getChild());
153                 logger.debug("Adding VNFC to dependency model : " + parentNode);
154                 nodes.add(parentNode);
155             }
156         }
157     }
158
159     private String[] getDependencyArray(JsonNode node, JsonNode nodeTemplateNode) throws InvalidDependencyModelException {
160         JsonNode requirementsNode = node.get("requirements");
161         Set<String> dependencyList  = new HashSet<>();
162         if(requirementsNode!=null) {
163             for (JsonNode internalNode : requirementsNode) {
164                 //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
165                 if (nodeNullCheck(internalNode) &&"tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue("capability").asText())
166                         && ("tosca.relationships.DependsOn".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()) || "tosca.capabilities.Node".equalsIgnoreCase(internalNode.findValue(RELATIONSHIP).asText()))) {
167                     if(internalNode.findValue("node") != null) {
168                         String nodeName = internalNode.findValue("node").asText();
169                         String fullVnfcName = nodeTemplateNode.get(nodeName).get("type").asText();
170                         dependencyList.add(getQualifiedVnfcType(fullVnfcName));
171                     }else{
172                         throw new InvalidDependencyModelException("Error parsing dependency model. " + "Dependent Node not found for "+ node.get("type"));
173                     }
174                 }
175             }
176             return  dependencyList.toArray(new String[0]);
177         }else{
178             return new String[0];
179         }
180     }
181
182     private boolean nodeNullCheck(JsonNode internalNode) {
183         return internalNode.get("dependency") != null && internalNode.findValue("capability") != null && internalNode.findValue(RELATIONSHIP) != null;
184     }
185
186     private Node<Vnfc> getNode(Set<Node<Vnfc>> nodes, String vnfcType) {
187         Iterator itr = nodes.iterator();
188         Node<Vnfc> node;
189         while (itr.hasNext()) {
190             node = (Node<Vnfc>) itr.next();
191             if (node.getChild().getVnfcType().equalsIgnoreCase(vnfcType)) {
192                 return node;
193             }
194         }
195         return null;
196     }
197
198     private String getVnfcType(String type) {
199         return type.substring(type.lastIndexOf('.') + 1, type.length());
200     }
201
202 }