2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.appc.seqgen.dgplugin.impl;
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;
45 import java.io.IOException;
48 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
50 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
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);
58 SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
59 List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
60 String output = objectMapper.writeValueAsString(sequence);
61 logger.debug("Sequence Generator Output " + output);
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());
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);
78 InventoryModel inventoryModel = buildInventoryModel(inputJson);
79 sequenceGeneratorInput.setInventoryModel(inventoryModel);
81 VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
82 sequenceGeneratorInput.setDependencyModel(dependencyModel);
84 return sequenceGeneratorInput;
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");
90 String action = sequenceGeneratorInput.getRequestInfo().getAction();
91 VNFOperation operation = VNFOperation.findByString(action);
92 if (operation == null) {
93 throw new APPCException("Invalid Action " + action);
95 SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
96 return sequenceGenerator.generateSequence(sequenceGeneratorInput);
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);
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);
117 currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
118 currentNode.getChild().setResilienceType(resilience);
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());
127 Vnfc parentVnfc = new Vnfc(parentVnfcType, null, null, false);
128 parentNode = new Node<>(parentVnfc);
129 currentNode.addParent(parentVnfc);
130 dependency.add(parentNode);
135 return new VnfcDependencyModel(dependency);
140 private String readResilience(JsonNode vnfcNode) {
141 String resilience = null;
142 if (vnfcNode.get("resilience") != null) {
143 resilience = vnfcNode.get("resilience").asText();
148 private String readMandatory(JsonNode vnfcNode) {
150 JsonNode mandatoryNode = vnfcNode.get("mandatory");
151 if (mandatoryNode == null) {
154 mandatory = mandatoryNode.asText();
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");
164 return vnfcTypeNode.asText();
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");
176 private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
177 for (Node<Vnfc> node : dependency) {
178 if (node.getChild().getVnfcType().equals(vnfcType)) {
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");
193 JsonNode vnfInfo = inventoryInfo.get("vnf-info");
194 if (vnfInfo == null) {
195 throw new APPCException("vnf-info is not provided in the input");
198 String vnfId = vnfInfo.get("vnf-id").asText();
199 String vnfType = vnfInfo.get("vnf-type").asText();
200 String vnfVersion = vnfInfo.get("vnf-version").asText();
202 Vnf vnf = new Vnf(vnfId, vnfType, vnfVersion);
204 JsonNode vms = vnfInfo.get("vm");
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 ");
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);
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);
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");
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);
231 vServers.add(vserver);
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);
241 return new InventoryModel(vnf);