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