Third part of onap rename
[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 : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.seqgen.dgplugin.impl;
22
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.fasterxml.jackson.core.JsonParser;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.appc.dg.objects.*;
32 import org.onap.appc.domainmodel.Vnf;
33 import org.onap.appc.domainmodel.Vnfc;
34 import org.onap.appc.domainmodel.Vserver;
35 import org.onap.appc.domainmodel.lcm.VNFOperation;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.seqgen.SequenceGenerator;
38 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
39 import org.onap.appc.seqgen.impl.SequenceGeneratorFactory;
40 import org.onap.appc.seqgen.objects.Constants;
41 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
42 import org.onap.appc.seqgen.objects.Transaction;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44
45 import java.io.IOException;
46 import java.util.*;
47
48 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
49
50     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
51
52     @Override
53     public void generateSequence(Map<String, String> params, SvcLogicContext context) {
54         ObjectMapper objectMapper = new ObjectMapper();
55         String inputJSON = context.getAttribute("inputJSON");
56         logger.debug("Input to Sequence Generator " + inputJSON);
57         try {
58             SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
59             List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
60             String output = objectMapper.writeValueAsString(sequence);
61             logger.debug("Sequence Generator Output " + output);
62
63             context.setAttribute("output", output);
64         } catch (Exception e) {
65             logger.error("Error generating sequence", e);
66             context.setAttribute("error-code", "401");
67             context.setAttribute("error-message", "Error generating sequence " + e.getMessage());
68         }
69     }
70
71     private SequenceGeneratorInput buildSequenceGeneratorInput(String inputJson) throws IOException, APPCException {
72         ObjectMapper objectMapper = new ObjectMapper();
73         SequenceGeneratorInput sequenceGeneratorInput ;
74         objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
75         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
76         sequenceGeneratorInput = objectMapper.readValue(inputJson, SequenceGeneratorInput.class);
77
78         InventoryModel inventoryModel = buildInventoryModel(inputJson);
79         sequenceGeneratorInput.setInventoryModel(inventoryModel);
80
81         VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
82         sequenceGeneratorInput.setDependencyModel(dependencyModel);
83
84         return sequenceGeneratorInput;
85     }
86     private List<Transaction> generateSequence(SequenceGeneratorInput sequenceGeneratorInput) throws APPCException {
87         if (sequenceGeneratorInput.getRequestInfo() == null) {
88             throw new APPCException("Request info is not provided in the input");
89         }
90         String action = sequenceGeneratorInput.getRequestInfo().getAction();
91         VNFOperation operation = VNFOperation.findByString(action);
92         if (operation == null) {
93             throw new APPCException("Invalid Action " + action);
94         }
95         SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
96         return sequenceGenerator.generateSequence(sequenceGeneratorInput);
97     }
98
99     // Dependency model is an optional attribute and may contain null values
100     private VnfcDependencyModel buildDependencyModel(String inputJson) throws IOException, APPCException {
101         Set<Node<Vnfc>> dependency = new HashSet<>();
102         ObjectMapper objectMapper = new ObjectMapper();
103         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
104         JsonNode rootNode = objectMapper.readTree(inputJson);
105         JsonNode vnfcs = getVnfcsNode(rootNode);
106         if (vnfcs != null) {
107             for (JsonNode vnfcNode : vnfcs) {
108                 String vnfcType = readVnfcType(vnfcNode);
109                 String mandatory = readMandatory(vnfcNode);
110                 String resilience = readResilience(vnfcNode);
111                 Vnfc vnfc = new Vnfc(vnfcType, resilience, null, Boolean.parseBoolean(mandatory));
112                 Node<Vnfc> currentNode = getNode(dependency, vnfcType);
113                 if (currentNode == null) {
114                     currentNode = new Node<>(vnfc);
115                     dependency.add(currentNode);
116                 } else {
117                     currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
118                     currentNode.getChild().setResilienceType(resilience);
119                 }
120                 JsonNode parents = vnfcNode.get("parents");
121                 for (JsonNode parent : parents) {
122                     String parentVnfcType = parent.asText();
123                     Node<Vnfc> parentNode = getNode(dependency, parentVnfcType);
124                     if (parentNode != null) {
125                         currentNode.addParent(parentNode.getChild());
126                     } else {
127                         Vnfc parentVnfc = new Vnfc(parentVnfcType, null, null, false);
128                         parentNode = new Node<>(parentVnfc);
129                         currentNode.addParent(parentVnfc);
130                         dependency.add(parentNode);
131                     }
132                 }
133
134             }
135             return new VnfcDependencyModel(dependency);
136         }
137         return null;
138     }
139
140     private String readResilience(JsonNode vnfcNode) {
141         String resilience = null;
142         if (vnfcNode.get("resilience") != null) {
143             resilience = vnfcNode.get("resilience").asText();
144         }
145         return resilience;
146     }
147
148     private String readMandatory(JsonNode vnfcNode) {
149         String mandatory ;
150         JsonNode mandatoryNode = vnfcNode.get("mandatory");
151         if (mandatoryNode == null) {
152             mandatory = "false";
153         } else {
154             mandatory = mandatoryNode.asText();
155         }
156         return mandatory;
157     }
158
159     private String readVnfcType(JsonNode vnfcNode) throws APPCException {
160         JsonNode vnfcTypeNode = vnfcNode.get(Constants.VNFC_TYPE);
161         if (vnfcTypeNode == null) {
162             throw new APPCException("vnfc-type is not available in dependency info");
163         }
164         return vnfcTypeNode.asText();
165     }
166
167     private JsonNode getVnfcsNode(JsonNode rootNode) {
168         JsonNode dependencyInfo = rootNode.get("dependency-info");
169         JsonNode vnfcs = null;
170         if (dependencyInfo != null) {
171             vnfcs = dependencyInfo.get("vnfcs");
172         }
173         return vnfcs;
174     }
175
176     private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
177         for (Node<Vnfc> node : dependency) {
178             if (node.getChild().getVnfcType().equals(vnfcType)) {
179                 return node;
180             }
181         }
182         return null;
183     }
184
185     private InventoryModel buildInventoryModel(String inputJson) throws IOException, APPCException {
186         ObjectMapper objectMapper = new ObjectMapper();
187         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
188         JsonNode jsonNode = objectMapper.readTree(inputJson);
189         JsonNode inventoryInfo = jsonNode.get("inventory-info");
190         if (inventoryInfo == null) {
191             throw new APPCException("inventory-info is not provided in the input");
192         }
193         JsonNode vnfInfo = inventoryInfo.get("vnf-info");
194         if (vnfInfo == null) {
195             throw new APPCException("vnf-info is not provided in the input");
196         }
197
198         String vnfId = vnfInfo.get("vnf-id").asText();
199         String vnfType = vnfInfo.get("vnf-type").asText();
200         String vnfVersion = vnfInfo.get("vnf-version").asText();
201
202         Vnf vnf = new Vnf(vnfId, vnfType, vnfVersion);
203
204         JsonNode vms = vnfInfo.get("vm");
205
206         Map<Vnfc, List<Vserver>> vfcs = new HashMap<>();
207         for (JsonNode vm : vms) {
208             if(vm.get("vserver-id")== null){
209                 throw new APPCException("vserver-id not found ");
210             }
211             String vserverId = vm.get("vserver-id").asText();
212             Vserver vserver = new Vserver(null, null, vserverId, null, null);
213             JsonNode vnfc = vm.get("vnfc");
214             if (vnfc.get("vnfc-name") == null) {
215                 throw new APPCException("vnfc-name not found for vserver " + vserverId);
216             }
217             String vnfcName = vnfc.get("vnfc-name").asText();
218             if (vnfc.get("vnfc-type") == null) {
219                 throw new APPCException("vnfc-type not found for vserver " + vserverId);
220             }
221             String vnfcType = vnfc.get("vnfc-type").asText();
222             if (StringUtils.isEmpty(vnfcType)) {
223                 throw new APPCException("vserver " + vserverId + " is not associated with any vnfc");
224             }
225             Vnfc vfc = new Vnfc(vnfcType, null, vnfcName);
226             List<Vserver> vServers = vfcs.get(vfc);
227             if (vServers == null) {
228                 vServers = new LinkedList<>();
229                 vfcs.put(vfc, vServers);
230             }
231             vServers.add(vserver);
232         }
233
234         for (Map.Entry<Vnfc, List<Vserver>> entry : vfcs.entrySet()) {
235             Vnfc vnfc = entry.getKey();
236             List<Vserver> vServers = vfcs.get(vnfc);
237             vnfc.addVms(vServers);
238             vnf.addVnfc(vnfc);
239         }
240
241         return new InventoryModel(vnf);
242     }
243 }