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;
25 import java.util.LinkedList;
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.SvcLogicExpressionFactory;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class SetNodeExecutor extends SvcLogicNodeExecutor {
39 private static final Logger LOG = LoggerFactory
40 .getLogger(SetNodeExecutor.class);
43 public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
44 SvcLogicContext ctx) throws SvcLogicException {
46 String ifunsetStr = SvcLogicExpressionResolver.evaluate(
47 node.getAttribute("only-if-unset"), node, ctx);
49 boolean ifunset = "true".equalsIgnoreCase(ifunsetStr);
51 Set<Map.Entry<String, SvcLogicExpression>> parameterSet = node
54 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parameterSet
55 .iterator(); iter.hasNext();) {
56 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
57 String curName = curEnt.getKey();
58 String lhsVarName = curName;
60 // Resolve LHS of assignment (could contain index variables)
62 SvcLogicExpression lhsExpr = SvcLogicExpressionFactory.parse(curName);
63 lhsVarName = SvcLogicExpressionResolver.resolveVariableName(lhsExpr, node, ctx);
64 } catch (Exception e) {
65 LOG.warn("Caught exception trying to resolve variable name ("+curName+")", e);
69 boolean setValue = true;
71 if (curName.endsWith(".")) {
73 // Copy subtree - value should be a variable name
74 SvcLogicExpression curValue = curEnt.getValue();
76 if (curValue != null) {
77 String rhsRoot = curValue.toString();
79 if ((rhsRoot != null) && (rhsRoot.length() > 0)) {
80 if (rhsRoot.endsWith(".")) {
82 .substring(0, rhsRoot.length() - 1);
86 // SDNGC-2321 : rhsRoot is variable name, possibly with subscript(s) to be resolved
88 SvcLogicExpression rhsExpr = SvcLogicExpressionFactory.parse(rhsRoot);
89 rhsRoot = SvcLogicExpressionResolver.resolveVariableName(rhsExpr, node, ctx);
90 } catch (Exception e) {
91 LOG.warn("Caught exception trying to resolve variable name ("+rhsRoot+")", e);
94 // See if the parameters are reversed (copying service-data to input) .. this
95 // was done as a workaround to earlier issue
96 if (curName.endsWith("-input.") && rhsRoot.startsWith("service-data")) {
97 LOG.warn("Arguments appear to be reversed .. will copy input to service-data instead");
98 lhsVarName = rhsRoot + ".";
99 rhsRoot = curName.substring(0, curName.length()-1);
102 rhsRoot = rhsRoot + ".";
103 String lhsPrefix = lhsVarName;
105 if (lhsPrefix.endsWith(".")) {
106 lhsPrefix = lhsPrefix.substring(0,
107 lhsPrefix.length()-1);
109 int lhsPfxLength = lhsPrefix.length();
110 HashMap<String, String> parmsToAdd = new HashMap<String,String>();
112 for (String sourceVarName : ctx.getAttributeKeySet()) {
114 if (sourceVarName.startsWith(rhsRoot)) {
116 String targetVar = lhsPrefix
119 .substring(rhsRoot.length());
121 LOG.debug("Copying " + sourceVarName
122 + " value to " + targetVar);
124 parmsToAdd.put(targetVar,
125 ctx.getAttribute(sourceVarName));
129 for (String newParmName : parmsToAdd.keySet()) {
130 ctx.setAttribute(newParmName, parmsToAdd.get(newParmName));
134 // If RHS is empty, unset attributes in LHS
135 String lhsPrefix = lhsVarName.substring(0,
136 lhsVarName.length() - 1);
137 int lhsPfxLength = lhsPrefix.length();
139 LinkedList<String> parmsToRemove = new LinkedList<String> ();
141 for (String curCtxVarname : ctx.getAttributeKeySet()) {
143 if (curCtxVarname.startsWith(lhsPrefix)) {
144 LOG.debug("Unsetting " + curCtxVarname);
145 parmsToRemove.add(curCtxVarname);
149 for (String parmName : parmsToRemove) {
150 ctx.setAttribute(parmName, null);
159 String ctxValue = ctx.getAttribute(lhsVarName);
161 if ((ctxValue != null) && (ctxValue.length() > 0)) {
163 LOG.debug("Attribute "
165 + " already set and only-if-unset is true, so not overriding");
170 String curValue = SvcLogicExpressionResolver.evaluate(
171 curEnt.getValue(), node, ctx);
173 if (LOG.isDebugEnabled()) {
174 LOG.trace("Parameter value "
175 + curEnt.getValue().asParsedExpr()
176 + " resolves to " + curValue);
177 LOG.debug("Setting context attribute " + lhsVarName
178 + " to " + curValue);
180 ctx.setAttribute(lhsVarName, curValue);