Updating licenses in all files
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / VnfExecutionFlowImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.common.impl;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.att.eelf.i18n.EELFResourceManager;
28 import org.openecomp.sdnc.sli.SvcLogicContext;
29
30 import java.util.*;
31
32 import org.openecomp.appc.dg.common.VnfExecutionFlow;
33 import org.openecomp.appc.dg.dependencymanager.DependencyManager;
34 import org.openecomp.appc.dg.dependencymanager.exception.DependencyModelNotFound;
35 import org.openecomp.appc.dg.dependencymanager.impl.DependencyModelFactory;
36 import org.openecomp.appc.dg.flowbuilder.FlowBuilder;
37 import org.openecomp.appc.dg.flowbuilder.exception.InvalidDependencyModel;
38 import org.openecomp.appc.dg.flowbuilder.impl.FlowBuilderFactory;
39 import org.openecomp.appc.dg.objects.*;
40 import org.openecomp.appc.domainmodel.Vnf;
41 import org.openecomp.appc.domainmodel.Vnfc;
42 import org.openecomp.appc.domainmodel.Vserver;
43 import org.openecomp.appc.i18n.Msg;
44 import org.openecomp.appc.metadata.objects.DependencyModelIdentifier;
45
46 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
47
48     private static final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
49
50     public VnfExecutionFlowImpl(){
51
52     }
53
54     @Override
55     public void getVnfExecutionFlowData(Map<String, String> params, SvcLogicContext context) {
56         String dependencyType = params.get(Constants.DEPENDENCY_TYPE);
57         String flowStrategy = params.get(Constants.FLOW_STRATEGY);
58         DependencyModelIdentifier modelIdentifier = readDependencyModelIdentifier(params);
59         VnfcDependencyModel dependencyModel = null;
60         try {
61             validateInput(dependencyType, flowStrategy, params);
62
63             if (logger.isTraceEnabled()) {
64                 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
65                         " , flowStrategy = " + flowStrategy +
66                         ", DependencyModelIdentifier = " + modelIdentifier.toString());
67             }
68
69             DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
70
71
72             dependencyModel = dependencyManager.getVnfcDependencyModel(
73                     modelIdentifier, DependencyTypes.findByString(dependencyType));
74         } catch (DependencyModelNotFound e) {
75             String msg = EELFResourceManager.format(Msg.DEPENDENCY_MODEL_NOT_FOUND,params.get(Constants.VNF_TYPE), e.getMessage());
76             logger.error(msg);
77             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
78             context.setAttribute("dependencyModelFound","false");
79             return;
80         } catch (InvalidDependencyModel e){
81             String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
82             logger.error(msg);
83             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
84             throw e;
85         }catch (RuntimeException e){
86             logger.error(e.getMessage());
87             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
88             throw e;
89         }
90
91
92         context.setAttribute("dependencyModelFound","true");
93         if(logger.isDebugEnabled()){
94             logger.debug("Dependency Model = " +dependencyModel);
95         }
96         logger.info("Building Inventory Model from DG context");
97         InventoryModel inventoryModel = readInventoryModel(context);
98         if(logger.isDebugEnabled()){
99             logger.debug("Inventory Model = " +inventoryModel);
100         }
101
102         if(logger.isDebugEnabled()){
103             logger.debug("Validating inventory model with dependency model");
104         }
105         try {
106             validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
107         }catch (RuntimeException e){
108             logger.error(e.getMessage());
109             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
110             throw e;
111         }
112         logger.info("Creating flow builder");
113         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
114                 FlowStrategies.findByString(flowStrategy));
115
116         logger.info("Building Vnf flow model");
117         VnfcFlowModel flowModel = null;
118         try{
119             flowModel = flowBuilder.buildFlowModel(dependencyModel,inventoryModel);
120         }
121         catch (InvalidDependencyModel e){
122             String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
123             logger.error(msg);
124             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
125             throw e;
126         }
127
128         // remove VNFCs from the flow model where vserver list is empty
129         reconcileFlowModel(flowModel);
130         populateContext(flowModel,context);
131         if(logger.isDebugEnabled()){
132             logContext(context);
133         }
134         String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData","VNF ID " + params.get(Constants.VNF_TYPE));
135         context.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
136     }
137
138     private void validateInput(String dependencyType, String flowStrategy, Map<String, String> params) {
139         DependencyTypes dependencyTypes = DependencyTypes.findByString(dependencyType);
140         if(dependencyTypes == null){
141             throw new RuntimeException("Dependency type from the input : " + dependencyType +" is invalid.");
142         }
143         FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
144         if(flowStrategies == null){
145             throw new RuntimeException("Flow Strategy from the input : " + flowStrategy +" is invalid.");
146         }
147         String vnfType = params.get(Constants.VNF_TYPE);
148         if(vnfType ==null || vnfType.length() ==0){
149             throw new RuntimeException("Vnf Type is not passed in the input");
150         }
151         String vnfVersion = params.get(Constants.VNF_VERION);
152         if(vnfVersion == null || vnfVersion.length() ==0){
153             throw new RuntimeException("Vnf Version not found");
154         }
155     }
156
157     private void logContext(SvcLogicContext context) {
158         for(String key:context.getAttributeKeySet()){
159             logger.debug(key + " = " + context.getAttribute(key) + "\n" );
160         }
161     }
162
163     private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
164         int flowIndex=0;
165         Iterator<List<Vnfc>> iterator = flowModel.getModelIterator();
166         while (iterator.hasNext()){
167             for(Vnfc vnfc:iterator.next()){
168                 context.setAttribute("vnfcFlow["+flowIndex+"].vnfcName",vnfc.getVnfcName());
169                 context.setAttribute("vnfcFlow["+flowIndex+"].vnfcType",vnfc.getVnfcType());
170                 context.setAttribute("vnfcFlow["+flowIndex+"].resilienceType",vnfc.getResilienceType());
171                 context.setAttribute("vnfcFlow["+flowIndex+"].vmCount",Integer.toString(vnfc.getVserverList().size()));
172                 int vmIndex =0;
173                 for(Vserver vm :vnfc.getVserverList()){
174                     context.setAttribute("vnfcFlow["+flowIndex+"].vm["+vmIndex+"].url",vm.getUrl());
175                     vmIndex++;
176                 }
177                 flowIndex++;
178             }
179         }
180         context.setAttribute("vnfcFlowCount",Integer.toString(flowIndex));
181     }
182
183     private InventoryModel readInventoryModel(SvcLogicContext context) {
184         String vnfId = context.getAttribute("input.action-identifiers.vnf-id");
185         String vnfType = context.getAttribute("vnf.type");
186         String vnfVersion = context.getAttribute("vnf.version");
187         String vnfcCountStr = context.getAttribute("vnf.vnfcCount");
188         Integer vnfcCount = Integer.parseInt(vnfcCountStr);
189
190         Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
191
192         for(Integer i=0;i<vnfcCount;i++){
193             String vnfcName = context.getAttribute("vnf.vnfc["+ i+"].name");
194             String vnfcType = context.getAttribute("vnf.vnfc["+ i+"].type");
195             String vmCountStr = context.getAttribute("vnf.vnfc["+ i+"].vm_count");
196             if(vnfcType ==null || vnfcType.length() ==0){
197                 throw new RuntimeException("Could not retrieve VNFC Type from DG Context for vnf.vnfc["+ i+"].type");
198             }
199             Integer vmCount = Integer.parseInt(vmCountStr);
200             Vnfc vnfc = new Vnfc(vnfcType,null,vnfcName);
201             for(Integer j=0;j<vmCount;j++){
202                 String vmURL = context.getAttribute("vnf.vnfc["+i+"].vm["+j+"].url");
203                 Vserver vm = new Vserver(vmURL);
204                 vnfc.addVm(vm);
205             }
206             vnf.addVnfc(vnfc);
207         }
208         return new InventoryModel(vnf);
209     }
210
211     private DependencyModelIdentifier readDependencyModelIdentifier(Map<String, String> params) {
212         String vnfType = params.get(Constants.VNF_TYPE);
213         String catalogVersion = params.get(Constants.VNF_VERION);
214         return new DependencyModelIdentifier(vnfType,catalogVersion);
215     }
216
217     private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel, InventoryModel inventoryModel) {
218         Set<String> dependencyModelVnfcSet = new HashSet<String>();
219         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<String>();
220         Set<String> inventoryModelVnfcsSet = new HashSet<String>();
221
222         for (Node<Vnfc> node : dependencyModel.getDependencies()) {
223             dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
224             if (node.getChild().isMandatory()) {
225                 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
226             }
227         }
228
229         for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
230             inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
231         }
232
233         // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
234         if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
235             return;
236         }
237
238         if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
239             Set<String> difference = new HashSet<String>(inventoryModelVnfcsSet);
240             difference.removeAll(dependencyModelVnfcSet);
241             logger.error("Dependency model is missing following vnfc type(s): " + difference);
242             throw new RuntimeException("Dependency model is missing following vnfc type(s): " + difference);
243         } else {
244             Set<String> difference = new HashSet<String>(dependencyModelVnfcSet);
245             difference.removeAll(inventoryModelVnfcsSet);
246             difference.retainAll(dependencyModelMandatoryVnfcSet);
247             if (difference.size() > 0) {
248                 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
249                 throw new RuntimeException("Inventory model is missing following mandatory vnfc type(s): " + difference);
250             }
251         }
252     }
253
254     private void reconcileFlowModel(VnfcFlowModel flowModel) {
255         Iterator<List<Vnfc>> flowIterator = flowModel.getModelIterator();
256         while (flowIterator.hasNext()) {
257             Iterator<Vnfc> vnfcIterator = flowIterator.next().iterator();
258             while (vnfcIterator.hasNext()) {
259                 Vnfc vnfc = vnfcIterator.next();
260                 if (vnfc.getVserverList().size() == 0) {
261                     if (logger.isDebugEnabled()) {
262                         logger.debug("No vservers present for Vnfc type: " + vnfc.getVnfcType() + ". Hence, removing it from the flow model.");
263                     }
264                     vnfcIterator.remove();
265                 }
266             }
267         }
268     }
269 }