2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.core.sli.provider;
23 import java.util.HashMap;
24 import java.util.Iterator;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class ConfigureNodeExecutor extends SvcLogicNodeExecutor {
37 private static final Logger LOG = LoggerFactory
38 .getLogger(ConfigureNodeExecutor.class);
40 public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
41 SvcLogicContext ctx) throws SvcLogicException {
43 String adaptorName = SvcLogicExpressionResolver.evaluate(
44 node.getAttribute("adaptor"), node, ctx);
45 String outValue = "failure";
47 if (LOG.isDebugEnabled()) {
48 LOG.debug("configure node encountered - looking for adaptor "
52 SvcLogicAdaptor adaptor = getAdaptor(adaptorName);
54 if (adaptor != null) {
55 String activate = SvcLogicExpressionResolver.evaluate(
56 node.getAttribute("activate"), node, ctx);
57 String key = SvcLogicExpressionResolver.evaluate(
58 node.getAttribute("key"), node, ctx);
60 Map<String, String> parmMap = new HashMap<String, String>();
62 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
64 boolean hasParms = false;
66 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
67 .iterator(); iter.hasNext();) {
69 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
70 String curName = curEnt.getKey();
71 SvcLogicExpression curExpr = curEnt.getValue();
72 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
74 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
76 parmMap.put(curName,curExprValue);
80 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
83 confStatus = adaptor.configure(key, parmMap, ctx);
84 } catch (Exception e) {
85 LOG.warn("Caught exception from "+adaptorName+".configure", e);
86 confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
92 if ((activate != null) && (activate.length() > 0)) {
93 if ("true".equalsIgnoreCase(activate)) {
94 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
97 activateStatus = adaptor.activate(key, ctx);
98 } catch (Exception e) {
100 LOG.warn("Caught exception from "+adaptorName+".activate", e);
101 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
103 switch (activateStatus) {
107 outValue = "already-active";
110 outValue = "not-found";
113 outValue = "not-ready";
117 outValue = "failure";
119 } else if ("false".equalsIgnoreCase(activate)) {
120 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
123 deactivateStatus = adaptor.deactivate(key, ctx);
124 } catch (Exception e) {
126 LOG.warn("Caught exception from "+adaptorName+".deactivate", e);
127 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
129 switch (deactivateStatus) {
133 outValue = "already-active";
136 outValue = "not-found";
139 outValue = "not-ready";
143 outValue = "failure";
149 outValue = "already-active";
152 outValue = "not-found";
155 outValue = "not-ready";
159 outValue = "failure";
162 if ((activate != null) && (activate.length() > 0)) {
163 if ("true".equalsIgnoreCase(activate)) {
164 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
166 activateStatus = adaptor.activate(key, ctx);
167 } catch (Exception e) {
168 LOG.warn("Caught exception from "+adaptorName+".activate", e);
169 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
171 switch (activateStatus) {
173 outValue = "success";
176 outValue = "already-active";
179 outValue = "not-found";
182 outValue = "not-ready";
186 outValue = "failure";
188 } else if ("false".equalsIgnoreCase(activate)) {
189 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
192 deactivateStatus = adaptor.deactivate(key, ctx);
193 } catch (Exception e) {
194 LOG.warn("Caught exception from "+adaptorName+".deactivate", e);
195 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
197 switch (deactivateStatus) {
199 outValue = "success";
202 outValue = "already-active";
205 outValue = "not-found";
208 outValue = "not-ready";
212 outValue = "failure";
216 LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
217 outValue = "success";
221 if (LOG.isWarnEnabled()) {
222 LOG.warn("Adaptor for " + adaptorName + " not found");
226 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
227 if (nextNode != null) {
228 if (LOG.isDebugEnabled()) {
229 LOG.debug("about to execute " + outValue + " branch");
234 nextNode = node.getOutcomeValue("Other");
235 if (nextNode != null) {
236 if (LOG.isDebugEnabled()) {
237 LOG.debug("about to execute Other branch");
240 if (LOG.isDebugEnabled()) {
241 LOG.debug("no " + outValue + " or Other branch found");