Fix sonar blocker in ChefAdpater
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / artifacts / helper / DependencyModelGenerator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.sdc.artifacts.helper;
26
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.openecomp.appc.dg.dependencymanager.helper.DependencyModelParser;
35 import org.openecomp.appc.dg.objects.Node;
36 import org.openecomp.appc.dg.objects.VnfcDependencyModel;
37 import org.openecomp.appc.domainmodel.Vnfc;
38 import org.openecomp.appc.exceptions.APPCException;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44  * Provides method for genrating Dependency JSON from Tosca model
45  */
46 public class DependencyModelGenerator {
47
48     private final EELFLogger logger = EELFManager.getInstance().getLogger(DependencyModelGenerator.class);
49
50     /**
51      *
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
56      */
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);
62
63         if (vnfcDependencyModel != null && !vnfcDependencyModel.getDependencies().isEmpty()) {
64             logger.debug(String.format("Dependency Model generated : %s ", vnfcDependencyModel.toString()));
65             List<org.openecomp.appc.sdc.artifacts.object.Vnfc> vnfcs = new ArrayList<>();
66
67             for (Node<Vnfc> node : vnfcDependencyModel.getDependencies()) {
68                 org.openecomp.appc.sdc.artifacts.object.Vnfc vnfc = new org.openecomp.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());
76                     }
77                     vnfc.setParents(parents);
78                 }
79                 vnfcs.add(vnfc);
80             }
81             ObjectMapper objectMapper = new ObjectMapper();
82
83             ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
84                     (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer().withRootName("vnfcs");
85             try {
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);
90             }
91         } else {
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");
94         }
95         return  dependencyJson;
96     }
97 }