2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Modifications Copyright (C) 2018 IBM.
 
   9  * ================================================================================
 
  10  * Licensed under the Apache License, Version 2.0 (the "License");
 
  11  * you may not use this file except in compliance with the License.
 
  12  * You may obtain a copy of the License at
 
  14  *      http://www.apache.org/licenses/LICENSE-2.0
 
  16  * Unless required by applicable law or agreed to in writing, software
 
  17  * distributed under the License is distributed on an "AS IS" BASIS,
 
  18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  19  * See the License for the specific language governing permissions and
 
  20  * limitations under the License.
 
  21  * ============LICENSE_END=========================================================
 
  24 package org.onap.ccsdk.sli.core.sli.provider.base;
 
  26 import java.util.HashMap;
 
  27 import java.util.Iterator;
 
  31 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
 
  32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  34 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
 
  35 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
 
  36 import org.slf4j.Logger;
 
  37 import org.slf4j.LoggerFactory;
 
  39 public class ConfigureNodeExecutor extends AbstractSvcLogicNodeExecutor {
 
  40         private static final Logger LOG = LoggerFactory
 
  41                         .getLogger(ConfigureNodeExecutor.class);
 
  42         private static final String CAUGHT_EXCEPTION_MSG="Caught exception from ";
 
  43         private static final String ALREADY_ACTIVE= "already-active";
 
  44         private static final String NOT_FOUND= "not-found";
 
  46         public SvcLogicNode execute(SvcLogicServiceBase svc, SvcLogicNode node,
 
  47                         SvcLogicContext ctx) throws SvcLogicException {
 
  49                 String adaptorName = SvcLogicExpressionResolver.evaluate(
 
  50                                 node.getAttribute("adaptor"), node, ctx);
 
  51                 String outValue = "failure";
 
  53                 if (LOG.isDebugEnabled()) {
 
  54                         LOG.debug("configure node encountered - looking for adaptor "
 
  58                 SvcLogicAdaptor adaptor = getAdaptor(adaptorName);
 
  60                 if (adaptor != null) {
 
  61                         String activate = SvcLogicExpressionResolver.evaluate(
 
  62                                         node.getAttribute("activate"), node, ctx);
 
  63                         String key = SvcLogicExpressionResolver.evaluate(
 
  64                                         node.getAttribute("key"), node, ctx);
 
  66                         Map<String, String> parmMap = new HashMap<>();
 
  68                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
 
  70                         boolean hasParms = false;
 
  72                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
 
  73                                         .iterator(); iter.hasNext();) {
 
  75                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
 
  76                                 String curName = curEnt.getKey();
 
  77                                 SvcLogicExpression curExpr = curEnt.getValue();
 
  78                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
 
  80                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
 
  82                                 parmMap.put(curName,curExprValue);
 
  86                                 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
  89                                         confStatus = adaptor.configure(key, parmMap, ctx);
 
  90                                 } catch (Exception e) {
 
  91                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".configure", e);
 
  92                                         confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
  98                                         if ((activate != null) && (activate.length() > 0)) {
 
  99                                                 if ("true".equalsIgnoreCase(activate)) {
 
 100                                                         SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 103                                                                 activateStatus = adaptor.activate(key, ctx);
 
 104                                                         } catch (Exception e) {
 
 106                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
 
 107                                                                 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 109                                                         switch (activateStatus) {
 
 113                                                                 outValue = ALREADY_ACTIVE;
 
 116                                                                 outValue = NOT_FOUND;
 
 119                                                                 outValue = "not-ready";
 
 123                                                                 outValue = "failure";
 
 125                                                 } else if ("false".equalsIgnoreCase(activate)) {
 
 126                                                         SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 129                                                                 deactivateStatus = adaptor.deactivate(key, ctx);
 
 130                                                         } catch (Exception e) {
 
 132                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
 
 133                                                                 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 135                                                         switch (deactivateStatus) {
 
 139                                                                 outValue = ALREADY_ACTIVE;
 
 142                                                                 outValue = NOT_FOUND;
 
 145                                                                 outValue = "not-ready";
 
 149                                                                 outValue = "failure";
 
 155                                         outValue = ALREADY_ACTIVE;
 
 158                                         outValue = NOT_FOUND;
 
 161                                         outValue = "not-ready";
 
 165                                         outValue = "failure";
 
 168                                 if ((activate != null) && (activate.length() > 0)) {
 
 169                                         if ("true".equalsIgnoreCase(activate)) {
 
 170                                                 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 172                                                         activateStatus = adaptor.activate(key, ctx);
 
 173                                                 } catch (Exception e) {
 
 174                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
 
 175                                                         activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 177                                                 switch (activateStatus) {
 
 179                                                         outValue = "success";
 
 182                                                         outValue = ALREADY_ACTIVE;
 
 185                                                         outValue = NOT_FOUND;
 
 188                                                         outValue = "not-ready";
 
 192                                                         outValue = "failure";
 
 194                                         } else if ("false".equalsIgnoreCase(activate)) {
 
 195                                                 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 198                                                         deactivateStatus = adaptor.deactivate(key, ctx);
 
 199                                                 } catch (Exception e) {
 
 200                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
 
 201                                                         deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
 
 203                                                 switch (deactivateStatus) {
 
 205                                                         outValue = "success";
 
 208                                                         outValue = ALREADY_ACTIVE;
 
 211                                                         outValue = NOT_FOUND;
 
 214                                                         outValue = "not-ready";
 
 218                                                         outValue = "failure";
 
 222                                         LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
 
 223                                         outValue = "success";
 
 227                         if (LOG.isWarnEnabled()) {
 
 228                                 LOG.warn("Adaptor for " + adaptorName + " not found");
 
 232                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
 
 233                 if (nextNode != null) {
 
 234                         if (LOG.isDebugEnabled()) {
 
 235                                 LOG.debug("about to execute " + outValue + " branch");
 
 240                 nextNode = node.getOutcomeValue("Other");
 
 241                 if (nextNode != null) {
 
 242                         if (LOG.isDebugEnabled()) {
 
 243                                 LOG.debug("about to execute Other branch");
 
 246                         if (LOG.isDebugEnabled()) {
 
 247                                 LOG.debug("no " + outValue + " or Other branch found");