2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.sdc.artifacts.helper;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.annotation.JsonInclude;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.MapperFeature;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.ObjectWriter;
34 import org.onap.appc.dg.dependencymanager.helper.DependencyModelParser;
35 import org.onap.appc.dg.objects.Node;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.domainmodel.Vnfc;
38 import org.onap.appc.exceptions.APPCException;
40 import java.util.ArrayList;
41 import java.util.List;
44 * Provides method for genrating Dependency JSON from Tosca model
46 public class DependencyModelGenerator {
48 private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelGenerator.class);
52 * @param tosca - tosca string from SDC
53 * @param vnfType - Vnf Type from tosca
54 * @return - Dependency JSON in String format
55 * @throws APPCException is thrown if error occurs
57 public String getDependencyModel(String tosca, String vnfType) throws APPCException {
58 logger.debug(String.format("Generating dependency model for vnfType : %s , TOSCA: %s ", vnfType ,tosca));
59 String dependencyJson;
60 DependencyModelParser dependencyModelParser = new DependencyModelParser();
61 VnfcDependencyModel vnfcDependencyModel = dependencyModelParser.generateDependencyModel(tosca, vnfType);
63 if (vnfcDependencyModel != null && !vnfcDependencyModel.getDependencies().isEmpty()) {
64 logger.debug(String.format("Dependency Model generated : %s ", vnfcDependencyModel.toString()));
65 List<org.onap.appc.sdc.artifacts.object.Vnfc> vnfcs = new ArrayList<>();
67 for (Node<Vnfc> node : vnfcDependencyModel.getDependencies()) {
68 org.onap.appc.sdc.artifacts.object.Vnfc vnfc = new org.onap.appc.sdc.artifacts.object.Vnfc();
69 vnfc.setVnfcType(node.getChild().getVnfcType());
70 vnfc.setMandatory(node.getChild().isMandatory());
71 vnfc.setResilienceType(node.getChild().getResilienceType());
72 if (node.getParents() != null && !node.getParents().isEmpty()) {
73 List<String> parents = new ArrayList<>();
74 for (Vnfc parentNode : node.getParents()) {
75 parents.add(parentNode.getVnfcType());
77 vnfc.setParents(parents);
81 ObjectMapper objectMapper = new ObjectMapper();
83 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
84 (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer().withRootName("vnfcs");
86 dependencyJson = writer.writeValueAsString(vnfcs);
87 } catch (JsonProcessingException e) {
88 logger.error("Error converting dependency model to JSON");
89 throw new APPCException("Error converting dependency model to JSON",e);
92 logger.error("Error generating dependency model from tosca. Empty dependency model");
93 throw new APPCException("Error generating dependency model from tosca. Empty dependency model");
95 return dependencyJson;