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