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