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.objects.InventoryModel;
34 import org.onap.appc.dg.objects.Node;
35 import org.onap.appc.dg.objects.VnfcDependencyModel;
36 import org.onap.appc.domainmodel.Vnf;
37 import org.onap.appc.domainmodel.Vnfc;
38 import org.onap.appc.domainmodel.Vserver;
39 import org.onap.appc.domainmodel.lcm.VNFOperation;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.seqgen.SequenceGenerator;
42 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
43 import org.onap.appc.seqgen.impl.SequenceGeneratorFactory;
44 import org.onap.appc.seqgen.objects.CapabilityModel;
45 import org.onap.appc.seqgen.objects.Constants;
46 import org.onap.appc.seqgen.objects.Constants.ActionLevel;
47 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
48 import org.onap.appc.seqgen.objects.Transaction;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
51 import java.io.IOException;
53 import java.util.List;
54 import java.util.HashSet;
55 import java.util.Iterator;
57 import java.util.ArrayList;
58 import java.util.HashMap;
59 import java.util.LinkedList;
61 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
63 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
66 public void generateSequence(Map<String, String> params, SvcLogicContext context) {
67 ObjectMapper objectMapper = new ObjectMapper();
68 String inputJSON = context.getAttribute("inputJSON");
69 logger.debug("Input to Sequence Generator " + inputJSON);
71 SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
72 List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
73 if (sequence.isEmpty()) {
74 logger.error("Transaction list is empty");
75 context.setAttribute("error-code", "450");
76 context.setAttribute("error-message", "Request is not supported");
78 String output = objectMapper.writeValueAsString(sequence);
79 logger.info("Sequence Generator Output " + output);
80 context.setAttribute("output", output);
82 } catch (Exception e) {
83 logger.error("Error generating sequence", e);
84 context.setAttribute("error-code", "401");
85 context.setAttribute("error-message", "Error generating sequence " + e.getMessage());
89 private SequenceGeneratorInput buildSequenceGeneratorInput(String inputJson) throws IOException, APPCException {
90 ObjectMapper objectMapper = new ObjectMapper();
91 SequenceGeneratorInput sequenceGeneratorInput ;
92 objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
93 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
94 sequenceGeneratorInput = objectMapper.readValue(inputJson, SequenceGeneratorInput.class);
96 InventoryModel inventoryModel = buildInventoryModel(inputJson);
97 sequenceGeneratorInput.setInventoryModel(inventoryModel);
99 VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
100 if(dependencyModel != null){
101 validateInventoryModelWithDependencyModel(dependencyModel,inventoryModel);
103 sequenceGeneratorInput.setDependencyModel(dependencyModel);
105 CapabilityModel capModel = buildCapabilitiesModel(inputJson);
106 sequenceGeneratorInput.setCapabilityModel(capModel);
107 return sequenceGeneratorInput;
110 private List<Transaction> generateSequence(SequenceGeneratorInput sequenceGeneratorInput) throws Exception {
111 if (sequenceGeneratorInput.getRequestInfo() == null) {
112 throw new APPCException("Request info is not provided in the input");
114 String action = sequenceGeneratorInput.getRequestInfo().getAction();
115 VNFOperation operation = VNFOperation.findByString(action);
116 if (operation == null) {
117 throw new APPCException("Invalid Action " + action);
119 if(Constants.ActionLevel.findByString(sequenceGeneratorInput.getRequestInfo().getActionLevel().toUpperCase())==null){
120 throw new APPCException("Invalid Action Level " + sequenceGeneratorInput.getRequestInfo().getActionLevel());
122 SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
123 return sequenceGenerator.generateSequence(sequenceGeneratorInput);
126 private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
127 Set<String> dependencyModelVnfcSet = new HashSet<>();
128 Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
129 Set<String> inventoryModelVnfcsSet = new HashSet<>();
131 for (Node<Vnfc> node : dependencyModel.getDependencies()) {
132 dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
133 if (node.getChild().isMandatory()) {
134 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
138 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
139 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
142 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
143 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
147 if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
148 Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
149 difference.removeAll(dependencyModelVnfcSet);
150 logger.error("Dependency model is missing following vnfc type(s): " + difference);
151 throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
153 Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
154 difference.removeAll(inventoryModelVnfcsSet);
155 if (difference.size() > 0) {
156 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
157 throw new APPCException("VMs missing for the mandatory VNFC : " + difference);
162 // Dependency model is an optional attribute and may contain null values
163 private VnfcDependencyModel buildDependencyModel(String inputJson) throws IOException, APPCException {
164 Set<Node<Vnfc>> dependency = new HashSet<>();
165 Set<String> parentVnfcs=new HashSet<>();
166 Set<String> allVnfcTypes=new HashSet<>();
167 ObjectMapper objectMapper = new ObjectMapper();
168 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
169 JsonNode rootNode = objectMapper.readTree(inputJson);
170 JsonNode vnfcs = getVnfcsNode(rootNode);
172 for (JsonNode vnfcNode : vnfcs) {
173 String vnfcType = readVnfcType(vnfcNode);
174 allVnfcTypes.add(vnfcType);
175 String mandatory = readMandatory(vnfcNode);
176 String resilience = readResilience(vnfcNode);
177 Vnfc vnfc = new Vnfc();
178 vnfc.setVnfcType(vnfcType);
179 vnfc.setResilienceType(resilience);
180 vnfc.setMandatory(Boolean.parseBoolean(mandatory));
181 Node<Vnfc> currentNode = getNode(dependency, vnfcType);
182 if (currentNode == null) {
183 currentNode = new Node<>(vnfc);
184 dependency.add(currentNode);
186 currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
187 currentNode.getChild().setResilienceType(resilience);
189 JsonNode parents = vnfcNode.get("parents");
190 for (JsonNode parent : parents) {
191 String parentVnfcType = parent.asText();
192 parentVnfcs.add(parentVnfcType);
193 Node<Vnfc> parentNode = getNode(dependency, parentVnfcType);
194 if (parentNode != null) {
195 currentNode.addParent(parentNode.getChild());
197 Vnfc parentVnfc=new Vnfc();
198 parentVnfc.setVnfcType(parentVnfcType);
199 parentVnfc.setMandatory(false);
200 parentNode = new Node<>(parentVnfc);
201 currentNode.addParent(parentVnfc);
202 dependency.add(parentNode);
207 for(String parent:parentVnfcs){
208 if(!allVnfcTypes.contains(parent)){
209 throw new APPCException("Dependency model missing vnfc type "+parent);
212 return new VnfcDependencyModel(dependency);
217 private String readResilience(JsonNode vnfcNode) {
218 String resilience = null;
219 if (vnfcNode.get("resilience") != null) {
220 resilience = vnfcNode.get("resilience").asText();
225 private String readMandatory(JsonNode vnfcNode) {
227 JsonNode mandatoryNode = vnfcNode.get("mandatory");
228 if (mandatoryNode == null) {
231 mandatory = mandatoryNode.asText();
236 private String readVnfcType(JsonNode vnfcNode) throws APPCException {
237 JsonNode vnfcTypeNode = vnfcNode.get(Constants.VNFC_TYPE);
238 if (vnfcTypeNode == null) {
239 throw new APPCException("vnfc-type is not available in dependency info");
241 return vnfcTypeNode.asText();
244 private JsonNode getVnfcsNode(JsonNode rootNode) {
245 JsonNode dependencyInfo = rootNode.get("dependency-info");
246 JsonNode vnfcs = null;
247 if (dependencyInfo != null) {
248 vnfcs = dependencyInfo.get("vnfcs");
253 private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
254 for (Node<Vnfc> node : dependency) {
255 if (node.getChild().getVnfcType().equals(vnfcType)) {
262 private InventoryModel buildInventoryModel(String inputJson) throws IOException, APPCException {
263 ObjectMapper objectMapper = new ObjectMapper();
264 JsonNode jsonNode = objectMapper.readTree(inputJson);
265 JsonNode inventoryInfo = jsonNode.get("inventory-info");
266 if (inventoryInfo == null) {
267 throw new APPCException("inventory-info is not provided in the input");
269 JsonNode vnfInfo = inventoryInfo.get("vnf-info");
270 if (vnfInfo == null) {
271 throw new APPCException("vnf-info is not provided in the input");
274 String vnfId = vnfInfo.get("vnf-id").asText();
275 String vnfType = vnfInfo.get("vnf-type").asText();
276 String identityUrl = vnfInfo.get("identity-url").asText();
279 vnf.setVnfType(vnfType);
280 vnf.setIdentityUrl(identityUrl);
281 logger.debug("IdentityUrl in SeqGen:" + identityUrl);
282 Map<Vnfc, List<Vserver>> vfcs = new HashMap<>();
283 JsonNode vms = vnfInfo.get("vm");
285 throw new APPCException("vm info not provided in the input");
287 for (JsonNode vm : vms) {
288 if(vm.get("vserver-id")== null){
289 throw new APPCException("vserver-id not found ");
291 String vserverId = vm.get("vserver-id").asText();
292 String vmId =vm.get("vm-id").asText();
293 Vserver vserver = new Vserver();
294 vserver.setId(vserverId);
295 vserver.setUrl(vmId);
296 Vnfc vfc = new Vnfc();
297 if (vm.get("vnfc") != null ) {
298 if (vm.get("vnfc").get("vnfc-name") != null)
299 vfc.setVnfcName(vm.get("vnfc").get("vnfc-name").asText());
300 if (vm.get("vnfc").get("vnfc-type") != null)
301 vfc.setVnfcType(vm.get("vnfc").get("vnfc-type").asText());
302 if (vm.get("vnfc").get("vnfc-function-code") != null)
303 vfc.setVnfcFunctionCode(vm.get("vnfc").get("vnfc-function-code").asText());
304 vserver.setVnfc(vfc);
305 List<Vserver> vServers = vfcs.get(vfc);
306 if (vServers == null) {
307 vServers = new LinkedList<>();
308 vfcs.put(vfc, vServers);
310 vServers.add(vserver);
312 vnf.addVserver(vserver);
315 for (Map.Entry<Vnfc, List<Vserver>> entry : vfcs.entrySet()) {
316 Vnfc vnfc = entry.getKey();
317 List<Vserver> vServers = vfcs.get(vnfc);
318 vnfc.addVservers(vServers);
321 return new InventoryModel(vnf);
324 private CapabilityModel buildCapabilitiesModel(String inputJson) throws IOException, APPCException {
325 logger.info("Entering buildCapabilitiesModel");
327 ObjectMapper objectMapper = new ObjectMapper();
328 JsonNode jsonNode = objectMapper.readTree(inputJson);
329 JsonNode capabilitiesNode = jsonNode.get("capabilities");
330 if (capabilitiesNode == null) {
334 List<String> vnfCapabilities = new ArrayList<>();
335 List<String> vfModuleCapabilities = new ArrayList<>();
336 Map<String, List<String>> vmCapabilities = new HashMap<>();
337 List<String> vnfcCapabilities = new ArrayList<>();
339 JsonNode vnfNode = capabilitiesNode.get(ActionLevel.VNF.getAction());
340 JsonNode vfModuleNode = capabilitiesNode.get(ActionLevel.VF_MODULE.getAction());
341 JsonNode vmNode = capabilitiesNode.get(ActionLevel.VM.getAction());
342 JsonNode vnfcNode = capabilitiesNode.get(ActionLevel.VNFC.getAction());
344 if (vnfNode != null && vnfNode.isArray() ) {
345 for (JsonNode nodes : vnfNode) {
346 vnfCapabilities.add(nodes.asText());
349 if (vfModuleNode != null && vfModuleNode.isArray() ){
350 for (JsonNode nodes : vfModuleNode) {
351 vfModuleCapabilities.add(nodes.asText());
354 if (vmNode != null && vmNode.isArray() ){
355 for (JsonNode jNode : vmNode) {
356 logger.debug("jNode=" + jNode);
357 Iterator<Map.Entry<String,JsonNode>> fldIter = jNode.fields();
358 while (fldIter.hasNext()) {
359 Map.Entry<String,JsonNode> currentEntry = fldIter.next();
360 logger.debug("currentEntry.getKey()=" + currentEntry.getKey());
361 logger.debug("currentEntry.getValue()=" + currentEntry.getValue());
362 if (currentEntry.getValue().isArray()) {
363 logger.debug("currentEntry.getValue().isArray() is true");
364 List<String> ls = new ArrayList<String>();
365 for (JsonNode node: currentEntry.getValue()) {
366 ls.add(node.asText());
368 vmCapabilities.put(currentEntry.getKey(), ls);
373 if (vnfcNode != null && vnfcNode.isArray() ){
374 for (JsonNode nodes : vnfcNode) {
375 vnfcCapabilities.add(nodes.asText());
378 logger.info("vnfCapabilities=" + vnfCapabilities);
379 logger.info("vfModuleCapabilities=" + vfModuleCapabilities);
380 logger.info("vmCapabilities=" + vmCapabilities);
381 logger.info("vnfcCapabilities=" + vnfcCapabilities);
383 return new CapabilityModel(vnfCapabilities, vfModuleCapabilities, vmCapabilities, vnfcCapabilities);