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 ";
44 public SvcLogicNode execute(SvcLogicServiceBase svc, SvcLogicNode node,
45 SvcLogicContext ctx) throws SvcLogicException {
47 String adaptorName = SvcLogicExpressionResolver.evaluate(
48 node.getAttribute("adaptor"), node, ctx);
49 String outValue = "failure";
51 if (LOG.isDebugEnabled()) {
52 LOG.debug("configure node encountered - looking for adaptor "
56 SvcLogicAdaptor adaptor = getAdaptor(adaptorName);
58 if (adaptor != null) {
59 String activate = SvcLogicExpressionResolver.evaluate(
60 node.getAttribute("activate"), node, ctx);
61 String key = SvcLogicExpressionResolver.evaluate(
62 node.getAttribute("key"), node, ctx);
64 Map<String, String> parmMap = new HashMap<>();
66 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
68 boolean hasParms = false;
70 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
71 .iterator(); iter.hasNext();) {
73 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
74 String curName = curEnt.getKey();
75 SvcLogicExpression curExpr = curEnt.getValue();
76 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
78 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
80 parmMap.put(curName,curExprValue);
84 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
87 confStatus = adaptor.configure(key, parmMap, ctx);
88 } catch (Exception e) {
89 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".configure", e);
90 confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
96 if ((activate != null) && (activate.length() > 0)) {
97 if ("true".equalsIgnoreCase(activate)) {
98 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
101 activateStatus = adaptor.activate(key, ctx);
102 } catch (Exception e) {
104 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
105 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
107 switch (activateStatus) {
111 outValue = "already-active";
114 outValue = "not-found";
117 outValue = "not-ready";
121 outValue = "failure";
123 } else if ("false".equalsIgnoreCase(activate)) {
124 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
127 deactivateStatus = adaptor.deactivate(key, ctx);
128 } catch (Exception e) {
130 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
131 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
133 switch (deactivateStatus) {
137 outValue = "already-active";
140 outValue = "not-found";
143 outValue = "not-ready";
147 outValue = "failure";
153 outValue = "already-active";
156 outValue = "not-found";
159 outValue = "not-ready";
163 outValue = "failure";
166 if ((activate != null) && (activate.length() > 0)) {
167 if ("true".equalsIgnoreCase(activate)) {
168 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
170 activateStatus = adaptor.activate(key, ctx);
171 } catch (Exception e) {
172 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
173 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
175 switch (activateStatus) {
177 outValue = "success";
180 outValue = "already-active";
183 outValue = "not-found";
186 outValue = "not-ready";
190 outValue = "failure";
192 } else if ("false".equalsIgnoreCase(activate)) {
193 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
196 deactivateStatus = adaptor.deactivate(key, ctx);
197 } catch (Exception e) {
198 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
199 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
201 switch (deactivateStatus) {
203 outValue = "success";
206 outValue = "already-active";
209 outValue = "not-found";
212 outValue = "not-ready";
216 outValue = "failure";
220 LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
221 outValue = "success";
225 if (LOG.isWarnEnabled()) {
226 LOG.warn("Adaptor for " + adaptorName + " not found");
230 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
231 if (nextNode != null) {
232 if (LOG.isDebugEnabled()) {
233 LOG.debug("about to execute " + outValue + " branch");
238 nextNode = node.getOutcomeValue("Other");
239 if (nextNode != null) {
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("about to execute Other branch");
244 if (LOG.isDebugEnabled()) {
245 LOG.debug("no " + outValue + " or Other branch found");