Changed to unmaintained
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / appc / dg / common / impl / VnfExecutionFlowImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.common.impl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.eelf.i18n.EELFResourceManager;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import org.onap.appc.dg.common.VnfExecutionFlow;
35 import org.onap.appc.dg.dependencymanager.DependencyManager;
36 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
37 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
38 import org.onap.appc.dg.flowbuilder.FlowBuilder;
39 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
40 import org.onap.appc.dg.flowbuilder.impl.FlowBuilderFactory;
41 import org.onap.appc.dg.objects.DependencyTypes;
42 import org.onap.appc.dg.objects.FlowStrategies;
43 import org.onap.appc.dg.objects.InventoryModel;
44 import org.onap.appc.dg.objects.Node;
45 import org.onap.appc.dg.objects.VnfcDependencyModel;
46 import org.onap.appc.dg.objects.VnfcFlowModel;
47 import org.onap.appc.domainmodel.Vnf;
48 import org.onap.appc.domainmodel.Vnfc;
49 import org.onap.appc.domainmodel.Vserver;
50 import org.onap.appc.exceptions.APPCException;
51 import org.onap.appc.i18n.Msg;
52 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
53 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
54
55 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
56
57     private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
58     private static final String VNFC_FLOW = "vnfcFlow[";
59     private static final String VNF_VNFC = "vnf.vnfc[";
60
61     /**
62      * Constructor <p>Used through blueprint
63      */
64     public VnfExecutionFlowImpl() {
65         // do nothing
66     }
67
68     @Override
69     public void getVnfExecutionFlowData(Map<String, String> params, SvcLogicContext context) {
70         String dependencyType = params.get(Constants.DEPENDENCY_TYPE);
71         String flowStrategy = params.get(Constants.FLOW_STRATEGY);
72         DependencyModelIdentifier modelIdentifier = readDependencyModelIdentifier(params);
73         VnfcDependencyModel dependencyModel;
74         try {
75             validateInput(dependencyType, flowStrategy, params);
76
77             if (logger.isTraceEnabled()) {
78                 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
79                     " , flowStrategy = " + flowStrategy +
80                     ", DependencyModelIdentifier = " + modelIdentifier.toString());
81             }
82
83             DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
84             dependencyModel = dependencyManager.getVnfcDependencyModel(
85                 modelIdentifier, DependencyTypes.findByString(dependencyType));
86
87         } catch (DependencyModelNotFound e) {
88             String msg = EELFResourceManager
89                 .format(Msg.DEPENDENCY_MODEL_NOT_FOUND, params.get(Constants.VNF_TYPE));
90             logger.error(msg, e);
91             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
92             context.setAttribute("dependencyModelFound", "false");
93             return;
94         } catch (InvalidDependencyModelException e) {
95             String msg = EELFResourceManager
96                 .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE));
97             logger.error(msg, e);
98             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
99             throw new VnfExecutionInternalException(e);
100         } catch (APPCException e) {
101             logger.error(e.getMessage());
102             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
103             throw new VnfExecutionInternalException(e);
104         } catch (RuntimeException e) {
105             logger.error(e.getMessage());
106             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
107             throw e;
108         }
109
110         context.setAttribute("dependencyModelFound", "true");
111         if (logger.isDebugEnabled()) {
112             logger.debug("Dependency Model = " + dependencyModel);
113         }
114         logger.info("Building Inventory Model from DG context");
115         InventoryModel inventoryModel;
116         try {
117             inventoryModel = readInventoryModel(context);
118         } catch (APPCException e) {
119             logger.error(e.getMessage());
120             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
121             throw new VnfExecutionInternalException(e);
122         }
123         if (logger.isDebugEnabled()) {
124             logger.debug("Inventory Model = " + inventoryModel);
125             logger.debug("Validating inventory model with dependency model");
126         }
127         try {
128             validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
129         } catch (APPCException e) {
130             logger.error(e.getMessage());
131             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
132             throw new VnfExecutionInternalException(e);
133         }
134         logger.info("Creating flow builder");
135         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
136             FlowStrategies.findByString(flowStrategy));
137
138         logger.info("Building Vnf flow model");
139         VnfcFlowModel flowModel;
140         try {
141             flowModel = flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
142         } catch (InvalidDependencyModelException e) {
143             String msg = EELFResourceManager
144                 .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE), e.getMessage());
145             logger.error(msg);
146             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
147             throw new VnfExecutionInternalException(e);
148         }
149
150         // remove VNFCs from the flow model where vserver list is empty
151         reconcileFlowModel(flowModel);
152         populateContext(flowModel, context);
153         if (logger.isDebugEnabled()) {
154             logContext(context);
155         }
156         String msg = EELFResourceManager
157             .format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData", "VNF ID " + params.get(Constants.VNF_TYPE));
158         context.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
159     }
160
161     private void validateInput(String dependencyType, String flowStrategy, Map<String, String> params)
162         throws APPCException {
163         DependencyTypes dependencyTypes = DependencyTypes.findByString(dependencyType);
164         if (dependencyTypes == null) {
165             throw new APPCException("Dependency type from the input : " + dependencyType + " is invalid.");
166         }
167         FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
168         if (flowStrategies == null) {
169             throw new APPCException("Flow Strategy from the input : " + flowStrategy + " is invalid.");
170         }
171         String vnfType = params.get(Constants.VNF_TYPE);
172         if (nullOrEmpty(vnfType)) {
173             throw new APPCException("Vnf Type is not passed in the input");
174         }
175         String vnfVersion = params.get(Constants.VNF_VERION);
176         if (nullOrEmpty(vnfVersion)) {
177             throw new APPCException("Vnf Version not found");
178         }
179     }
180
181     private boolean nullOrEmpty(String vnfType) {
182         return vnfType == null || vnfType.isEmpty();
183     }
184
185     private void logContext(SvcLogicContext context) {
186         for (String key : context.getAttributeKeySet()) {
187             logger.debug(key + " = " + context.getAttribute(key) + "\n");
188         }
189     }
190
191     private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
192         int flowIndex = 0;
193         Iterator<List<Vnfc>> iterator = flowModel.getModelIterator();
194         while (iterator.hasNext()) {
195             for (Vnfc vnfc : iterator.next()) {
196                 context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcName", vnfc.getVnfcName());
197                 context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcType", vnfc.getVnfcType());
198                 context.setAttribute(VNFC_FLOW + flowIndex + "].resilienceType", vnfc.getResilienceType());
199                 context
200                     .setAttribute(VNFC_FLOW + flowIndex + "].vmCount", Integer.toString(vnfc.getVserverList().size()));
201                 int vmIndex = 0;
202                 for (Vserver vm : vnfc.getVserverList()) {
203                     context.setAttribute(VNFC_FLOW + flowIndex + "].vm[" + vmIndex + "].url", vm.getUrl());
204                     vmIndex++;
205                 }
206                 flowIndex++;
207             }
208         }
209         context.setAttribute("vnfcFlowCount", Integer.toString(flowIndex));
210     }
211
212     private InventoryModel readInventoryModel(SvcLogicContext context) throws APPCException {
213         String vnfId = context.getAttribute("input.action-identifiers.vnf-id");
214         String vnfType = context.getAttribute("vnf.type");
215         String vnfVersion = context.getAttribute("vnf.version");
216         String vnfcCountStr = context.getAttribute("vnf.vnfcCount");
217         Integer vnfcCount = Integer.parseInt(vnfcCountStr);
218         Vnf vnf = createVnf(vnfId, vnfType, vnfVersion);
219         for (Integer i = 0; i < vnfcCount; i++) {
220             String vnfcName = context.getAttribute(VNF_VNFC + i + "].name");
221             String vnfcType = context.getAttribute(VNF_VNFC + i + "].type");
222             String vmCountStr = context.getAttribute(VNF_VNFC + i + "].vm_count");
223             if (nullOrEmpty(vnfcType)) {
224                 throw new APPCException("Could not retrieve VNFC Type from DG Context for vnf.vnfc[" + i + "].type");
225             }
226             Integer vmCount = Integer.parseInt(vmCountStr);
227             Vnfc vnfc = createVnfc(vnfcName, vnfcType);
228             for (Integer j = 0; j < vmCount; j++) {
229                 String vmURL = context.getAttribute(VNF_VNFC + i + "].vm[" + j + "].url");
230                 Vserver vm = createVserver(vmURL);
231                 vm.setVnfc(vnfc);
232                 vnfc.addVserver(vm);
233                 vnf.addVserver(vm);
234             }
235         }
236         return new InventoryModel(vnf);
237     }
238
239     private Vserver createVserver(String vmURL) {
240         Vserver vserver = new Vserver();
241         vserver.setUrl(vmURL);
242         return vserver;
243     }
244
245     private Vnfc createVnfc(String vnfcName, String vnfcType) {
246         Vnfc vnfc = new Vnfc();
247         vnfc.setVnfcName(vnfcName);
248         vnfc.setVnfcType(vnfcType);
249         return vnfc;
250     }
251
252     private Vnf createVnf(String vnfId, String vnfType, String vnfVersion) {
253         Vnf vnf = new Vnf();
254         vnf.setVnfType(vnfType);
255         vnf.setVnfId(vnfId);
256         vnf.setVnfVersion(vnfVersion);
257         return vnf;
258     }
259
260     private DependencyModelIdentifier readDependencyModelIdentifier(Map<String, String> params) {
261         String vnfType = params.get(Constants.VNF_TYPE);
262         String catalogVersion = params.get(Constants.VNF_VERION);
263         return new DependencyModelIdentifier(vnfType, catalogVersion);
264     }
265
266     private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel,
267         InventoryModel inventoryModel) throws APPCException {
268         Set<String> dependencyModelVnfcSet = new HashSet<>();
269         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
270         Set<String> inventoryModelVnfcsSet = new HashSet<>();
271         for (Node<Vnfc> node : dependencyModel.getDependencies()) {
272             dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
273             if (node.getChild().isMandatory()) {
274                 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
275             }
276         }
277         for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
278             inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
279         }
280         // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
281         if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
282             return;
283         }
284         if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
285             Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
286             difference.removeAll(dependencyModelVnfcSet);
287             logger.error("Dependency model is missing following vnfc type(s): " + difference);
288             throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
289         } else {
290             Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
291             difference.removeAll(inventoryModelVnfcsSet);
292             if (!difference.isEmpty()) {
293                 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
294                 throw new APPCException("Inventory model is missing following mandatory vnfc type(s): " + difference);
295             }
296         }
297     }
298
299     private void reconcileFlowModel(VnfcFlowModel flowModel) {
300         Iterator<List<Vnfc>> flowIterator = flowModel.getModelIterator();
301         while (flowIterator.hasNext()) {
302             Iterator<Vnfc> vnfcIterator = flowIterator.next().iterator();
303             while (vnfcIterator.hasNext()) {
304                 Vnfc vnfc = vnfcIterator.next();
305                 tryRemoveInterator(vnfcIterator, vnfc);
306             }
307         }
308     }
309
310     private void tryRemoveInterator(Iterator<Vnfc> vnfcIterator, Vnfc vnfc) {
311         if (vnfc.getVserverList().isEmpty()) {
312             if (logger.isDebugEnabled()) {
313                 logger.debug("No vservers present for Vnfc type: " + vnfc.getVnfcType()
314                     + ". Hence, removing it from the flow model.");
315             }
316             vnfcIterator.remove();
317         }
318     }
319 }