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.sdc.artifacts.helper;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.annotation.JsonInclude;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.MapperFeature;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.ObjectWriter;
33 import org.onap.appc.dg.dependencymanager.helper.DependencyModelParser;
34 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
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 = null;
63 vnfcDependencyModel = dependencyModelParser.generateDependencyModel(tosca, vnfType);
64 } catch (InvalidDependencyModelException e) {
65 logger.error("Error generating dependency model");
66 throw new APPCException(e.getMessage(),e);
69 if (vnfcDependencyModel != null && !vnfcDependencyModel.getDependencies().isEmpty()) {
70 logger.debug(String.format("Dependency Model generated : %s ", vnfcDependencyModel.toString()));
71 List<org.onap.appc.sdc.artifacts.object.Vnfc> vnfcs = new ArrayList<>();
73 for (Node<Vnfc> node : vnfcDependencyModel.getDependencies()) {
74 org.onap.appc.sdc.artifacts.object.Vnfc vnfc = new org.onap.appc.sdc.artifacts.object.Vnfc();
75 vnfc.setVnfcType(node.getChild().getVnfcType());
76 vnfc.setMandatory(node.getChild().isMandatory());
77 vnfc.setResilienceType(node.getChild().getResilienceType());
78 if (node.getParents() != null && !node.getParents().isEmpty()) {
79 List<String> parents = new ArrayList<>();
80 for (Vnfc parentNode : node.getParents()) {
81 parents.add(parentNode.getVnfcType());
83 vnfc.setParents(parents);
87 ObjectMapper objectMapper = new ObjectMapper();
89 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
90 (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer().withRootName("vnfcs");
92 dependencyJson = writer.writeValueAsString(vnfcs);
93 } catch (JsonProcessingException e) {
94 logger.error("Error converting dependency model to JSON");
95 throw new APPCException("Error converting dependency model to JSON",e);
98 logger.error("Error generating dependency model from tosca. Empty dependency model");
99 throw new APPCException("Error generating dependency model from tosca. Empty dependency model");
101 return dependencyJson;