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.onap.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 java.util.HashSet;
 
  31 import java.util.Iterator;
 
  32 import java.util.List;
 
  35 import org.onap.appc.dg.common.VnfExecutionFlow;
 
  36 import org.onap.appc.dg.dependencymanager.DependencyManager;
 
  37 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
 
  38 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
 
  39 import org.onap.appc.dg.flowbuilder.FlowBuilder;
 
  40 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
 
  41 import org.onap.appc.dg.flowbuilder.impl.FlowBuilderFactory;
 
  42 import org.onap.appc.dg.objects.DependencyTypes;
 
  43 import org.onap.appc.dg.objects.FlowStrategies;
 
  44 import org.onap.appc.dg.objects.InventoryModel;
 
  45 import org.onap.appc.dg.objects.Node;
 
  46 import org.onap.appc.dg.objects.VnfcDependencyModel;
 
  47 import org.onap.appc.dg.objects.VnfcFlowModel;
 
  48 import org.onap.appc.domainmodel.Vnf;
 
  49 import org.onap.appc.domainmodel.Vnfc;
 
  50 import org.onap.appc.domainmodel.Vserver;
 
  51 import org.onap.appc.exceptions.APPCException;
 
  52 import org.onap.appc.i18n.Msg;
 
  53 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
 
  54 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  56 public class VnfExecutionFlowImpl implements VnfExecutionFlow {
 
  58     private final EELFLogger logger = EELFManager.getInstance().getLogger(VnfExecutionFlowImpl.class);
 
  59     private static final String VNFC_FLOW = "vnfcFlow[";
 
  60     private static final String VNF_VNFC = "vnf.vnfc[";
 
  63      * Constructor <p>Used through blueprint
 
  65     public VnfExecutionFlowImpl() {
 
  70     public void getVnfExecutionFlowData(Map<String, String> params, SvcLogicContext context) {
 
  71         String dependencyType = params.get(Constants.DEPENDENCY_TYPE);
 
  72         String flowStrategy = params.get(Constants.FLOW_STRATEGY);
 
  73         DependencyModelIdentifier modelIdentifier = readDependencyModelIdentifier(params);
 
  74         VnfcDependencyModel dependencyModel;
 
  76             validateInput(dependencyType, flowStrategy, params);
 
  78             if (logger.isTraceEnabled()) {
 
  79                 logger.trace("Input received from DG Node : dependencyType = " + dependencyType +
 
  80                     " , flowStrategy = " + flowStrategy +
 
  81                     ", DependencyModelIdentifier = " + modelIdentifier.toString());
 
  84             DependencyManager dependencyManager = DependencyModelFactory.createDependencyManager();
 
  85             dependencyModel = dependencyManager.getVnfcDependencyModel(
 
  86                 modelIdentifier, DependencyTypes.findByString(dependencyType));
 
  88         } catch (DependencyModelNotFound e) {
 
  89             String msg = EELFResourceManager
 
  90                 .format(Msg.DEPENDENCY_MODEL_NOT_FOUND, params.get(Constants.VNF_TYPE));
 
  92             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
 
  93             context.setAttribute("dependencyModelFound", "false");
 
  95         } catch (InvalidDependencyModelException e) {
 
  96             String msg = EELFResourceManager
 
  97                 .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE));
 
  99             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
 
 100             throw new VnfExecutionInternalException(e);
 
 101         } catch (APPCException e) {
 
 102             logger.error(e.getMessage());
 
 103             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
 
 104             throw new VnfExecutionInternalException(e);
 
 105         } catch (RuntimeException e) {
 
 106             logger.error(e.getMessage());
 
 107             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
 
 111         context.setAttribute("dependencyModelFound", "true");
 
 112         if (logger.isDebugEnabled()) {
 
 113             logger.debug("Dependency Model = " + dependencyModel);
 
 115         logger.info("Building Inventory Model from DG context");
 
 116         InventoryModel inventoryModel;
 
 118             inventoryModel = readInventoryModel(context);
 
 119         } catch (APPCException e) {
 
 120             logger.error(e.getMessage());
 
 121             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
 
 122             throw new VnfExecutionInternalException(e);
 
 124         if (logger.isDebugEnabled()) {
 
 125             logger.debug("Inventory Model = " + inventoryModel);
 
 126             logger.debug("Validating inventory model with dependency model");
 
 129             validateInventoryModelWithDependencyModel(dependencyModel, inventoryModel);
 
 130         } catch (APPCException e) {
 
 131             logger.error(e.getMessage());
 
 132             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, e.getMessage());
 
 133             throw new VnfExecutionInternalException(e);
 
 135         logger.info("Creating flow builder");
 
 136         FlowBuilder flowBuilder = FlowBuilderFactory.getInstance().getFlowBuilder(
 
 137             FlowStrategies.findByString(flowStrategy));
 
 139         logger.info("Building Vnf flow model");
 
 140         VnfcFlowModel flowModel;
 
 142             flowModel = flowBuilder.buildFlowModel(dependencyModel, inventoryModel);
 
 143         } catch (InvalidDependencyModelException e) {
 
 144             String msg = EELFResourceManager
 
 145                 .format(Msg.INVALID_DEPENDENCY_MODEL, params.get(Constants.VNF_TYPE), e.getMessage());
 
 147             context.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
 
 148             throw new VnfExecutionInternalException(e);
 
 151         // remove VNFCs from the flow model where vserver list is empty
 
 152         reconcileFlowModel(flowModel);
 
 153         populateContext(flowModel, context);
 
 154         if (logger.isDebugEnabled()) {
 
 157         String msg = EELFResourceManager
 
 158             .format(Msg.SUCCESS_EVENT_MESSAGE, "GetVnfExecutionFlowData", "VNF ID " + params.get(Constants.VNF_TYPE));
 
 159         context.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
 
 162     private void validateInput(String dependencyType, String flowStrategy, Map<String, String> params)
 
 163         throws APPCException {
 
 164         DependencyTypes dependencyTypes = DependencyTypes.findByString(dependencyType);
 
 165         if (dependencyTypes == null) {
 
 166             throw new APPCException("Dependency type from the input : " + dependencyType + " is invalid.");
 
 168         FlowStrategies flowStrategies = FlowStrategies.findByString(flowStrategy);
 
 169         if (flowStrategies == null) {
 
 170             throw new APPCException("Flow Strategy from the input : " + flowStrategy + " is invalid.");
 
 172         String vnfType = params.get(Constants.VNF_TYPE);
 
 173         if (nullOrEmpty(vnfType)) {
 
 174             throw new APPCException("Vnf Type is not passed in the input");
 
 176         String vnfVersion = params.get(Constants.VNF_VERION);
 
 177         if (nullOrEmpty(vnfVersion)) {
 
 178             throw new APPCException("Vnf Version not found");
 
 182     private boolean nullOrEmpty(String vnfType) {
 
 183         return vnfType == null || vnfType.isEmpty();
 
 186     private void logContext(SvcLogicContext context) {
 
 187         for (String key : context.getAttributeKeySet()) {
 
 188             logger.debug(key + " = " + context.getAttribute(key) + "\n");
 
 192     private void populateContext(VnfcFlowModel flowModel, SvcLogicContext context) {
 
 194         Iterator<List<Vnfc>> iterator = flowModel.getModelIterator();
 
 195         while (iterator.hasNext()) {
 
 196             for (Vnfc vnfc : iterator.next()) {
 
 197                 context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcName", vnfc.getVnfcName());
 
 198                 context.setAttribute(VNFC_FLOW + flowIndex + "].vnfcType", vnfc.getVnfcType());
 
 199                 context.setAttribute(VNFC_FLOW + flowIndex + "].resilienceType", vnfc.getResilienceType());
 
 201                     .setAttribute(VNFC_FLOW + flowIndex + "].vmCount", Integer.toString(vnfc.getVserverList().size()));
 
 203                 for (Vserver vm : vnfc.getVserverList()) {
 
 204                     context.setAttribute(VNFC_FLOW + flowIndex + "].vm[" + vmIndex + "].url", vm.getUrl());
 
 210         context.setAttribute("vnfcFlowCount", Integer.toString(flowIndex));
 
 213     private InventoryModel readInventoryModel(SvcLogicContext context) throws APPCException {
 
 214         String vnfId = context.getAttribute("input.action-identifiers.vnf-id");
 
 215         String vnfType = context.getAttribute("vnf.type");
 
 216         String vnfVersion = context.getAttribute("vnf.version");
 
 217         String vnfcCountStr = context.getAttribute("vnf.vnfcCount");
 
 218         Integer vnfcCount = Integer.parseInt(vnfcCountStr);
 
 219         Vnf vnf = createVnf(vnfId, vnfType, vnfVersion);
 
 220         for (Integer i = 0; i < vnfcCount; i++) {
 
 221             String vnfcName = context.getAttribute(VNF_VNFC + i + "].name");
 
 222             String vnfcType = context.getAttribute(VNF_VNFC + i + "].type");
 
 223             String vmCountStr = context.getAttribute(VNF_VNFC + i + "].vm_count");
 
 224             if (nullOrEmpty(vnfcType)) {
 
 225                 throw new APPCException("Could not retrieve VNFC Type from DG Context for vnf.vnfc[" + i + "].type");
 
 227             Integer vmCount = Integer.parseInt(vmCountStr);
 
 228             Vnfc vnfc = createVnfc(vnfcName, vnfcType);
 
 229             for (Integer j = 0; j < vmCount; j++) {
 
 230                 String vmURL = context.getAttribute(VNF_VNFC + i + "].vm[" + j + "].url");
 
 231                 Vserver vm = createVserver(vmURL);
 
 237         return new InventoryModel(vnf);
 
 240     private Vserver createVserver(String vmURL) {
 
 241         Vserver vserver = new Vserver();
 
 242         vserver.setUrl(vmURL);
 
 246     private Vnfc createVnfc(String vnfcName, String vnfcType) {
 
 247         Vnfc vnfc = new Vnfc();
 
 248         vnfc.setVnfcName(vnfcName);
 
 249         vnfc.setVnfcType(vnfcType);
 
 253     private Vnf createVnf(String vnfId, String vnfType, String vnfVersion) {
 
 255         vnf.setVnfType(vnfType);
 
 257         vnf.setVnfVersion(vnfVersion);
 
 261     private DependencyModelIdentifier readDependencyModelIdentifier(Map<String, String> params) {
 
 262         String vnfType = params.get(Constants.VNF_TYPE);
 
 263         String catalogVersion = params.get(Constants.VNF_VERION);
 
 264         return new DependencyModelIdentifier(vnfType, catalogVersion);
 
 267     private void validateInventoryModelWithDependencyModel(VnfcDependencyModel dependencyModel,
 
 268         InventoryModel inventoryModel) throws APPCException {
 
 269         Set<String> dependencyModelVnfcSet = new HashSet<>();
 
 270         Set<String> dependencyModelMandatoryVnfcSet = new HashSet<>();
 
 271         Set<String> inventoryModelVnfcsSet = new HashSet<>();
 
 272         for (Node<Vnfc> node : dependencyModel.getDependencies()) {
 
 273             dependencyModelVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
 
 274             if (node.getChild().isMandatory()) {
 
 275                 dependencyModelMandatoryVnfcSet.add(node.getChild().getVnfcType().toLowerCase());
 
 278         for (Vnfc vnfc : inventoryModel.getVnf().getVnfcs()) {
 
 279             inventoryModelVnfcsSet.add(vnfc.getVnfcType().toLowerCase());
 
 281         // if dependency model and inventory model contains same set of VNFCs, validation succeed and hence return
 
 282         if (dependencyModelVnfcSet.equals(inventoryModelVnfcsSet)) {
 
 285         if (inventoryModelVnfcsSet.size() >= dependencyModelVnfcSet.size()) {
 
 286             Set<String> difference = new HashSet<>(inventoryModelVnfcsSet);
 
 287             difference.removeAll(dependencyModelVnfcSet);
 
 288             logger.error("Dependency model is missing following vnfc type(s): " + difference);
 
 289             throw new APPCException("Dependency model is missing following vnfc type(s): " + difference);
 
 291             Set<String> difference = new HashSet<>(dependencyModelMandatoryVnfcSet);
 
 292             difference.removeAll(inventoryModelVnfcsSet);
 
 293             if (!difference.isEmpty()) {
 
 294                 logger.error("Inventory model is missing following mandatory vnfc type(s): " + difference);
 
 295                 throw new APPCException("Inventory model is missing following mandatory vnfc type(s): " + difference);
 
 300     private void reconcileFlowModel(VnfcFlowModel flowModel) {
 
 301         Iterator<List<Vnfc>> flowIterator = flowModel.getModelIterator();
 
 302         while (flowIterator.hasNext()) {
 
 303             Iterator<Vnfc> vnfcIterator = flowIterator.next().iterator();
 
 304             while (vnfcIterator.hasNext()) {
 
 305                 Vnfc vnfc = vnfcIterator.next();
 
 306                 tryRemoveInterator(vnfcIterator, vnfc);
 
 311     private void tryRemoveInterator(Iterator<Vnfc> vnfcIterator, Vnfc vnfc) {
 
 312         if (vnfc.getVserverList().isEmpty()) {
 
 313             if (logger.isDebugEnabled()) {
 
 314                 logger.debug("No vservers present for Vnfc type: " + vnfc.getVnfcType()
 
 315                     + ". Hence, removing it from the flow model.");
 
 317             vnfcIterator.remove();