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