0c8166ae17221969308c39b2b5ee057327f3ccf3
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / SvcLogicNodeExecutor.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.SvcLogicAdaptor;
25 import org.openecomp.sdnc.sli.SvcLogicContext;
26 import org.openecomp.sdnc.sli.SvcLogicException;
27 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
28 import org.openecomp.sdnc.sli.SvcLogicNode;
29 import org.openecomp.sdnc.sli.SvcLogicRecorder;
30 import org.openecomp.sdnc.sli.SvcLogicResource;
31 import org.openecomp.sdnc.sli.SvcLogicStore;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34 import org.osgi.framework.ServiceReference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public abstract class SvcLogicNodeExecutor {
39         
40         public abstract SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException;
41
42     private static final Logger LOG = LoggerFactory.getLogger(SvcLogicNodeExecutor.class);
43
44     protected String evaluateNodeTest(SvcLogicNode node, SvcLogicContext ctx)
45                         throws SvcLogicException {
46                 if (node == null) {
47                         return null;
48                 }
49
50                 return (SvcLogicExpressionResolver.evaluate(node.getAttribute("test"),
51                                 node, ctx));
52
53         }
54         
55     protected SvcLogicStore getStore() throws SvcLogicException {
56         return SvcLogicActivator.getStore();
57     }
58     
59     protected SvcLogicAdaptor getAdaptor(String adaptorName) {
60         return SvcLogicAdaptorFactory.getInstance(adaptorName);
61     }
62     
63     protected SvcLogicResource getSvcLogicResource(String plugin) {
64         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
65                 .getBundleContext();
66
67         ServiceReference sref = bctx.getServiceReference(plugin);
68         if (sref != null) {
69             SvcLogicResource resourcePlugin = (SvcLogicResource) bctx
70                     .getService(sref);
71             return resourcePlugin;
72         }
73         else {
74             LOG.warn("Could not find service reference object for plugin " + plugin);
75             return null;
76         }
77     }
78     
79     protected SvcLogicRecorder getSvcLogicRecorder(String plugin) {
80         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
81                 .getBundleContext();
82
83         ServiceReference sref = bctx.getServiceReference(plugin);
84         if (sref != null) {
85             SvcLogicRecorder resourcePlugin = (SvcLogicRecorder) bctx
86                     .getService(sref);
87             return resourcePlugin;
88         }
89         else {
90             return null;
91         }
92     }
93     
94     protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){
95         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
96                  .getBundleContext();
97
98          ServiceReference sref = bctx.getServiceReference(pluginName);
99
100          if (sref == null) {
101              LOG.warn("Could not find service reference object for plugin " + pluginName);
102              return null;
103          } else {
104              SvcLogicJavaPlugin plugin  = (SvcLogicJavaPlugin) bctx
105                      .getService(sref);
106              return plugin;
107          }
108  }
109     
110 }