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