55ad737e9d10f910870af62b7e7fbe24bd7dc4a6
[sdnc/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.io.IOException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.Map;
29 import java.util.Set;
30
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;
38
39 public class SetNodeExecutor extends SvcLogicNodeExecutor {
40
41         private static final Logger LOG = LoggerFactory
42                         .getLogger(SetNodeExecutor.class);
43
44         @Override
45         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
46                         SvcLogicContext ctx) throws SvcLogicException {
47
48                 String ifunsetStr = SvcLogicExpressionResolver.evaluate(
49                                 node.getAttribute("only-if-unset"), node, ctx);
50
51                 boolean ifunset = "true".equalsIgnoreCase(ifunsetStr);
52
53                 Set<Map.Entry<String, SvcLogicExpression>> parameterSet = node
54                                 .getParameterSet();
55
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;
61                         
62                         // Resolve LHS of assignment (could contain index variables)
63                         try {
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);
68                         }
69                         
70
71                         boolean setValue = true;
72
73                         if (curName.endsWith(".")) {
74
75                                 // Copy subtree - value should be a variable name
76                                 SvcLogicExpression curValue = curEnt.getValue();
77
78                                 if (curValue != null) {
79                                         String rhsRoot = curValue.toString();
80                                 
81                                         if ((rhsRoot != null) && (rhsRoot.length() > 0)) {
82                                                 if (rhsRoot.endsWith(".")) {
83                                                         rhsRoot = rhsRoot
84                                                                         .substring(0, rhsRoot.length() - 1);
85                                                 }
86
87
88                                                 // SDNGC-2321 : rhsRoot is variable name, possibly with subscript(s) to be resolved
89                                                 try {
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);
94                                                 }
95                                                 
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);
102                                                 }
103                                                 
104                                                 rhsRoot = rhsRoot + ".";
105                                                 String lhsPrefix = lhsVarName;
106                                                 
107                                                 if (lhsPrefix.endsWith(".")) {
108                                                         lhsPrefix = lhsPrefix.substring(0,
109                                                                 lhsPrefix.length()-1);
110                                                 }
111                                                 int lhsPfxLength = lhsPrefix.length();
112                                                 HashMap<String, String> parmsToAdd = new HashMap<String,String>();
113
114                                                 for (String sourceVarName : ctx.getAttributeKeySet()) {
115
116                                                         if (sourceVarName.startsWith(rhsRoot)) {
117
118                                                                 String targetVar = lhsPrefix
119                                                                                 + "."
120                                                                                 + sourceVarName
121                                                                                                 .substring(rhsRoot.length());
122
123                                                                 LOG.debug("Copying " + sourceVarName
124                                                                                 + " value to " + targetVar);
125
126                                                                 parmsToAdd.put(targetVar,
127                                                                                 ctx.getAttribute(sourceVarName));
128                                                         }
129                                                 }
130                                                 
131                                                 for (String newParmName : parmsToAdd.keySet()) {
132                                                         ctx.setAttribute(newParmName, parmsToAdd.get(newParmName));
133                                                 }
134
135                                         } else {
136                                                 // If RHS is empty, unset attributes in LHS
137                                                 String lhsPrefix = lhsVarName.substring(0,
138                                                                 lhsVarName.length() - 1);
139                                                 int lhsPfxLength = lhsPrefix.length();
140                                                 
141                                                 LinkedList<String> parmsToRemove = new LinkedList<String> ();
142
143                                                 for (String curCtxVarname : ctx.getAttributeKeySet()) {
144
145                                                         if (curCtxVarname.startsWith(lhsPrefix)) {
146                                                                 LOG.debug("Unsetting " + curCtxVarname);
147                                                                 parmsToRemove.add(curCtxVarname);
148                                                         }
149                                                 }
150                                                 
151                                                 for (String parmName : parmsToRemove) {
152                                                         ctx.setAttribute(parmName, null);
153                                                 }
154
155                                         }
156                                 }
157
158                         } else {
159
160                                 if (ifunset) {
161                                         String ctxValue = ctx.getAttribute(lhsVarName);
162
163                                         if ((ctxValue != null) && (ctxValue.length() > 0)) {
164                                                 setValue = false;
165                                                 LOG.debug("Attribute "
166                                                                 + lhsVarName
167                                                                 + " already set and only-if-unset is true, so not overriding");
168                                         }
169                                 }
170
171                                 if (setValue) {
172                                         String curValue = SvcLogicExpressionResolver.evaluate(
173                                                         curEnt.getValue(), node, ctx);
174
175                                         if (LOG.isDebugEnabled()) {
176                                                 LOG.trace("Parameter value "
177                                                                 + curEnt.getValue().asParsedExpr()
178                                                                 + " resolves to " + curValue);
179                                                 LOG.debug("Setting context attribute " + lhsVarName
180                                                                 + " to " + curValue);
181                                         }
182                                         ctx.setAttribute(lhsVarName, curValue);
183                                 }
184                         }
185                 }
186                 
187                 return null;
188         }
189
190 }