e9fdc55ef3eb40f494afce84649ff47d4d99dc3a
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / ForNodeExecutor.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 org.openecomp.sdnc.sli.BreakNodeException;
25 import org.openecomp.sdnc.sli.SvcLogicContext;
26 import org.openecomp.sdnc.sli.SvcLogicException;
27 import org.openecomp.sdnc.sli.SvcLogicExpression;
28 import org.openecomp.sdnc.sli.SvcLogicNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class ForNodeExecutor extends SvcLogicNodeExecutor {
33
34         private static final Logger LOG = LoggerFactory
35                         .getLogger(ForNodeExecutor.class);
36
37         @Override
38         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
39                         SvcLogicContext ctx) throws SvcLogicException {
40
41                 SvcLogicExpression atomicExpr = node.getAttribute("atomic");
42                 String atomicStr = SvcLogicExpressionResolver.evaluate(atomicExpr, node, ctx);
43                 boolean isAtomic = !("false".equalsIgnoreCase(atomicStr));
44
45                 int numOutcomes = node.getNumOutcomes();
46                 String idxVar = SvcLogicExpressionResolver.evaluate(
47                                 node.getAttribute("index"), node, ctx);
48                 String startVal = SvcLogicExpressionResolver.evaluate(
49                                 node.getAttribute("start"), node, ctx);
50                 String endVal = SvcLogicExpressionResolver.evaluate(
51                                 node.getAttribute("end"), node, ctx);
52
53                 LOG.debug("Executing "+ (isAtomic ? "atomic" : "non-atomic") + " for loop - for (int " + idxVar + " = " + startVal
54                                 + "; " + idxVar + " < " + endVal + "; " + idxVar + "++)");
55
56                 int startIdx = 0;
57                 int endIdx = 0;
58
59                 try {
60                         startIdx = Integer.parseInt(startVal);
61                         endIdx = Integer.parseInt(endVal);
62                 } catch (NumberFormatException e) {
63                         SvcLogicExpression silentFailureExpr = node.getAttribute("silentFailure");
64                         String silentFailure = SvcLogicExpressionResolver.evaluate(silentFailureExpr, node, ctx);
65                         boolean isSilentFailure = Boolean.parseBoolean(silentFailure);
66                         String message = "Invalid index values [" + startVal + "," + endVal + "]";
67                         if(!isSilentFailure){
68                         throw new SvcLogicException(message);
69                         }else{
70                             LOG.debug(message + ". Not exiting because silentFailure was set to true.");
71                             return(null);
72                         }
73                 }
74
75         try {
76                 for (int ctr = startIdx; ctr < endIdx; ctr++) {
77
78                         ctx.setAttribute(idxVar, "" + ctr);
79
80                         for (int i = 0; i < numOutcomes; i++) {
81
82                                 if ("failure".equals(ctx.getStatus()) && isAtomic) {
83                                         LOG.info("For - stopped executing nodes due to failure status");
84                                         return(null);
85                                 }
86
87                                 SvcLogicNode nextNode = node.getOutcomeValue("" + (i + 1));
88                                 if (nextNode != null) {
89                                         if (LOG.isDebugEnabled()) {
90                                                 LOG.debug("For  - executing outcome " + (i + 1));
91                                         }
92                                         SvcLogicNode innerNextNode = nextNode;
93                                         while (innerNextNode != null) {
94                                                 innerNextNode = svc.executeNode(innerNextNode, ctx);
95                                         }
96                                 } else {
97                                         if (LOG.isDebugEnabled()) {
98                                                 LOG.debug("For - done: no outcome " + (i + 1));
99                                         }
100                                 }
101                         }
102                 }
103         } catch (BreakNodeException br) {
104             LOG.debug("ForNodeExecutor caught break");
105         }
106                 return (null);
107         }
108
109 }