013997eda2658e01ab70454e6d1f64e7572fa297
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / SetNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.provider;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.Map;
28 import java.util.Set;
29
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;
37
38 public class SetNodeExecutor extends SvcLogicNodeExecutor {
39
40         private static final Logger LOG = LoggerFactory
41                         .getLogger(SetNodeExecutor.class);
42
43         @Override
44         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
45                         SvcLogicContext ctx) throws SvcLogicException {
46
47                 String ifunsetStr = SvcLogicExpressionResolver.evaluate(
48                                 node.getAttribute("only-if-unset"), node, ctx);
49
50                 boolean ifunset = "true".equalsIgnoreCase(ifunsetStr);
51
52                 Set<Map.Entry<String, SvcLogicExpression>> parameterSet = node
53                                 .getParameterSet();
54
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;
60                         
61                         // Resolve LHS of assignment (could contain index variables)
62                         try {
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);
67                         }
68                         
69
70                         boolean setValue = true;
71
72                         if (curName.endsWith(".")) {
73
74                                 // Copy subtree - value should be a variable name
75                                 SvcLogicExpression curValue = curEnt.getValue();
76
77                                 if (curValue != null) {
78                                         String rhsRoot = curValue.toString();
79                                 
80                                         if ((rhsRoot != null) && (rhsRoot.length() > 0)) {
81                                                 if (rhsRoot.endsWith(".")) {
82                                                         rhsRoot = rhsRoot
83                                                                         .substring(0, rhsRoot.length() - 1);
84                                                 }
85
86
87                                                 // SDNGC-2321 : rhsRoot is variable name, possibly with subscript(s) to be resolved
88                                                 try {
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);
93                                                 }
94                                                 
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);
101                                                 }
102                                                 
103                                                 rhsRoot = rhsRoot + ".";
104                                                 String lhsPrefix = lhsVarName;
105                                                 
106                                                 if (lhsPrefix.endsWith(".")) {
107                                                         lhsPrefix = lhsPrefix.substring(0,
108                                                                 lhsPrefix.length()-1);
109                                                 }
110                                                 int lhsPfxLength = lhsPrefix.length();
111                                                 HashMap<String, String> parmsToAdd = new HashMap<String,String>();
112
113                                                 for (String sourceVarName : ctx.getAttributeKeySet()) {
114
115                                                         if (sourceVarName.startsWith(rhsRoot)) {
116
117                                                                 String targetVar = lhsPrefix
118                                                                                 + "."
119                                                                                 + sourceVarName
120                                                                                                 .substring(rhsRoot.length());
121
122                                                                 LOG.debug("Copying " + sourceVarName
123                                                                                 + " value to " + targetVar);
124
125                                                                 parmsToAdd.put(targetVar,
126                                                                                 ctx.getAttribute(sourceVarName));
127                                                         }
128                                                 }
129                                                 
130                                                 for (String newParmName : parmsToAdd.keySet()) {
131                                                         ctx.setAttribute(newParmName, parmsToAdd.get(newParmName));
132                                                 }
133
134                                         } else {
135                                                 // If RHS is empty, unset attributes in LHS
136                                                 String lhsPrefix = lhsVarName.substring(0,
137                                                                 lhsVarName.length() - 1);
138                                                 int lhsPfxLength = lhsPrefix.length();
139                                                 
140                                                 LinkedList<String> parmsToRemove = new LinkedList<String> ();
141
142                                                 for (String curCtxVarname : ctx.getAttributeKeySet()) {
143
144                                                         if (curCtxVarname.startsWith(lhsPrefix)) {
145                                                                 LOG.debug("Unsetting " + curCtxVarname);
146                                                                 parmsToRemove.add(curCtxVarname);
147                                                         }
148                                                 }
149                                                 
150                                                 for (String parmName : parmsToRemove) {
151                                                         ctx.setAttribute(parmName, null);
152                                                 }
153
154                                         }
155                                 }
156
157                         } else {
158
159                                 if (ifunset) {
160                                         String ctxValue = ctx.getAttribute(lhsVarName);
161
162                                         if ((ctxValue != null) && (ctxValue.length() > 0)) {
163                                                 setValue = false;
164                                                 LOG.debug("Attribute "
165                                                                 + lhsVarName
166                                                                 + " already set and only-if-unset is true, so not overriding");
167                                         }
168                                 }
169
170                                 if (setValue) {
171                                         String curValue = SvcLogicExpressionResolver.evaluate(
172                                                         curEnt.getValue(), node, ctx);
173
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);
180                                         }
181                                         ctx.setAttribute(lhsVarName, curValue);
182                                 }
183                         }
184                 }
185                 
186                 return null;
187         }
188
189 }