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.flowbuilder.exception.InvalidDependencyModelException;
36 import org.onap.appc.dg.objects.Node;
37 import org.onap.appc.dg.objects.VnfcDependencyModel;
38 import org.onap.appc.domainmodel.Vnfc;
39 import org.onap.appc.exceptions.APPCException;
41 import java.util.ArrayList;
42 import java.util.List;
45 * Provides method for genrating Dependency JSON from Tosca model
47 public class DependencyModelGenerator {
49 private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelGenerator.class);
53 * @param tosca - tosca string from SDC
54 * @param vnfType - Vnf Type from tosca
55 * @return - Dependency JSON in String format
56 * @throws APPCException is thrown if error occurs
58 public String getDependencyModel(String tosca, String vnfType) throws APPCException {
59 logger.debug(String.format("Generating dependency model for vnfType : %s , TOSCA: %s ", vnfType ,tosca));
60 String dependencyJson;
61 DependencyModelParser dependencyModelParser = new DependencyModelParser();
62 VnfcDependencyModel vnfcDependencyModel = null;
64 vnfcDependencyModel = dependencyModelParser.generateDependencyModel(tosca, vnfType);
65 } catch (InvalidDependencyModelException e) {
66 logger.error("Error generating dependency model");
67 throw new APPCException(e.getMessage(),e);
70 if (vnfcDependencyModel != null && !vnfcDependencyModel.getDependencies().isEmpty()) {
71 logger.debug(String.format("Dependency Model generated : %s ", vnfcDependencyModel.toString()));
72 List<org.onap.appc.sdc.artifacts.object.Vnfc> vnfcs = new ArrayList<>();
74 for (Node<Vnfc> node : vnfcDependencyModel.getDependencies()) {
75 org.onap.appc.sdc.artifacts.object.Vnfc vnfc = new org.onap.appc.sdc.artifacts.object.Vnfc();
76 vnfc.setVnfcType(node.getChild().getVnfcType());
77 vnfc.setMandatory(node.getChild().isMandatory());
78 vnfc.setResilienceType(node.getChild().getResilienceType());
79 if (node.getParents() != null && !node.getParents().isEmpty()) {
80 List<String> parents = new ArrayList<>();
81 for (Vnfc parentNode : node.getParents()) {
82 parents.add(parentNode.getVnfcType());
84 vnfc.setParents(parents);
88 ObjectMapper objectMapper = new ObjectMapper();
90 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
91 (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer().withRootName("vnfcs");
93 dependencyJson = writer.writeValueAsString(vnfcs);
94 } catch (JsonProcessingException e) {
95 logger.error("Error converting dependency model to JSON");
96 throw new APPCException("Error converting dependency model to JSON",e);
99 logger.error("Error generating dependency model from tosca. Empty dependency model");
100 throw new APPCException("Error generating dependency model from tosca. Empty dependency model");
102 return dependencyJson;