f0bbda88f764cbb027a8b82a4e7a8fae0373e1fa
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / ReserveNodeExecutor.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.SvcLogicContext;
25 import org.openecomp.sdnc.sli.SvcLogicException;
26 import org.openecomp.sdnc.sli.SvcLogicExpression;
27 import org.openecomp.sdnc.sli.SvcLogicNode;
28 import org.openecomp.sdnc.sli.SvcLogicResource;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class ReserveNodeExecutor extends SvcLogicNodeExecutor {
33
34         private static final Logger LOG = LoggerFactory
35                         .getLogger(ReserveNodeExecutor.class);
36         
37         @Override
38         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
39                         SvcLogicContext ctx) throws SvcLogicException {
40
41                 String plugin = SvcLogicExpressionResolver.evaluate(
42                                 node.getAttribute("plugin"), node, ctx);
43                 String resourceType = SvcLogicExpressionResolver.evaluate(
44                                 node.getAttribute("resource"), node, ctx);
45                 String key = SvcLogicExpressionResolver.evaluateAsKey(
46                                 node.getAttribute("key"), node, ctx);
47                 String pfx = SvcLogicExpressionResolver.evaluate(node.getAttribute("pfx"),node,ctx);
48
49                 
50         SvcLogicExpression selectExpr = node.getAttribute("select");
51         String select = null;
52
53         if (selectExpr != null)
54         {
55                 select = SvcLogicExpressionResolver.evaluateAsKey(selectExpr, node, ctx);
56         }
57
58                 String outValue = "failure";
59
60                 if (LOG.isDebugEnabled()) {
61                         LOG.debug("reserve node encountered - looking for resource class "
62                                         + plugin);
63                 }
64
65
66
67         SvcLogicResource resourcePlugin = getSvcLogicResource(plugin);
68
69                         if (resourcePlugin != null) {
70
71                                 try {
72                                         switch (resourcePlugin.reserve(resourceType, select, key, pfx, ctx)) {
73                                         case SUCCESS:
74                                                 outValue = "success";
75                                                 break;
76                                         case NOT_FOUND:
77                                                 outValue = "not-found";
78                                                 break;
79                                         case FAILURE:
80                                         default:
81                                                 outValue = "failure";
82                                         }
83                                 } catch (SvcLogicException e) {
84                                         LOG.error("Caught exception from resource plugin", e);
85                                         outValue = "failure";
86                                 }
87                         } else {
88                                 LOG.warn("Could not find SvcLogicResource object for plugin "
89                                                 + plugin);
90                         }
91
92                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
93                 if (nextNode != null) {
94                         if (LOG.isDebugEnabled()) {
95                                 LOG.debug("about to execute " + outValue + " branch");
96                         }
97                         return (nextNode);
98                 }
99
100                 nextNode = node.getOutcomeValue("Other");
101                 if (nextNode != null) {
102                         if (LOG.isDebugEnabled()) {
103                                 LOG.debug("about to execute Other branch");
104                         }
105                 } else {
106                         if (LOG.isDebugEnabled()) {
107
108                                 LOG.debug("no "+outValue+" or Other branch found");
109                         }
110                 }
111                 return (nextNode);
112         }
113
114 }