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