5d6a08391dbb0a14c4a154dd8be5239753de9d5f
[ccsdk/sli/core.git] / sli / provider-base / src / main / java / org / onap / ccsdk / sli / core / sli / provider / base / ConfigureNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.core.sli.provider.base;
25
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ConfigureNodeExecutor extends AbstractSvcLogicNodeExecutor {
40         private static final Logger LOG = LoggerFactory
41                         .getLogger(ConfigureNodeExecutor.class);
42         private static final String CAUGHT_EXCEPTION_MSG="Caught exception from ";
43         private static final String ALREADY_ACTIVE= "already-active";
44         private static final String NOT_FOUND= "not-found";
45
46         public SvcLogicNode execute(SvcLogicServiceBase svc, SvcLogicNode node,
47                         SvcLogicContext ctx) throws SvcLogicException {
48
49                 String adaptorName = SvcLogicExpressionResolver.evaluate(
50                                 node.getAttribute("adaptor"), node, ctx);
51                 String outValue = "failure";
52
53                 if (LOG.isDebugEnabled()) {
54                         LOG.debug("configure node encountered - looking for adaptor "
55                                         + adaptorName);
56                 }
57
58                 SvcLogicAdaptor adaptor = getAdaptor(adaptorName);
59
60                 if (adaptor != null) {
61                         String activate = SvcLogicExpressionResolver.evaluate(
62                                         node.getAttribute("activate"), node, ctx);
63                         String key = SvcLogicExpressionResolver.evaluate(
64                                         node.getAttribute("key"), node, ctx);
65
66                         Map<String, String> parmMap = new HashMap<>();
67
68                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
69                                         .getParameterSet();
70                         boolean hasParms = false;
71
72                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
73                                         .iterator(); iter.hasNext();) {
74                                 hasParms = true;
75                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
76                                 String curName = curEnt.getKey();
77                                 SvcLogicExpression curExpr = curEnt.getValue();
78                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
79                                 
80                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
81
82                                 parmMap.put(curName,curExprValue);
83                         }
84
85                         if (hasParms) {
86                                 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
87                                 
88                                 try {
89                                         confStatus = adaptor.configure(key, parmMap, ctx);
90                                 } catch (Exception e) {
91                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".configure", e);
92                                         confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
93                                 }
94                                 
95                                 switch (confStatus) {
96                                 case SUCCESS:
97                                         outValue = "success";
98                                         if ((activate != null) && (activate.length() > 0)) {
99                                                 if ("true".equalsIgnoreCase(activate)) {
100                                                         SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
101                                                         
102                                                         try {
103                                                                 activateStatus = adaptor.activate(key, ctx);
104                                                         } catch (Exception e) {
105
106                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
107                                                                 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
108                                                         }
109                                                         switch (activateStatus) {
110                                                         case SUCCESS:
111                                                                 break;
112                                                         case ALREADY_ACTIVE:
113                                                                 outValue = ALREADY_ACTIVE;
114                                                                 break;
115                                                         case NOT_FOUND:
116                                                                 outValue = NOT_FOUND;
117                                                                 break;
118                                                         case NOT_READY:
119                                                                 outValue = "not-ready";
120                                                                 break;
121                                                         case FAILURE:
122                                                         default:
123                                                                 outValue = "failure";
124                                                         }
125                                                 } else if ("false".equalsIgnoreCase(activate)) {
126                                                         SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
127                                                         
128                                                         try {
129                                                                 deactivateStatus = adaptor.deactivate(key, ctx);
130                                                         } catch (Exception e) {
131
132                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
133                                                                 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
134                                                         }
135                                                         switch (deactivateStatus) {
136                                                         case SUCCESS:
137                                                                 break;
138                                                         case ALREADY_ACTIVE:
139                                                                 outValue = ALREADY_ACTIVE;
140                                                                 break;
141                                                         case NOT_FOUND:
142                                                                 outValue = NOT_FOUND;
143                                                                 break;
144                                                         case NOT_READY:
145                                                                 outValue = "not-ready";
146                                                                 break;
147                                                         case FAILURE:
148                                                         default:
149                                                                 outValue = "failure";
150                                                         }
151                                                 }
152                                         }
153                                         break;
154                                 case ALREADY_ACTIVE:
155                                         outValue = ALREADY_ACTIVE;
156                                         break;
157                                 case NOT_FOUND:
158                                         outValue = NOT_FOUND;
159                                         break;
160                                 case NOT_READY:
161                                         outValue = "not-ready";
162                                         break;
163                                 case FAILURE:
164                                 default:
165                                         outValue = "failure";
166                                 }
167                         } else {
168                                 if ((activate != null) && (activate.length() > 0)) {
169                                         if ("true".equalsIgnoreCase(activate)) {
170                                                 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
171                                                 try {
172                                                         activateStatus = adaptor.activate(key, ctx);
173                                                 } catch (Exception e) {
174                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
175                                                         activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
176                                                 }
177                                                 switch (activateStatus) {
178                                                 case SUCCESS:
179                                                         outValue = "success";
180                                                         break;
181                                                 case ALREADY_ACTIVE:
182                                                         outValue = ALREADY_ACTIVE;
183                                                         break;
184                                                 case NOT_FOUND:
185                                                         outValue = NOT_FOUND;
186                                                         break;
187                                                 case NOT_READY:
188                                                         outValue = "not-ready";
189                                                         break;
190                                                 case FAILURE:
191                                                 default:
192                                                         outValue = "failure";
193                                                 }
194                                         } else if ("false".equalsIgnoreCase(activate)) {
195                                                 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
196                                                 
197                                                 try {
198                                                         deactivateStatus = adaptor.deactivate(key, ctx);
199                                                 } catch (Exception e) {
200                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
201                                                         deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
202                                                 }
203                                                 switch (deactivateStatus) {
204                                                 case SUCCESS:
205                                                         outValue = "success";
206                                                         break;
207                                                 case ALREADY_ACTIVE:
208                                                         outValue = ALREADY_ACTIVE;
209                                                         break;
210                                                 case NOT_FOUND:
211                                                         outValue = NOT_FOUND;
212                                                         break;
213                                                 case NOT_READY:
214                                                         outValue = "not-ready";
215                                                         break;
216                                                 case FAILURE:
217                                                 default:
218                                                         outValue = "failure";
219                                                 }
220                                         }
221                                 } else {
222                                         LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
223                                         outValue = "success";
224                                 }
225                         }
226                 } else {
227                         if (LOG.isWarnEnabled()) {
228                                 LOG.warn("Adaptor for " + adaptorName + " not found");
229                         }
230                 }
231
232                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
233                 if (nextNode != null) {
234                         if (LOG.isDebugEnabled()) {
235                                 LOG.debug("about to execute " + outValue + " branch");
236                         }
237                         return (nextNode);
238                 }
239
240                 nextNode = node.getOutcomeValue("Other");
241                 if (nextNode != null) {
242                         if (LOG.isDebugEnabled()) {
243                                 LOG.debug("about to execute Other branch");
244                         }
245                 } else {
246                         if (LOG.isDebugEnabled()) {
247                                 LOG.debug("no " + outValue + " or Other branch found");
248                         }
249                 }
250                 return (nextNode);
251         }
252
253 }