Update lic header in appc sdc-listener and seq-gen
[appc.git] / appc-sequence-generator / appc-sequence-generator-bundle / src / main / java / org / onap / appc / seqgen / dgplugin / impl / SequenceGeneratorPluginImpl.java
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.seqgen.dgplugin.impl;
25
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.core.JsonParser;
30 import com.fasterxml.jackson.databind.DeserializationFeature;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
34 import org.onap.appc.dg.objects.InventoryModel;
35 import org.onap.appc.dg.objects.Node;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.domainmodel.Vnf;
38 import org.onap.appc.domainmodel.Vnfc;
39 import org.onap.appc.domainmodel.Vserver;
40 import org.onap.appc.domainmodel.lcm.VNFOperation;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.seqgen.SequenceGenerator;
43 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
44 import org.onap.appc.seqgen.impl.SequenceGeneratorFactory;
45 import org.onap.appc.seqgen.objects.Constants;
46 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
47 import org.onap.appc.seqgen.objects.Transaction;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
49
50 import java.io.IOException;
51 import java.util.Map;
52 import java.util.List;
53 import java.util.HashSet;
54 import java.util.Set;
55 import java.util.HashMap;
56 import java.util.LinkedList;
57
58 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
59
60     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
61
62     @Override
63     public void generateSequence(Map<String, String> params, SvcLogicContext context) {
64         ObjectMapper objectMapper = new ObjectMapper();
65         String inputJSON = context.getAttribute("inputJSON");
66         logger.debug("Input to Sequence Generator " + inputJSON);
67         try {
68             SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
69             List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
70             String output = objectMapper.writeValueAsString(sequence);
71             logger.debug("Sequence Generator Output " + output);
72
73             context.setAttribute("output", output);
74         } catch (Exception e) {
75             logger.error("Error generating sequence", e);
76             context.setAttribute("error-code", "401");
77             context.setAttribute("error-message", "Error generating sequence " + e.getMessage());
78         }
79     }
80
81     private SequenceGeneratorInput buildSequenceGeneratorInput(String inputJson) throws IOException, APPCException {
82         ObjectMapper objectMapper = new ObjectMapper();
83         SequenceGeneratorInput sequenceGeneratorInput ;
84         objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
85         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
86         sequenceGeneratorInput = objectMapper.readValue(inputJson, SequenceGeneratorInput.class);
87
88         InventoryModel inventoryModel = buildInventoryModel(inputJson);
89         sequenceGeneratorInput.setInventoryModel(inventoryModel);
90
91         VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
92         if(dependencyModel!=null){
93             validateInventoryModelWithDependencyModel(dependencyModel,inventoryModel);
94         }
95         sequenceGeneratorInput.setDependencyModel(dependencyModel);
96
97         return sequenceGeneratorInput;
98     }
99     private List<Transaction> generateSequence(SequenceGeneratorInput sequenceGeneratorInput) throws APPCException {
100         if (sequenceGeneratorInput.getRequestInfo() == null) {
101             throw new APPCException("Request info is not provided in the input");
102         }
103         String action = sequenceGeneratorInput.getRequestInfo().getAction();
104         VNFOperation operation = VNFOperation.findByString(action);
105         if (operation == null) {
106             throw new APPCException("Invalid Action " + action);
107         }
108         if(Constants.ActionLevel.findByString(sequenceGeneratorInput.getRequestInfo().getActionLevel().toUpperCase())==null){
109             throw new APPCException("Invalid Action Level " + sequenceGeneratorInput.getRequestInfo().getActionLevel());
110         }
111         SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
112         return sequenceGenerator.generateSequence(sequenceGeneratorInput);
113     }
114
115     private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
116         Set<String> dependencyModelVnfcSet = new HashSet<>();
117         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
118         Set<String> inventoryModelVnfcsSet = new HashSet<>();
119
120         for (Node<Vnfc> node : dependencyModel.getDependencies()) {
121             dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
122             if (node.getChild().isMandatory()) {
123                 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
124             }
125         }
126
127         for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
128             inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
129         }
130
131         // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
132         if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
133             return;
134         }
135
136         if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
137             Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
138             difference.removeAll(dependencyModelVnfcSet);
139             logger.error("Dependency model is missing following vnfc type(s): " + difference);
140             throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
141         } else {
142             Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
143             difference.removeAll(inventoryModelVnfcsSet);
144             if (difference.size() > 0) {
145                 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
146                 throw new APPCException("VMs missing for the mandatory VNFC : " + difference);
147             }
148         }
149     }
150
151     // Dependency model is an optional attribute and may contain null values
152     private VnfcDependencyModel buildDependencyModel(String inputJson) throws IOException, APPCException {
153         Set<Node<Vnfc>> dependency = new HashSet<>();
154         Set<String> parentVnfcs=new HashSet<>();
155         Set<String> allVnfcTypes=new HashSet<>();
156         ObjectMapper objectMapper = new ObjectMapper();
157         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
158         JsonNode rootNode = objectMapper.readTree(inputJson);
159         JsonNode vnfcs = getVnfcsNode(rootNode);
160         if (vnfcs != null) {
161             for (JsonNode vnfcNode : vnfcs) {
162                 String vnfcType = readVnfcType(vnfcNode);
163                 allVnfcTypes.add(vnfcType);
164                 String mandatory = readMandatory(vnfcNode);
165                 String resilience = readResilience(vnfcNode);
166                 Vnfc vnfc = new Vnfc();
167                 vnfc.setVnfcType(vnfcType);
168                 vnfc.setResilienceType(resilience);
169                 vnfc.setMandatory(Boolean.parseBoolean(mandatory));
170                 Node<Vnfc> currentNode = getNode(dependency, vnfcType);
171                 if (currentNode == null) {
172                     currentNode = new Node<>(vnfc);
173                     dependency.add(currentNode);
174                 } else {
175                     currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
176                     currentNode.getChild().setResilienceType(resilience);
177                 }
178                 JsonNode parents = vnfcNode.get("parents");
179                 for (JsonNode parent : parents) {
180                     String parentVnfcType = parent.asText();
181                     parentVnfcs.add(parentVnfcType);
182                     Node<Vnfc> parentNode = getNode(dependency, parentVnfcType);
183                     if (parentNode != null) {
184                         currentNode.addParent(parentNode.getChild());
185                     } else {
186                         Vnfc parentVnfc=new Vnfc();
187                         parentVnfc.setVnfcType(parentVnfcType);
188                         parentVnfc.setMandatory(false);
189                         parentNode = new Node<>(parentVnfc);
190                         currentNode.addParent(parentVnfc);
191                         dependency.add(parentNode);
192                     }
193                 }
194
195             }
196             for(String parent:parentVnfcs){
197                 if(!allVnfcTypes.contains(parent)){
198                     throw new APPCException("Dependency model missing vnfc type "+parent);
199                 }
200             }
201             return new VnfcDependencyModel(dependency);
202         }
203         return null;
204     }
205
206     private String readResilience(JsonNode vnfcNode) {
207         String resilience = null;
208         if (vnfcNode.get("resilience") != null) {
209             resilience = vnfcNode.get("resilience").asText();
210         }
211         return resilience;
212     }
213
214     private String readMandatory(JsonNode vnfcNode) {
215         String mandatory ;
216         JsonNode mandatoryNode = vnfcNode.get("mandatory");
217         if (mandatoryNode == null) {
218             mandatory = "false";
219         } else {
220             mandatory = mandatoryNode.asText();
221         }
222         return mandatory;
223     }
224
225     private String readVnfcType(JsonNode vnfcNode) throws APPCException {
226         JsonNode vnfcTypeNode = vnfcNode.get(Constants.VNFC_TYPE);
227         if (vnfcTypeNode == null) {
228             throw new APPCException("vnfc-type is not available in dependency info");
229         }
230         return vnfcTypeNode.asText();
231     }
232
233     private JsonNode getVnfcsNode(JsonNode rootNode) {
234         JsonNode dependencyInfo = rootNode.get("dependency-info");
235         JsonNode vnfcs = null;
236         if (dependencyInfo != null) {
237             vnfcs = dependencyInfo.get("vnfcs");
238         }
239         return vnfcs;
240     }
241
242     private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
243         for (Node<Vnfc> node : dependency) {
244             if (node.getChild().getVnfcType().equals(vnfcType)) {
245                 return node;
246             }
247         }
248         return null;
249     }
250
251     private InventoryModel buildInventoryModel(String inputJson) throws IOException, APPCException {
252         ObjectMapper objectMapper = new ObjectMapper();
253         JsonNode jsonNode = objectMapper.readTree(inputJson);
254         JsonNode inventoryInfo = jsonNode.get("inventory-info");
255         if (inventoryInfo == null) {
256             throw new APPCException("inventory-info is not provided in the input");
257         }
258         JsonNode vnfInfo = inventoryInfo.get("vnf-info");
259         if (vnfInfo == null) {
260             throw new APPCException("vnf-info is not provided in the input");
261         }
262
263         String vnfId = vnfInfo.get("vnf-id").asText();
264         String vnfType = vnfInfo.get("vnf-type").asText();
265         Vnf vnf =new Vnf();
266         vnf.setVnfId(vnfId);
267         vnf.setVnfType(vnfType);
268         Map<Vnfc, List<Vserver>> vfcs = new HashMap<>();
269         JsonNode vms = vnfInfo.get("vm");
270         if(vms.size()<1){
271             throw new APPCException("vm info not provided in the input");
272         }
273         for (JsonNode vm : vms) {
274             if(vm.get("vserver-id")== null){
275                 throw new APPCException("vserver-id not found ");
276             }
277             String vserverId = vm.get("vserver-id").asText();
278             Vserver vserver = new Vserver();
279             vserver.setId(vserverId);
280             if (vm.get("vnfc")!=null&& vm.get("vnfc").get("vnfc-name") != null && vm.get("vnfc").get("vnfc-type")!= null) {
281                 Vnfc vfc = new Vnfc();
282                 vfc.setVnfcType(vm.get("vnfc").get("vnfc-type").asText());
283                 vfc.setVnfcName(vm.get("vnfc").get("vnfc-name").asText());
284                 vserver.setVnfc(vfc);
285                 List<Vserver> vServers = vfcs.get(vfc);
286                 if (vServers == null) {
287                     vServers = new LinkedList<>();
288                     vfcs.put(vfc, vServers);
289                 }
290                 vServers.add(vserver);
291             }
292             vnf.addVserver(vserver);
293         }
294
295         for (Map.Entry<Vnfc, List<Vserver>> entry : vfcs.entrySet()) {
296             Vnfc vnfc = entry.getKey();
297             List<Vserver> vServers = vfcs.get(vnfc);
298             vnfc.addVservers(vServers);
299         }
300
301         return new InventoryModel(vnf);
302     }
303 }