Refactor dblib
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli.provider;
22
23 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
24 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.FrameworkUtil;
33 import org.osgi.framework.ServiceReference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public abstract class SvcLogicNodeExecutor {
38         
39         public abstract SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException;
40
41     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicNodeExecutor.class);
42
43     protected String evaluateNodeTest(SvcLogicNode node, SvcLogicContext ctx)
44                         throws SvcLogicException {
45                 if (node == null) {
46                         return null;
47                 }
48
49                 return (SvcLogicExpressionResolver.evaluate(node.getAttribute("test"),
50                                 node, ctx));
51
52         }
53         
54     protected SvcLogicStore getStore() throws SvcLogicException {
55         return SvcLogicActivator.getStore();
56     }
57     
58     protected SvcLogicAdaptor getAdaptor(String adaptorName) {
59         return SvcLogicAdaptorFactory.getInstance(adaptorName);
60     }
61     
62     protected SvcLogicResource getSvcLogicResource(String plugin) {
63         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
64                 .getBundleContext();
65
66         ServiceReference sref = bctx.getServiceReference(plugin);
67         if (sref != null) {
68             SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
69                     .getService(sref);
70             return resourcePlugin;
71         }
72         else {
73             LOG.warn("Could not find service reference object for plugin " + plugin);
74             return null;
75         }
76     }
77     
78     protected SvcLogicRecorder getSvcLogicRecorder(String plugin) {
79         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
80                 .getBundleContext();
81
82         ServiceReference sref = bctx.getServiceReference(plugin);
83         if (sref != null) {
84             SvcLogicRecorder resourcePlugin = (SvcLogicRecorder) bctx
85                     .getService(sref);
86             return resourcePlugin;
87         }
88         else {
89             return null;
90         }
91     }
92     
93     protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){
94         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
95                  .getBundleContext();
96
97          ServiceReference sref = bctx.getServiceReference(pluginName);
98
99          if (sref == null) {
100              LOG.warn("Could not find service reference object for plugin " + pluginName);
101              return null;
102          } else {
103              SvcLogicJavaPlugin plugin  = (SvcLogicJavaPlugin) bctx
104                      .getService(sref);
105              return plugin;
106          }
107  }
108     
109 }