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