logging changes
[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                         boolean hasParms = false;
67                         
68             Map<String, String> parmMap = getResolvedParameters(node,ctx);
69             if(!parmMap.isEmpty()) {
70                 hasParms = true;
71             }
72
73                         if (hasParms) {
74                                 SvcLogicAdaptor.ConfigStatus confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
75                                 
76                                 try {
77                                         confStatus = adaptor.configure(key, parmMap, ctx);
78                                 } catch (Exception e) {
79                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".configure", e);
80                                         confStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
81                                 }
82                                 
83                                 switch (confStatus) {
84                                 case SUCCESS:
85                                         outValue = "success";
86                                         if ((activate != null) && (activate.length() > 0)) {
87                                                 if ("true".equalsIgnoreCase(activate)) {
88                                                         SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
89                                                         
90                                                         try {
91                                                                 activateStatus = adaptor.activate(key, ctx);
92                                                         } catch (Exception e) {
93
94                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
95                                                                 activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
96                                                         }
97                                                         switch (activateStatus) {
98                                                         case SUCCESS:
99                                                                 break;
100                                                         case ALREADY_ACTIVE:
101                                                                 outValue = ALREADY_ACTIVE;
102                                                                 break;
103                                                         case NOT_FOUND:
104                                                                 outValue = NOT_FOUND;
105                                                                 break;
106                                                         case NOT_READY:
107                                                                 outValue = "not-ready";
108                                                                 break;
109                                                         case FAILURE:
110                                                         default:
111                                                                 outValue = "failure";
112                                                         }
113                                                 } else if ("false".equalsIgnoreCase(activate)) {
114                                                         SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
115                                                         
116                                                         try {
117                                                                 deactivateStatus = adaptor.deactivate(key, ctx);
118                                                         } catch (Exception e) {
119
120                                                                 LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
121                                                                 deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
122                                                         }
123                                                         switch (deactivateStatus) {
124                                                         case SUCCESS:
125                                                                 break;
126                                                         case ALREADY_ACTIVE:
127                                                                 outValue = ALREADY_ACTIVE;
128                                                                 break;
129                                                         case NOT_FOUND:
130                                                                 outValue = NOT_FOUND;
131                                                                 break;
132                                                         case NOT_READY:
133                                                                 outValue = "not-ready";
134                                                                 break;
135                                                         case FAILURE:
136                                                         default:
137                                                                 outValue = "failure";
138                                                         }
139                                                 }
140                                         }
141                                         break;
142                                 case ALREADY_ACTIVE:
143                                         outValue = ALREADY_ACTIVE;
144                                         break;
145                                 case NOT_FOUND:
146                                         outValue = NOT_FOUND;
147                                         break;
148                                 case NOT_READY:
149                                         outValue = "not-ready";
150                                         break;
151                                 case FAILURE:
152                                 default:
153                                         outValue = "failure";
154                                 }
155                         } else {
156                                 if ((activate != null) && (activate.length() > 0)) {
157                                         if ("true".equalsIgnoreCase(activate)) {
158                                                 SvcLogicAdaptor.ConfigStatus activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
159                                                 try {
160                                                         activateStatus = adaptor.activate(key, ctx);
161                                                 } catch (Exception e) {
162                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".activate", e);
163                                                         activateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
164                                                 }
165                                                 switch (activateStatus) {
166                                                 case SUCCESS:
167                                                         outValue = "success";
168                                                         break;
169                                                 case ALREADY_ACTIVE:
170                                                         outValue = ALREADY_ACTIVE;
171                                                         break;
172                                                 case NOT_FOUND:
173                                                         outValue = NOT_FOUND;
174                                                         break;
175                                                 case NOT_READY:
176                                                         outValue = "not-ready";
177                                                         break;
178                                                 case FAILURE:
179                                                 default:
180                                                         outValue = "failure";
181                                                 }
182                                         } else if ("false".equalsIgnoreCase(activate)) {
183                                                 SvcLogicAdaptor.ConfigStatus deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
184                                                 
185                                                 try {
186                                                         deactivateStatus = adaptor.deactivate(key, ctx);
187                                                 } catch (Exception e) {
188                                                         LOG.warn(CAUGHT_EXCEPTION_MSG+adaptorName+".deactivate", e);
189                                                         deactivateStatus = SvcLogicAdaptor.ConfigStatus.FAILURE;
190                                                 }
191                                                 switch (deactivateStatus) {
192                                                 case SUCCESS:
193                                                         outValue = "success";
194                                                         break;
195                                                 case ALREADY_ACTIVE:
196                                                         outValue = ALREADY_ACTIVE;
197                                                         break;
198                                                 case NOT_FOUND:
199                                                         outValue = NOT_FOUND;
200                                                         break;
201                                                 case NOT_READY:
202                                                         outValue = "not-ready";
203                                                         break;
204                                                 case FAILURE:
205                                                 default:
206                                                         outValue = "failure";
207                                                 }
208                                         }
209                                 } else {
210                                         LOG.warn("Nothing to configure - no parameters passed, and activate attribute is not set");
211                                         outValue = "success";
212                                 }
213                         }
214                 } else {
215                         if (LOG.isWarnEnabled()) {
216                                 LOG.warn("Adaptor for " + adaptorName + " not found");
217                         }
218                 }
219
220                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
221                 if (nextNode != null) {
222                         if (LOG.isDebugEnabled()) {
223                                 LOG.debug("about to execute " + outValue + " branch");
224                         }
225                         return (nextNode);
226                 }
227
228                 nextNode = node.getOutcomeValue("Other");
229                 if (nextNode != null) {
230                         if (LOG.isDebugEnabled()) {
231                                 LOG.debug("about to execute Other branch");
232                         }
233                 } else {
234                         if (LOG.isDebugEnabled()) {
235                                 LOG.debug("no " + outValue + " or Other branch found");
236                         }
237                 }
238                 return (nextNode);
239         }
240
241 }