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