2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.seqgen.dgplugin.impl;
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.CapabilityModel;
46 import org.onap.appc.seqgen.objects.Constants;
47 import org.onap.appc.seqgen.objects.Constants.ActionLevel;
48 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
49 import org.onap.appc.seqgen.objects.Transaction;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
52 import java.io.IOException;
54 import java.util.List;
55 import java.util.HashSet;
56 import java.util.Iterator;
58 import java.util.ArrayList;
59 import java.util.HashMap;
60 import java.util.LinkedList;
62 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
64 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
67 public void generateSequence(Map<String, String> params, SvcLogicContext context) {
68 ObjectMapper objectMapper = new ObjectMapper();
69 String inputJSON = context.getAttribute("inputJSON");
70 logger.debug("Input to Sequence Generator " + inputJSON);
72 SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
73 List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
74 if (sequence.isEmpty()) {
75 logger.error("Transaction list is empty");
76 context.setAttribute("error-code", "450");
77 context.setAttribute("error-message", "Request is not supported");
79 String output = objectMapper.writeValueAsString(sequence);
80 logger.info("Sequence Generator Output " + output);
81 context.setAttribute("output", output);
83 } catch (Exception e) {
84 logger.error("Error generating sequence", e);
85 context.setAttribute("error-code", "401");
86 context.setAttribute("error-message", "Error generating sequence " + e.getMessage());
90 private SequenceGeneratorInput buildSequenceGeneratorInput(String inputJson) throws IOException, APPCException {
91 ObjectMapper objectMapper = new ObjectMapper();
92 SequenceGeneratorInput sequenceGeneratorInput ;
93 objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
94 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
95 sequenceGeneratorInput = objectMapper.readValue(inputJson, SequenceGeneratorInput.class);
97 InventoryModel inventoryModel = buildInventoryModel(inputJson);
98 sequenceGeneratorInput.setInventoryModel(inventoryModel);
100 VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
101 if(dependencyModel != null){
102 validateInventoryModelWithDependencyModel(dependencyModel,inventoryModel);
104 sequenceGeneratorInput.setDependencyModel(dependencyModel);
106 CapabilityModel capModel = buildCapabilitiesModel(inputJson);
107 sequenceGeneratorInput.setCapabilityModel(capModel);
108 return sequenceGeneratorInput;
111 private List<Transaction> generateSequence(SequenceGeneratorInput sequenceGeneratorInput) throws Exception {
112 if (sequenceGeneratorInput.getRequestInfo() == null) {
113 throw new APPCException("Request info is not provided in the input");
115 String action = sequenceGeneratorInput.getRequestInfo().getAction();
116 VNFOperation operation = VNFOperation.findByString(action);
117 if (operation == null) {
118 throw new APPCException("Invalid Action " + action);
120 if(Constants.ActionLevel.findByString(sequenceGeneratorInput.getRequestInfo().getActionLevel().toUpperCase())==null){
121 throw new APPCException("Invalid Action Level " + sequenceGeneratorInput.getRequestInfo().getActionLevel());
123 SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
124 return sequenceGenerator.generateSequence(sequenceGeneratorInput);
127 private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
128 Set<String> dependencyModelVnfcSet = new HashSet<>();
129 Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
130 Set<String> inventoryModelVnfcsSet = new HashSet<>();
132 for (Node<Vnfc> node : dependencyModel.getDependencies()) {
133 dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
134 if (node.getChild().isMandatory()) {
135 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
139 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
140 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
143 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
144 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
148 if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
149 Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
150 difference.removeAll(dependencyModelVnfcSet);
151 logger.error("Dependency model is missing following vnfc type(s): " + difference);
152 throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
154 Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
155 difference.removeAll(inventoryModelVnfcsSet);
156 if (difference.size() > 0) {
157 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
158 throw new APPCException("VMs missing for the mandatory VNFC : " + difference);
163 // Dependency model is an optional attribute and may contain null values
164 private VnfcDependencyModel buildDependencyModel(String inputJson) throws IOException, APPCException {
165 Set<Node<Vnfc>> dependency = new HashSet<>();
166 Set<String> parentVnfcs=new HashSet<>();
167 Set<String> allVnfcTypes=new HashSet<>();
168 ObjectMapper objectMapper = new ObjectMapper();
169 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
170 JsonNode rootNode = objectMapper.readTree(inputJson);
171 JsonNode vnfcs = getVnfcsNode(rootNode);
173 for (JsonNode vnfcNode : vnfcs) {
174 String vnfcType = readVnfcType(vnfcNode);
175 allVnfcTypes.add(vnfcType);
176 String mandatory = readMandatory(vnfcNode);
177 String resilience = readResilience(vnfcNode);
178 Vnfc vnfc = new Vnfc();
179 vnfc.setVnfcType(vnfcType);
180 vnfc.setResilienceType(resilience);
181 vnfc.setMandatory(Boolean.parseBoolean(mandatory));
182 Node<Vnfc> currentNode = getNode(dependency, vnfcType);
183 if (currentNode == null) {
184 currentNode = new Node<>(vnfc);
185 dependency.add(currentNode);
187 currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
188 currentNode.getChild().setResilienceType(resilience);
190 JsonNode parents = vnfcNode.get("parents");
191 for (JsonNode parent : parents) {
192 String parentVnfcType = parent.asText();
193 parentVnfcs.add(parentVnfcType);
194 Node<Vnfc> parentNode = getNode(dependency, parentVnfcType);
195 if (parentNode != null) {
196 currentNode.addParent(parentNode.getChild());
198 Vnfc parentVnfc=new Vnfc();
199 parentVnfc.setVnfcType(parentVnfcType);
200 parentVnfc.setMandatory(false);
201 parentNode = new Node<>(parentVnfc);
202 currentNode.addParent(parentVnfc);
203 dependency.add(parentNode);
208 for(String parent:parentVnfcs){
209 if(!allVnfcTypes.contains(parent)){
210 throw new APPCException("Dependency model missing vnfc type "+parent);
213 return new VnfcDependencyModel(dependency);
218 private String readResilience(JsonNode vnfcNode) {
219 String resilience = null;
220 if (vnfcNode.get("resilience") != null) {
221 resilience = vnfcNode.get("resilience").asText();
226 private String readMandatory(JsonNode vnfcNode) {
228 JsonNode mandatoryNode = vnfcNode.get("mandatory");
229 if (mandatoryNode == null) {
232 mandatory = mandatoryNode.asText();
237 private String readVnfcType(JsonNode vnfcNode) throws APPCException {
238 JsonNode vnfcTypeNode = vnfcNode.get(Constants.VNFC_TYPE);
239 if (vnfcTypeNode == null) {
240 throw new APPCException("vnfc-type is not available in dependency info");
242 return vnfcTypeNode.asText();
245 private JsonNode getVnfcsNode(JsonNode rootNode) {
246 JsonNode dependencyInfo = rootNode.get("dependency-info");
247 JsonNode vnfcs = null;
248 if (dependencyInfo != null) {
249 vnfcs = dependencyInfo.get("vnfcs");
254 private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
255 for (Node<Vnfc> node : dependency) {
256 if (node.getChild().getVnfcType().equals(vnfcType)) {
263 private InventoryModel buildInventoryModel(String inputJson) throws IOException, APPCException {
264 ObjectMapper objectMapper = new ObjectMapper();
265 JsonNode jsonNode = objectMapper.readTree(inputJson);
266 JsonNode inventoryInfo = jsonNode.get("inventory-info");
267 if (inventoryInfo == null) {
268 throw new APPCException("inventory-info is not provided in the input");
270 JsonNode vnfInfo = inventoryInfo.get("vnf-info");
271 if (vnfInfo == null) {
272 throw new APPCException("vnf-info is not provided in the input");
275 String vnfId = vnfInfo.get("vnf-id").asText();
276 String vnfType = vnfInfo.get("vnf-type").asText();
277 String identityUrl = vnfInfo.get("identity-url").asText();
280 vnf.setVnfType(vnfType);
281 vnf.setIdentityUrl(identityUrl);
282 logger.debug("IdentityUrl in SeqGen:" + identityUrl);
283 Map<Vnfc, List<Vserver>> vfcs = new HashMap<>();
284 JsonNode vms = vnfInfo.get("vm");
286 throw new APPCException("vm info not provided in the input");
288 for (JsonNode vm : vms) {
289 if(vm.get("vserver-id")== null){
290 throw new APPCException("vserver-id not found ");
292 String vserverId = vm.get("vserver-id").asText();
293 String vmId =vm.get("vm-id").asText();
294 Vserver vserver = new Vserver();
295 vserver.setId(vserverId);
296 vserver.setUrl(vmId);
297 Vnfc vfc = new Vnfc();
298 if (vm.get("vnfc") != null ) {
299 if (vm.get("vnfc").get("vnfc-name") != null)
300 vfc.setVnfcName(vm.get("vnfc").get("vnfc-name").asText());
301 if (vm.get("vnfc").get("vnfc-type") != null)
302 vfc.setVnfcType(vm.get("vnfc").get("vnfc-type").asText());
303 if (vm.get("vnfc").get("vnfc-function-code") != null)
304 vfc.setVnfcFunctionCode(vm.get("vnfc").get("vnfc-function-code").asText());
305 vserver.setVnfc(vfc);
306 List<Vserver> vServers = vfcs.get(vfc);
307 if (vServers == null) {
308 vServers = new LinkedList<>();
309 vfcs.put(vfc, vServers);
311 vServers.add(vserver);
313 vnf.addVserver(vserver);
316 for (Map.Entry<Vnfc, List<Vserver>> entry : vfcs.entrySet()) {
317 Vnfc vnfc = entry.getKey();
318 List<Vserver> vServers = vfcs.get(vnfc);
319 vnfc.addVservers(vServers);
322 return new InventoryModel(vnf);
325 private CapabilityModel buildCapabilitiesModel(String inputJson) throws IOException, APPCException {
326 logger.info("Entering buildCapabilitiesModel");
328 ObjectMapper objectMapper = new ObjectMapper();
329 JsonNode jsonNode = objectMapper.readTree(inputJson);
330 JsonNode capabilitiesNode = jsonNode.get("capabilities");
331 if (capabilitiesNode == null) {
335 List<String> vnfCapabilities = new ArrayList<>();
336 List<String> vfModuleCapabilities = new ArrayList<>();
337 Map<String, List<String>> vmCapabilities = new HashMap<>();
338 List<String> vnfcCapabilities = new ArrayList<>();
340 JsonNode vnfNode = capabilitiesNode.get(ActionLevel.VNF.getAction());
341 JsonNode vfModuleNode = capabilitiesNode.get(ActionLevel.VF_MODULE.getAction());
342 JsonNode vmNode = capabilitiesNode.get(ActionLevel.VM.getAction());
343 JsonNode vnfcNode = capabilitiesNode.get(ActionLevel.VNFC.getAction());
345 if (vnfNode != null && vnfNode.isArray() ) {
346 for (JsonNode nodes : vnfNode) {
347 vnfCapabilities.add(nodes.asText());
350 if (vfModuleNode != null && vfModuleNode.isArray() ){
351 for (JsonNode nodes : vfModuleNode) {
352 vfModuleCapabilities.add(nodes.asText());
355 if (vmNode != null && vmNode.isArray() ){
356 for (JsonNode jNode : vmNode) {
357 logger.debug("jNode=" + jNode);
358 Iterator<Map.Entry<String,JsonNode>> fldIter = jNode.fields();
359 while (fldIter.hasNext()) {
360 Map.Entry<String,JsonNode> currentEntry = fldIter.next();
361 logger.debug("currentEntry.getKey()=" + currentEntry.getKey());
362 logger.debug("currentEntry.getValue()=" + currentEntry.getValue());
363 if (currentEntry.getValue().isArray()) {
364 logger.debug("currentEntry.getValue().isArray() is true");
365 List<String> ls = new ArrayList<String>();
366 for (JsonNode node: currentEntry.getValue()) {
367 ls.add(node.asText());
369 vmCapabilities.put(currentEntry.getKey(), ls);
374 if (vnfcNode != null && vnfcNode.isArray() ){
375 for (JsonNode nodes : vnfcNode) {
376 vnfcCapabilities.add(nodes.asText());
379 logger.info("vnfCapabilities=" + vnfCapabilities);
380 logger.info("vfModuleCapabilities=" + vfModuleCapabilities);
381 logger.info("vmCapabilities=" + vmCapabilities);
382 logger.info("vnfcCapabilities=" + vnfcCapabilities);
384 return new CapabilityModel(vnfCapabilities, vfModuleCapabilities, vmCapabilities, vnfcCapabilities);