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.flowbuilder.exception.InvalidDependencyModelException;
34 import org.onap.appc.dg.objects.InventoryModel;
35 import org.onap.appc.dg.objects.Node;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.domainmodel.Vnf;
38 import org.onap.appc.domainmodel.Vnfc;
39 import org.onap.appc.domainmodel.Vserver;
40 import org.onap.appc.domainmodel.lcm.VNFOperation;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.seqgen.SequenceGenerator;
43 import org.onap.appc.seqgen.dgplugin.SequenceGeneratorPlugin;
44 import org.onap.appc.seqgen.impl.SequenceGeneratorFactory;
45 import org.onap.appc.seqgen.objects.Constants;
46 import org.onap.appc.seqgen.objects.SequenceGeneratorInput;
47 import org.onap.appc.seqgen.objects.Transaction;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
50 import java.io.IOException;
52 import java.util.List;
53 import java.util.HashSet;
55 import java.util.HashMap;
56 import java.util.LinkedList;
58 public class SequenceGeneratorPluginImpl implements SequenceGeneratorPlugin {
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SequenceGeneratorPluginImpl.class);
63 public void generateSequence(Map<String, String> params, SvcLogicContext context) {
64 ObjectMapper objectMapper = new ObjectMapper();
65 String inputJSON = context.getAttribute("inputJSON");
66 logger.debug("Input to Sequence Generator " + inputJSON);
68 SequenceGeneratorInput sequenceGeneratorInput = buildSequenceGeneratorInput(inputJSON);
69 List<Transaction> sequence = generateSequence(sequenceGeneratorInput);
70 String output = objectMapper.writeValueAsString(sequence);
71 logger.debug("Sequence Generator Output " + output);
73 context.setAttribute("output", output);
74 } catch (Exception e) {
75 logger.error("Error generating sequence", e);
76 context.setAttribute("error-code", "401");
77 context.setAttribute("error-message", "Error generating sequence " + e.getMessage());
81 private SequenceGeneratorInput buildSequenceGeneratorInput(String inputJson) throws IOException, APPCException {
82 ObjectMapper objectMapper = new ObjectMapper();
83 SequenceGeneratorInput sequenceGeneratorInput ;
84 objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
85 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
86 sequenceGeneratorInput = objectMapper.readValue(inputJson, SequenceGeneratorInput.class);
88 InventoryModel inventoryModel = buildInventoryModel(inputJson);
89 sequenceGeneratorInput.setInventoryModel(inventoryModel);
91 VnfcDependencyModel dependencyModel = buildDependencyModel(inputJson);
92 if(dependencyModel!=null){
93 validateInventoryModelWithDependencyModel(dependencyModel,inventoryModel);
95 sequenceGeneratorInput.setDependencyModel(dependencyModel);
97 return sequenceGeneratorInput;
99 private List<Transaction> generateSequence(SequenceGeneratorInput sequenceGeneratorInput) throws APPCException {
100 if (sequenceGeneratorInput.getRequestInfo() == null) {
101 throw new APPCException("Request info is not provided in the input");
103 String action = sequenceGeneratorInput.getRequestInfo().getAction();
104 VNFOperation operation = VNFOperation.findByString(action);
105 if (operation == null) {
106 throw new APPCException("Invalid Action " + action);
108 if(Constants.ActionLevel.findByString(sequenceGeneratorInput.getRequestInfo().getActionLevel().toUpperCase())==null){
109 throw new APPCException("Invalid Action Level " + sequenceGeneratorInput.getRequestInfo().getActionLevel());
111 SequenceGenerator sequenceGenerator = SequenceGeneratorFactory.getInstance().createSequenceGenerator(operation);
112 return sequenceGenerator.generateSequence(sequenceGeneratorInput);
115 private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) throws APPCException {
116 Set<String> dependencyModelVnfcSet = new HashSet<>();
117 Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
118 Set<String> inventoryModelVnfcsSet = new HashSet<>();
120 for (Node<Vnfc> node : dependencyModel.getDependencies()) {
121 dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
122 if (node.getChild().isMandatory()) {
123 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
127 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
128 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
131 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
132 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
136 if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
137 Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
138 difference.removeAll(dependencyModelVnfcSet);
139 logger.error("Dependency model is missing following vnfc type(s): " + difference);
140 throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
142 Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
143 difference.removeAll(inventoryModelVnfcsSet);
144 if (difference.size() > 0) {
145 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
146 throw new APPCException("VMs missing for the mandatory VNFC : " + difference);
151 // Dependency model is an optional attribute and may contain null values
152 private VnfcDependencyModel buildDependencyModel(String inputJson) throws IOException, APPCException {
153 Set<Node<Vnfc>> dependency = new HashSet<>();
154 Set<String> parentVnfcs=new HashSet<>();
155 Set<String> allVnfcTypes=new HashSet<>();
156 ObjectMapper objectMapper = new ObjectMapper();
157 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
158 JsonNode rootNode = objectMapper.readTree(inputJson);
159 JsonNode vnfcs = getVnfcsNode(rootNode);
161 for (JsonNode vnfcNode : vnfcs) {
162 String vnfcType = readVnfcType(vnfcNode);
163 allVnfcTypes.add(vnfcType);
164 String mandatory = readMandatory(vnfcNode);
165 String resilience = readResilience(vnfcNode);
166 Vnfc vnfc = new Vnfc();
167 vnfc.setVnfcType(vnfcType);
168 vnfc.setResilienceType(resilience);
169 vnfc.setMandatory(Boolean.parseBoolean(mandatory));
170 Node<Vnfc> currentNode = getNode(dependency, vnfcType);
171 if (currentNode == null) {
172 currentNode = new Node<>(vnfc);
173 dependency.add(currentNode);
175 currentNode.getChild().setMandatory(Boolean.valueOf(mandatory));
176 currentNode.getChild().setResilienceType(resilience);
178 JsonNode parents = vnfcNode.get("parents");
179 for (JsonNode parent : parents) {
180 String parentVnfcType = parent.asText();
181 parentVnfcs.add(parentVnfcType);
182 Node<Vnfc> parentNode = getNode(dependency, parentVnfcType);
183 if (parentNode != null) {
184 currentNode.addParent(parentNode.getChild());
186 Vnfc parentVnfc=new Vnfc();
187 parentVnfc.setVnfcType(parentVnfcType);
188 parentVnfc.setMandatory(false);
189 parentNode = new Node<>(parentVnfc);
190 currentNode.addParent(parentVnfc);
191 dependency.add(parentNode);
196 for(String parent:parentVnfcs){
197 if(!allVnfcTypes.contains(parent)){
198 throw new APPCException("Dependency model missing vnfc type "+parent);
201 return new VnfcDependencyModel(dependency);
206 private String readResilience(JsonNode vnfcNode) {
207 String resilience = null;
208 if (vnfcNode.get("resilience") != null) {
209 resilience = vnfcNode.get("resilience").asText();
214 private String readMandatory(JsonNode vnfcNode) {
216 JsonNode mandatoryNode = vnfcNode.get("mandatory");
217 if (mandatoryNode == null) {
220 mandatory = mandatoryNode.asText();
225 private String readVnfcType(JsonNode vnfcNode) throws APPCException {
226 JsonNode vnfcTypeNode = vnfcNode.get(Constants.VNFC_TYPE);
227 if (vnfcTypeNode == null) {
228 throw new APPCException("vnfc-type is not available in dependency info");
230 return vnfcTypeNode.asText();
233 private JsonNode getVnfcsNode(JsonNode rootNode) {
234 JsonNode dependencyInfo = rootNode.get("dependency-info");
235 JsonNode vnfcs = null;
236 if (dependencyInfo != null) {
237 vnfcs = dependencyInfo.get("vnfcs");
242 private Node<Vnfc> getNode(Set<Node<Vnfc>> dependency, String vnfcType) {
243 for (Node<Vnfc> node : dependency) {
244 if (node.getChild().getVnfcType().equals(vnfcType)) {
251 private InventoryModel buildInventoryModel(String inputJson) throws IOException, APPCException {
252 ObjectMapper objectMapper = new ObjectMapper();
253 JsonNode jsonNode = objectMapper.readTree(inputJson);
254 JsonNode inventoryInfo = jsonNode.get("inventory-info");
255 if (inventoryInfo == null) {
256 throw new APPCException("inventory-info is not provided in the input");
258 JsonNode vnfInfo = inventoryInfo.get("vnf-info");
259 if (vnfInfo == null) {
260 throw new APPCException("vnf-info is not provided in the input");
263 String vnfId = vnfInfo.get("vnf-id").asText();
264 String vnfType = vnfInfo.get("vnf-type").asText();
267 vnf.setVnfType(vnfType);
268 Map<Vnfc, List<Vserver>> vfcs = new HashMap<>();
269 JsonNode vms = vnfInfo.get("vm");
271 throw new APPCException("vm info not provided in the input");
273 for (JsonNode vm : vms) {
274 if(vm.get("vserver-id")== null){
275 throw new APPCException("vserver-id not found ");
277 String vserverId = vm.get("vserver-id").asText();
278 Vserver vserver = new Vserver();
279 vserver.setId(vserverId);
280 if (vm.get("vnfc")!=null&& vm.get("vnfc").get("vnfc-name") != null && vm.get("vnfc").get("vnfc-type")!= null) {
281 Vnfc vfc = new Vnfc();
282 vfc.setVnfcType(vm.get("vnfc").get("vnfc-type").asText());
283 vfc.setVnfcName(vm.get("vnfc").get("vnfc-name").asText());
284 vserver.setVnfc(vfc);
285 List<Vserver> vServers = vfcs.get(vfc);
286 if (vServers == null) {
287 vServers = new LinkedList<>();
288 vfcs.put(vfc, vServers);
290 vServers.add(vserver);
292 vnf.addVserver(vserver);
295 for (Map.Entry<Vnfc, List<Vserver>> entry : vfcs.entrySet()) {
296 Vnfc vnfc = entry.getKey();
297 List<Vserver> vServers = vfcs.get(vnfc);
298 vnfc.addVservers(vServers);
301 return new InventoryModel(vnf);