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