SDNC-12 refactoring to support easy mocking
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / WhileNodeExecutor.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 WhileNodeExecutor extends SvcLogicNodeExecutor {
33
34     private static final Logger LOG = LoggerFactory.getLogger(WhileNodeExecutor.class);
35
36     @Override
37     public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
38
39         String testResult = evaluateNodeTest(node, ctx);
40         SvcLogicExpression silentFailureExpr = node.getAttribute("do");
41         String doWhile = SvcLogicExpressionResolver.evaluate(silentFailureExpr, node, ctx);
42         if ("true".equals(doWhile)) {
43             LOG.debug("While loop will execute once regardless of expression because do is set to true");
44         }
45
46         try {
47             while ("true".equals(testResult) || "true".equals(doWhile)) {
48                 if (!"true".equals(doWhile)) {
49                     LOG.debug("Test expression (" + node.getAttribute("test") + ") evaluates to true, executing loop.");
50                 }
51                 int numOutcomes = node.getNumOutcomes() + 1;
52                 for (int i = 0; i < numOutcomes; i++) {
53                     SvcLogicNode nextNode = node.getOutcomeValue("" + (i + 1));
54                     if (nextNode != null) {
55                         while (nextNode != null) {
56                             nextNode = svc.executeNode(nextNode, ctx);
57                         }
58                     } else {
59                         if ("true".equals(doWhile)) {
60                             LOG.debug("Do executed, will only execute again if test expression is true.");
61                             doWhile = "false";
62                         }
63                         testResult = evaluateNodeTest(node, ctx);
64                         LOG.debug("test expression (" + node.getAttribute("test") + ") evaluates to " + testResult);
65                     }
66                 }
67             }
68             LOG.debug("testResult was " + testResult + " which is not equal to true, exiting while loop.");
69         } catch (BreakNodeException e) {
70             LOG.debug("WhileNodeExecutor caught break");
71         }
72         return (null);
73     }
74
75 }