2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdnc.sli.provider;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
30 import org.openecomp.sdnc.sli.SvcLogicContext;
31 import org.openecomp.sdnc.sli.SvcLogicException;
32 import org.openecomp.sdnc.sli.SvcLogicExpression;
33 import org.openecomp.sdnc.sli.SvcLogicExpressionFactory;
34 import org.openecomp.sdnc.sli.SvcLogicNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class SetNodeExecutor extends SvcLogicNodeExecutor {
40 private static final Logger LOG = LoggerFactory
41 .getLogger(SetNodeExecutor.class);
44 public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
45 SvcLogicContext ctx) throws SvcLogicException {
47 String ifunsetStr = SvcLogicExpressionResolver.evaluate(
48 node.getAttribute("only-if-unset"), node, ctx);
50 boolean ifunset = "true".equalsIgnoreCase(ifunsetStr);
52 Set<Map.Entry<String, SvcLogicExpression>> parameterSet = node
55 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parameterSet
56 .iterator(); iter.hasNext();) {
57 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
58 String curName = curEnt.getKey();
59 String lhsVarName = curName;
61 // Resolve LHS of assignment (could contain index variables)
63 SvcLogicExpression lhsExpr = SvcLogicExpressionFactory.parse(curName);
64 lhsVarName = SvcLogicExpressionResolver.resolveVariableName(lhsExpr, node, ctx);
65 } catch (Exception e) {
66 LOG.warn("Caught exception trying to resolve variable name ("+curName+")", e);
70 boolean setValue = true;
72 if (curName.endsWith(".")) {
74 // Copy subtree - value should be a variable name
75 SvcLogicExpression curValue = curEnt.getValue();
77 if (curValue != null) {
78 String rhsRoot = curValue.toString();
80 if ((rhsRoot != null) && (rhsRoot.length() > 0)) {
81 if (rhsRoot.endsWith(".")) {
83 .substring(0, rhsRoot.length() - 1);
87 // SDNGC-2321 : rhsRoot is variable name, possibly with subscript(s) to be resolved
89 SvcLogicExpression rhsExpr = SvcLogicExpressionFactory.parse(rhsRoot);
90 rhsRoot = SvcLogicExpressionResolver.resolveVariableName(rhsExpr, node, ctx);
91 } catch (Exception e) {
92 LOG.warn("Caught exception trying to resolve variable name ("+rhsRoot+")", e);
95 // See if the parameters are reversed (copying service-data to input) .. this
96 // was done as a workaround to earlier issue
97 if (curName.endsWith("-input.") && rhsRoot.startsWith("service-data")) {
98 LOG.warn("Arguments appear to be reversed .. will copy input to service-data instead");
99 lhsVarName = rhsRoot + ".";
100 rhsRoot = curName.substring(0, curName.length()-1);
103 rhsRoot = rhsRoot + ".";
104 String lhsPrefix = lhsVarName;
106 if (lhsPrefix.endsWith(".")) {
107 lhsPrefix = lhsPrefix.substring(0,
108 lhsPrefix.length()-1);
110 int lhsPfxLength = lhsPrefix.length();
111 HashMap<String, String> parmsToAdd = new HashMap<String,String>();
113 for (String sourceVarName : ctx.getAttributeKeySet()) {
115 if (sourceVarName.startsWith(rhsRoot)) {
117 String targetVar = lhsPrefix
120 .substring(rhsRoot.length());
122 LOG.debug("Copying " + sourceVarName
123 + " value to " + targetVar);
125 parmsToAdd.put(targetVar,
126 ctx.getAttribute(sourceVarName));
130 for (String newParmName : parmsToAdd.keySet()) {
131 ctx.setAttribute(newParmName, parmsToAdd.get(newParmName));
135 // If RHS is empty, unset attributes in LHS
136 String lhsPrefix = lhsVarName.substring(0,
137 lhsVarName.length() - 1);
138 int lhsPfxLength = lhsPrefix.length();
140 LinkedList<String> parmsToRemove = new LinkedList<String> ();
142 for (String curCtxVarname : ctx.getAttributeKeySet()) {
144 if (curCtxVarname.startsWith(lhsPrefix)) {
145 LOG.debug("Unsetting " + curCtxVarname);
146 parmsToRemove.add(curCtxVarname);
150 for (String parmName : parmsToRemove) {
151 ctx.setAttribute(parmName, null);
160 String ctxValue = ctx.getAttribute(lhsVarName);
162 if ((ctxValue != null) && (ctxValue.length() > 0)) {
164 LOG.debug("Attribute "
166 + " already set and only-if-unset is true, so not overriding");
171 String curValue = SvcLogicExpressionResolver.evaluate(
172 curEnt.getValue(), node, ctx);
174 if (LOG.isDebugEnabled()) {
175 LOG.trace("Parameter value "
176 + curEnt.getValue().asParsedExpr()
177 + " resolves to " + curValue);
178 LOG.debug("Setting context attribute " + lhsVarName
179 + " to " + curValue);
181 ctx.setAttribute(lhsVarName, curValue);