2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.core.sli.provider;
24 import java.io.FileInputStream;
25 import java.util.HashMap;
26 import java.util.Hashtable;
27 import java.util.LinkedList;
29 import java.util.Properties;
31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
36 import org.osgi.framework.BundleActivator;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.ServiceReference;
39 import org.osgi.framework.ServiceRegistration;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.mysql.jdbc.Driver;
45 public class SvcLogicActivator implements BundleActivator {
47 private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
48 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
50 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
52 put("block", new BlockNodeExecutor());
53 put("call", new CallNodeExecutor());
54 put("configure", new ConfigureNodeExecutor());
55 put("delete", new DeleteNodeExecutor());
56 put("execute", new ExecuteNodeExecutor());
57 put("exists", new ExistsNodeExecutor());
58 put("for", new ForNodeExecutor());
59 put("get-resource", new GetResourceNodeExecutor());
60 put("is-available", new IsAvailableNodeExecutor());
61 put("notify", new NotifyNodeExecutor());
62 put("record", new RecordNodeExecutor());
63 put("release", new ReleaseNodeExecutor());
64 put("reserve", new ReserveNodeExecutor());
65 put("return", new ReturnNodeExecutor());
66 put("save", new SaveNodeExecutor());
67 put("set", new SetNodeExecutor());
68 put("switch", new SwitchNodeExecutor());
69 put("update", new UpdateNodeExecutor());
70 put("break", new BreakNodeExecutor());
75 private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
77 private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
79 private static final Logger LOG = LoggerFactory
80 .getLogger(SvcLogicActivator.class);
82 private static Properties props = null;
84 private static BundleContext bundleCtx = null;
86 private static SvcLogicService svcLogicServiceImpl = null;
89 public void start(BundleContext ctx) throws Exception {
91 LOG.info("Activating SLI");
96 props = new Properties();
97 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
99 if (propPath == null) {
100 String propDir = System.getenv(SDNC_CONFIG_DIR);
101 if (propDir == null) {
103 propDir = "/opt/sdnc/data/properties";
105 propPath = propDir + "/svclogic.properties";
106 LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
109 File propFile = new File(propPath);
111 if (!propFile.exists()) {
113 throw new ConfigurationException(
114 "Missing configuration properties file : "
119 props.load(new FileInputStream(propFile));
120 } catch (Exception e) {
121 throw new ConfigurationException(
122 "Could not load properties file " + propPath, e);
127 if (registrations == null) {
129 registrations = new LinkedList<ServiceRegistration>();
132 // Advertise SvcLogicService
133 svcLogicServiceImpl = new SvcLogicServiceImpl();
135 LOG.info("SLI: Registering service " + SvcLogicService.NAME
136 + " in bundle " + ctx.getBundle().getSymbolicName());
137 ServiceRegistration reg = ctx.registerService(SvcLogicService.NAME,
138 svcLogicServiceImpl, null);
139 registrations.add(reg);
141 // Initialize SvcLogicStore
143 SvcLogicStore store = getStore();
144 registerNodeTypes(store);
145 } catch (ConfigurationException e) {
146 LOG.warn("Could not initialize SvcLogicScore", e);
149 LOG.info("SLI - done registering services");
153 public void stop(BundleContext ctx) throws Exception {
155 if (registrations != null) {
156 for (ServiceRegistration reg : registrations) {
157 ServiceReference regRef = reg.getReference();
158 /* Don't bother to remove node types from table
159 String nodeType = (String) regRef.getProperty("nodeType");
160 if (nodeType != null) {
161 LOG.info("SLI - unregistering node type " + nodeType);
162 store.unregisterNodeType(nodeType);
167 registrations = null;
171 public static SvcLogicStore getStore() throws SvcLogicException {
172 // Create and initialize SvcLogicStore object - used to access
173 // saved service logic.
175 SvcLogicStore store = null;
178 Driver dvr = new Driver();
179 store = SvcLogicStoreFactory.getSvcLogicStore(props);
180 } catch (Exception e) {
181 throw new ConfigurationException(
182 "Could not get service logic store", e);
188 } catch (Exception e) {
189 throw new ConfigurationException(
190 "Could not get service logic store", e);
196 private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
201 // Advertise built-in node executors
202 LOG.info("SLI : Registering built-in node executors");
203 Hashtable propTable = new Hashtable();
205 for (String nodeType : BUILTIN_NODES.keySet()) {
206 LOG.info("SLI - registering node type " + nodeType);
208 propTable.put("nodeType", nodeType);
210 ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
211 BUILTIN_NODES.get(nodeType), propTable);
212 registrations.add(reg);
214 store.registerNodeType(nodeType);
216 LOG.info("SLI - registering node executor");
218 ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));