7ec36e1ba7c907af315609e67edf06eb7f66f803
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / WhileNodeExecutor.java
1 /*-\r
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");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  * \r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * \r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.openecomp.sdnc.sli.provider;\r
23 \r
24 import org.slf4j.Logger;\r
25 import org.slf4j.LoggerFactory;\r
26 \r
27 import org.openecomp.sdnc.sli.BreakNodeException;\r
28 import org.openecomp.sdnc.sli.SvcLogicContext;\r
29 import org.openecomp.sdnc.sli.SvcLogicException;\r
30 import org.openecomp.sdnc.sli.SvcLogicExpression;\r
31 import org.openecomp.sdnc.sli.SvcLogicNode;\r
32 \r
33 public class WhileNodeExecutor extends SvcLogicNodeExecutor {\r
34 \r
35     private static final Logger LOG = LoggerFactory.getLogger(WhileNodeExecutor.class);\r
36 \r
37     @Override\r
38     public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {\r
39 \r
40         String testResult = evaluateNodeTest(node, ctx);\r
41         SvcLogicExpression silentFailureExpr = node.getAttribute("do");\r
42         String doWhile = SvcLogicExpressionResolver.evaluate(silentFailureExpr, node, ctx);\r
43         if ("true".equals(doWhile)) {\r
44             LOG.debug("While loop will execute once regardless of expression because do is set to true");\r
45         }\r
46 \r
47         try {\r
48             while ("true".equals(testResult) || "true".equals(doWhile)) {\r
49                 if (!"true".equals(doWhile)) {\r
50                     LOG.debug("Test expression (" + node.getAttribute("test") + ") evaluates to true, executing loop.");\r
51                 }\r
52                 int numOutcomes = node.getNumOutcomes() + 1;\r
53                 for (int i = 0; i < numOutcomes; i++) {\r
54                     SvcLogicNode nextNode = node.getOutcomeValue("" + (i + 1));\r
55                     if (nextNode != null) {\r
56                         while (nextNode != null) {\r
57                             nextNode = svc.executeNode(nextNode, ctx);\r
58                         }\r
59                     } else {\r
60                         if ("true".equals(doWhile)) {\r
61                             LOG.debug("Do executed, will only execute again if test expression is true.");\r
62                             doWhile = "false";\r
63                         }\r
64                         testResult = evaluateNodeTest(node, ctx);\r
65                         LOG.debug("test expression (" + node.getAttribute("test") + ") evaluates to " + testResult);\r
66                     }\r
67                 }\r
68             }\r
69             LOG.debug("testResult was " + testResult + " which is not equal to true, exiting while loop.");\r
70         } catch (BreakNodeException e) {\r
71             LOG.debug("WhileNodeExecutor caught break");\r
72         }\r
73         return (null);\r
74     }\r
75 \r
76 }\r