Refactor dblib
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / ConfigureNodeExecutor.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.Map;
26 import java.util.Set;
27
28 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
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.SvcLogicNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class ConfigureNodeExecutor extends SvcLogicNodeExecutor {
37         private static final Logger LOG = LoggerFactory
38                         .getLogger(ConfigureNodeExecutor.class);
39
40         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
41                         SvcLogicContext ctx) throws SvcLogicException {
42
43                 String adaptorName = SvcLogicExpressionResolver.evaluate(
44                                 node.getAttribute("adaptor"), node, ctx);
45                 String outValue = "failure";
46
47                 if (LOG.isDebugEnabled()) {
48                         LOG.debug("configure node encountered - looking for adaptor "
49                                         + adaptorName);
50                 }
51
52                 SvcLogicAdaptor adaptor = getAdaptor(adaptorName);
53
54                 if (adaptor != null) {
55                         String activate = SvcLogicExpressionResolver.evaluate(
56                                         node.getAttribute("activate"), node, ctx);
57                         String key = SvcLogicExpressionResolver.evaluate(
58                                         node.getAttribute("key"), node, ctx);
59
60                         Map<String, String> parmMap = new HashMap<String, String>();
61
62                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
63                                         .getParameterSet();
64                         boolean hasParms = false;
65
66                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
67                                         .iterator(); iter.hasNext();) {
68                                 hasParms = true;
69                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
70                                 String curName = curEnt.getKey();
71                                 SvcLogicExpression curExpr = curEnt.getValue();
72                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
73                                 
74                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
75
76                                 parmMap.put(curName,curExprValue);
77                         }
78
79                         if (hasParms) {
80                                 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
81                                 
82                                 try {
83                                         confStatus = adaptor.configure(key, parmMap, ctx);
84                                 } catch (Exception e) {
85                                         LOG.warn("Caught exception from "+adaptorName+".configure", e);
86                                         confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
87                                 }
88                                 
89                                 switch (confStatus) {
90                                 case SUCCESS:
91                                         outValue = "success";
92                                         if ((activate != null) && (activate.length() > 0)) {
93                                                 if ("true".equalsIgnoreCase(activate)) {
94                                                         SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
95                                                         
96                                                         try {
97                                                                 activateStatus = adaptor.activate(key, ctx);
98                                                         } catch (Exception e) {
99
100                                                                 LOG.warn("Caught exception from "+adaptorName+".activate", e);
101                                                                 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
102                                                         }
103                                                         switch (activateStatus) {
104                                                         case SUCCESS:
105                                                                 break;
106                                                         case ALREADY_ACTIVE:
107                                                                 outValue = "already-active";
108                                                                 break;
109                                                         case NOT_FOUND:
110                                                                 outValue = "not-found";
111                                                                 break;
112                                                         case NOT_READY:
113                                                                 outValue = "not-ready";
114                                                                 break;
115                                                         case FAILURE:
116                                                         default:
117                                                                 outValue = "failure";
118                                                         }
119                                                 } else if ("false".equalsIgnoreCase(activate)) {
120                                                         SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
121                                                         
122                                                         try {
123                                                                 deactivateStatus = adaptor.deactivate(key, ctx);
124                                                         } catch (Exception e) {
125
126                                                                 LOG.warn("Caught exception from "+adaptorName+".deactivate", e);
127                                                                 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
128                                                         }
129                                                         switch (deactivateStatus) {
130                                                         case SUCCESS:
131                                                                 break;
132                                                         case ALREADY_ACTIVE:
133                                                                 outValue = "already-active";
134                                                                 break;
135                                                         case NOT_FOUND:
136                                                                 outValue = "not-found";
137                                                                 break;
138                                                         case NOT_READY:
139                                                                 outValue = "not-ready";
140                                                                 break;
141                                                         case FAILURE:
142                                                         default:
143                                                                 outValue = "failure";
144                                                         }
145                                                 }
146                                         }
147                                         break;
148                                 case ALREADY_ACTIVE:
149                                         outValue = "already-active";
150                                         break;
151                                 case NOT_FOUND:
152                                         outValue = "not-found";
153                                         break;
154                                 case NOT_READY:
155                                         outValue = "not-ready";
156                                         break;
157                                 case FAILURE:
158                                 default:
159                                         outValue = "failure";
160                                 }
161                         } else {
162                                 if ((activate != null) && (activate.length() > 0)) {
163                                         if ("true".equalsIgnoreCase(activate)) {
164                                                 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
165                                                 try {
166                                                         activateStatus = adaptor.activate(key, ctx);
167                                                 } catch (Exception e) {
168                                                         LOG.warn("Caught exception from "+adaptorName+".activate", e);
169                                                         activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
170                                                 }
171                                                 switch (activateStatus) {
172                                                 case SUCCESS:
173                                                         outValue = "success";
174                                                         break;
175                                                 case ALREADY_ACTIVE:
176                                                         outValue = "already-active";
177                                                         break;
178                                                 case NOT_FOUND:
179                                                         outValue = "not-found";
180                                                         break;
181                                                 case NOT_READY:
182                                                         outValue = "not-ready";
183                                                         break;
184                                                 case FAILURE:
185                                                 default:
186                                                         outValue = "failure";
187                                                 }
188                                         } else if ("false".equalsIgnoreCase(activate)) {
189                                                 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
190                                                 
191                                                 try {
192                                                         deactivateStatus = adaptor.deactivate(key, ctx);
193                                                 } catch (Exception e) {
194                                                         LOG.warn("Caught exception from "+adaptorName+".deactivate", e);
195                                                         deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
196                                                 }
197                                                 switch (deactivateStatus) {
198                                                 case SUCCESS:
199                                                         outValue = "success";
200                                                         break;
201                                                 case ALREADY_ACTIVE:
202                                                         outValue = "already-active";
203                                                         break;
204                                                 case NOT_FOUND:
205                                                         outValue = "not-found";
206                                                         break;
207                                                 case NOT_READY:
208                                                         outValue = "not-ready";
209                                                         break;
210                                                 case FAILURE:
211                                                 default:
212                                                         outValue = "failure";
213                                                 }
214                                         }
215                                 } else {
216                                         LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
217                                         outValue = "success";
218                                 }
219                         }
220                 } else {
221                         if (LOG.isWarnEnabled()) {
222                                 LOG.warn("Adaptor for " + adaptorName + " not found");
223                         }
224                 }
225
226                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
227                 if (nextNode != null) {
228                         if (LOG.isDebugEnabled()) {
229                                 LOG.debug("about to execute " + outValue + " branch");
230                         }
231                         return (nextNode);
232                 }
233
234                 nextNode = node.getOutcomeValue("Other");
235                 if (nextNode != null) {
236                         if (LOG.isDebugEnabled()) {
237                                 LOG.debug("about to execute Other branch");
238                         }
239                 } else {
240                         if (LOG.isDebugEnabled()) {
241                                 LOG.debug("no " + outValue + " or Other branch found");
242                         }
243                 }
244                 return (nextNode);
245         }
246
247 }