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