2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.dg.common.impl;
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;
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;
45 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
49 public VnfExecutionFlowImpl(){
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;
60 validateInput(dependencyType, flowStrategy, params);
62 if (logger.isTraceEnabled()) {
63 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
64 " , flowStrategy = " + flowStrategy +
65 ", DependencyModelIdentifier = " + modelIdentifier.toString());
68 DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
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());
76 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
77 context.setAttribute("dependencyModelFound","false");
79 } catch (InvalidDependencyModel e){
80 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
82 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
84 }catch (RuntimeException e){
85 logger.error(e.getMessage());
86 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
91 context.setAttribute("dependencyModelFound","true");
92 if(logger.isDebugEnabled()){
93 logger.debug("Dependency Model = " +dependencyModel);
95 logger.info("Building Inventory Model from DG context");
96 InventoryModel inventoryModel = readInventoryModel(context);
97 if(logger.isDebugEnabled()){
98 logger.debug("Inventory Model = " +inventoryModel);
101 if(logger.isDebugEnabled()){
102 logger.debug("Validating inventory model with dependency model");
105 validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
106 }catch (RuntimeException e){
107 logger.error(e.getMessage());
108 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
111 logger.info("Creating flow builder");
112 FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
113 FlowStrategies.findByString(flowStrategy));
115 logger.info("Building Vnf flow model");
116 VnfcFlowModel flowModel = null;
118 flowModel = flowBuilder.buildFlowModel(dependencyModel,inventoryModel);
120 catch (InvalidDependencyModel e){
121 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
123 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
127 // remove VNFCs from the flow model where vserver list is empty
128 reconcileFlowModel(flowModel);
129 populateContext(flowModel,context);
130 if(logger.isDebugEnabled()){
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);
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.");
142 FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
143 if(flowStrategies == null){
144 throw new RuntimeException("Flow Strategy from the input : " + flowStrategy +" is invalid.");
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");
150 String vnfVersion = params.get(Constants.VNF_VERION);
151 if(vnfVersion == null || vnfVersion.length() ==0){
152 throw new RuntimeException("Vnf Version not found");
156 private void logContext(SvcLogicContext context) {
157 for(String key:context.getAttributeKeySet()){
158 logger.debug(key + " = " + context.getAttribute(key) + "\n" );
162 private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
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()));
172 for(Vserver vm :vnfc.getVserverList()){
173 context.setAttribute("vnfcFlow["+flowIndex+"].vm["+vmIndex+"].url",vm.getUrl());
179 context.setAttribute("vnfcFlowCount",Integer.toString(flowIndex));
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);
189 Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
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");
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);
207 return new InventoryModel(vnf);
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);
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>();
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());
228 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
229 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
232 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
233 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
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);
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);
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.");
263 vnfcIterator.remove();