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.io.IOException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
32 import org.openecomp.sdnc.sli.SvcLogicException;
33 import org.openecomp.sdnc.sli.SvcLogicExpression;
34 import org.openecomp.sdnc.sli.SvcLogicExpressionFactory;
35 import org.openecomp.sdnc.sli.SvcLogicNode;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class SetNodeExecutor extends SvcLogicNodeExecutor {
41 private static final Logger LOG = LoggerFactory
42 .getLogger(SetNodeExecutor.class);
45 public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
46 SvcLogicContext ctx) throws SvcLogicException {
48 String ifunsetStr = SvcLogicExpressionResolver.evaluate(
49 node.getAttribute("only-if-unset"), node, ctx);
51 boolean ifunset = "true".equalsIgnoreCase(ifunsetStr);
53 Set<Map.Entry<String, SvcLogicExpression>> parameterSet = node
56 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parameterSet
57 .iterator(); iter.hasNext();) {
58 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
59 String curName = curEnt.getKey();
60 String lhsVarName = curName;
62 // Resolve LHS of assignment (could contain index variables)
64 SvcLogicExpression lhsExpr = SvcLogicExpressionFactory.parse(curName);
65 lhsVarName = SvcLogicExpressionResolver.resolveVariableName(lhsExpr, node, ctx);
66 } catch (Exception e) {
67 LOG.warn("Caught exception trying to resolve variable name ("+curName+")", e);
71 boolean setValue = true;
73 if (curName.endsWith(".")) {
75 // Copy subtree - value should be a variable name
76 SvcLogicExpression curValue = curEnt.getValue();
78 if (curValue != null) {
79 String rhsRoot = curValue.toString();
81 if ((rhsRoot != null) && (rhsRoot.length() > 0)) {
82 if (rhsRoot.endsWith(".")) {
84 .substring(0, rhsRoot.length() - 1);
88 // SDNGC-2321 : rhsRoot is variable name, possibly with subscript(s) to be resolved
90 SvcLogicExpression rhsExpr = SvcLogicExpressionFactory.parse(rhsRoot);
91 rhsRoot = SvcLogicExpressionResolver.resolveVariableName(rhsExpr, node, ctx);
92 } catch (Exception e) {
93 LOG.warn("Caught exception trying to resolve variable name ("+rhsRoot+")", e);
96 // See if the parameters are reversed (copying service-data to input) .. this
97 // was done as a workaround to earlier issue
98 if (curName.endsWith("-input.") && rhsRoot.startsWith("service-data")) {
99 LOG.warn("Arguments appear to be reversed .. will copy input to service-data instead");
100 lhsVarName = rhsRoot + ".";
101 rhsRoot = curName.substring(0, curName.length()-1);
104 rhsRoot = rhsRoot + ".";
105 String lhsPrefix = lhsVarName;
107 if (lhsPrefix.endsWith(".")) {
108 lhsPrefix = lhsPrefix.substring(0,
109 lhsPrefix.length()-1);
111 int lhsPfxLength = lhsPrefix.length();
112 HashMap<String, String> parmsToAdd = new HashMap<String,String>();
114 for (String sourceVarName : ctx.getAttributeKeySet()) {
116 if (sourceVarName.startsWith(rhsRoot)) {
118 String targetVar = lhsPrefix
121 .substring(rhsRoot.length());
123 LOG.debug("Copying " + sourceVarName
124 + " value to " + targetVar);
126 parmsToAdd.put(targetVar,
127 ctx.getAttribute(sourceVarName));
131 for (String newParmName : parmsToAdd.keySet()) {
132 ctx.setAttribute(newParmName, parmsToAdd.get(newParmName));
136 // If RHS is empty, unset attributes in LHS
137 String lhsPrefix = lhsVarName.substring(0,
138 lhsVarName.length() - 1);
139 int lhsPfxLength = lhsPrefix.length();
141 LinkedList<String> parmsToRemove = new LinkedList<String> ();
143 for (String curCtxVarname : ctx.getAttributeKeySet()) {
145 if (curCtxVarname.startsWith(lhsPrefix)) {
146 LOG.debug("Unsetting " + curCtxVarname);
147 parmsToRemove.add(curCtxVarname);
151 for (String parmName : parmsToRemove) {
152 ctx.setAttribute(parmName, null);
161 String ctxValue = ctx.getAttribute(lhsVarName);
163 if ((ctxValue != null) && (ctxValue.length() > 0)) {
165 LOG.debug("Attribute "
167 + " already set and only-if-unset is true, so not overriding");
172 String curValue = SvcLogicExpressionResolver.evaluate(
173 curEnt.getValue(), node, ctx);
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("Parameter value "
177 + curEnt.getValue().asParsedExpr()
178 + " resolves to " + curValue);
179 LOG.debug("Setting context attribute " + lhsVarName
180 + " to " + curValue);
182 ctx.setAttribute(lhsVarName, curValue);