2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.common.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResourceManager;
30 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import org.openecomp.appc.dg.common.VnfExecutionFlow;
35 import org.openecomp.appc.dg.dependencymanager.DependencyManager;
36 import org.openecomp.appc.dg.dependencymanager.exception.DependencyModelNotFound;
37 import org.openecomp.appc.dg.dependencymanager.impl.DependencyModelFactory;
38 import org.openecomp.appc.dg.flowbuilder.FlowBuilder;
39 import org.openecomp.appc.dg.flowbuilder.exception.InvalidDependencyModel;
40 import org.openecomp.appc.dg.flowbuilder.impl.FlowBuilderFactory;
41 import org.openecomp.appc.dg.objects.*;
42 import org.openecomp.appc.domainmodel.Vnf;
43 import org.openecomp.appc.domainmodel.Vnfc;
44 import org.openecomp.appc.domainmodel.Vserver;
45 import org.openecomp.appc.i18n.Msg;
46 import org.openecomp.appc.metadata.objects.DependencyModelIdentifier;
48 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
50 private static final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
52 public VnfExecutionFlowImpl(){
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;
63 validateInput(dependencyType, flowStrategy, params);
65 if (logger.isTraceEnabled()) {
66 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
67 " , flowStrategy = " + flowStrategy +
68 ", DependencyModelIdentifier = " + modelIdentifier.toString());
71 DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
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());
79 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
80 context.setAttribute("dependencyModelFound","false");
82 } catch (InvalidDependencyModel e){
83 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
85 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
87 }catch (RuntimeException e){
88 logger.error(e.getMessage());
89 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
94 context.setAttribute("dependencyModelFound","true");
95 if(logger.isDebugEnabled()){
96 logger.debug("Dependency Model = " +dependencyModel);
98 logger.info("Building Inventory Model from DG context");
99 InventoryModel inventoryModel = readInventoryModel(context);
100 if(logger.isDebugEnabled()){
101 logger.debug("Inventory Model = " +inventoryModel);
104 if(logger.isDebugEnabled()){
105 logger.debug("Validating inventory model with dependency model");
108 validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
109 }catch (RuntimeException e){
110 logger.error(e.getMessage());
111 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,e.getMessage());
114 logger.info("Creating flow builder");
115 FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
116 FlowStrategies.findByString(flowStrategy));
118 logger.info("Building Vnf flow model");
119 VnfcFlowModel flowModel = null;
121 flowModel = flowBuilder.buildFlowModel(dependencyModel,inventoryModel);
123 catch (InvalidDependencyModel e){
124 String msg = EELFResourceManager.format(Msg.INVALID_DEPENDENCY_MODEL,params.get(Constants.VNF_TYPE), e.getMessage());
126 context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE,msg);
130 // remove VNFCs from the flow model where vserver list is empty
131 reconcileFlowModel(flowModel);
132 populateContext(flowModel,context);
133 if(logger.isDebugEnabled()){
136 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData","VNF ID " + params.get(Constants.VNF_TYPE));
137 context.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
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.");
145 FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
146 if(flowStrategies == null){
147 throw new RuntimeException("Flow Strategy from the input : " + flowStrategy +" is invalid.");
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");
153 String vnfVersion = params.get(Constants.VNF_VERION);
154 if(vnfVersion == null || vnfVersion.length() ==0){
155 throw new RuntimeException("Vnf Version not found");
159 private void logContext(SvcLogicContext context) {
160 for(String key:context.getAttributeKeySet()){
161 logger.debug(key + " = " + context.getAttribute(key) + "\n" );
165 private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
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()));
175 for(Vserver vm :vnfc.getVserverList()){
176 context.setAttribute("vnfcFlow["+flowIndex+"].vm["+vmIndex+"].url",vm.getUrl());
182 context.setAttribute("vnfcFlowCount",Integer.toString(flowIndex));
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);
192 Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
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");
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);
210 return new InventoryModel(vnf);
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);
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>();
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());
231 for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
232 inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
235 // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
236 if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
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);
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);
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.");
266 vnfcIterator.remove();