2 * ============LICENSE_START=======================================================
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
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
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;
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;
46 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
50 public VnfExecutionFlowImpl(){
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;
61 validateInput(dependencyType, flowStrategy, params);
63 if (logger.isTraceEnabled()) {
64 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
65 " , flowStrategy = " + flowStrategy +
66 ", DependencyModelIdentifier = " + modelIdentifier.toString());
69 DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
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());
77 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
78 context.setAttribute("dependencyModelFound","false");
80 } catch (InvalidDependencyModel e){
81 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
83 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
85 }catch (RuntimeException e){
86 logger.error(e.getMessage());
87 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
92 context.setAttribute("dependencyModelFound","true");
93 if(logger.isDebugEnabled()){
94 logger.debug("Dependency Model = " +dependencyModel);
96 logger.info("Building Inventory Model from DG context");
97 InventoryModel inventoryModel = readInventoryModel(context);
98 if(logger.isDebugEnabled()){
99 logger.debug("Inventory Model = " +inventoryModel);
102 if(logger.isDebugEnabled()){
103 logger.debug("Validating inventory model with dependency model");
106 validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
107 }catch (RuntimeException e){
108 logger.error(e.getMessage());
109 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
112 logger.info("Creating flow builder");
113 FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
114 FlowStrategies.findByString(flowStrategy));
116 logger.info("Building Vnf flow model");
117 VnfcFlowModel flowModel = null;
119 flowModel = flowBuilder.buildFlowModel(dependencyModel,inventoryModel);
121 catch (InvalidDependencyModel e){
122 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
124 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
128 // remove VNFCs from the flow model where vserver list is empty
129 reconcileFlowModel(flowModel);
130 populateContext(flowModel,context);
131 if(logger.isDebugEnabled()){
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);
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.");
143 FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
144 if(flowStrategies == null){
145 throw new RuntimeException("Flow Strategy from the input : " + flowStrategy +" is invalid.");
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");
151 String vnfVersion = params.get(Constants.VNF_VERION);
152 if(vnfVersion == null || vnfVersion.length() ==0){
153 throw new RuntimeException("Vnf Version not found");
157 private void logContext(SvcLogicContext context) {
158 for(String key:context.getAttributeKeySet()){
159 logger.debug(key + " = " + context.getAttribute(key) + "\n" );
163 private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
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()));
173 for(Vserver vm :vnfc.getVserverList()){
174 context.setAttribute("vnfcFlow["+flowIndex+"].vm["+vmIndex+"].url",vm.getUrl());
180 context.setAttribute("vnfcFlowCount",Integer.toString(flowIndex));
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);
190 Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
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");
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);
208 return new InventoryModel(vnf);
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);
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>();
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());
229 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
230 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
233 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
234 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
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);
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);
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.");
264 vnfcIterator.remove();