2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.sli.provider;
25 import java.io.FileInputStream;
27 import java.util.HashMap;
28 import java.util.Hashtable;
29 import java.util.LinkedList;
31 import java.util.Properties;
33 import org.openecomp.sdnc.sli.ConfigurationException;
34 import org.openecomp.sdnc.sli.SvcLogicAdaptor;
35 import org.openecomp.sdnc.sli.SvcLogicException;
36 import org.openecomp.sdnc.sli.SvcLogicStore;
37 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
38 import org.osgi.framework.BundleActivator;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceReference;
41 import org.osgi.framework.ServiceRegistration;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.mysql.jdbc.Driver;
47 public class SvcLogicActivator implements BundleActivator {
49 private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
50 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
52 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
54 put("block", new BlockNodeExecutor());
55 put("call", new CallNodeExecutor());
56 put("configure", new ConfigureNodeExecutor());
57 put("delete", new DeleteNodeExecutor());
58 put("execute", new ExecuteNodeExecutor());
59 put("exists", new ExistsNodeExecutor());
60 put("for", new ForNodeExecutor());
61 put("get-resource", new GetResourceNodeExecutor());
62 put("is-available", new IsAvailableNodeExecutor());
63 put("notify", new NotifyNodeExecutor());
64 put("record", new RecordNodeExecutor());
65 put("release", new ReleaseNodeExecutor());
66 put("reserve", new ReserveNodeExecutor());
67 put("return", new ReturnNodeExecutor());
68 put("save", new SaveNodeExecutor());
69 put("set", new SetNodeExecutor());
70 put("switch", new SwitchNodeExecutor());
71 put("update", new UpdateNodeExecutor());
76 private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
78 private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
80 private static final Logger LOG = LoggerFactory
81 .getLogger(SvcLogicActivator.class);
83 private static Properties props = null;
85 private static BundleContext bundleCtx = null;
87 private static SvcLogicService svcLogicServiceImpl = null;
90 public void start(BundleContext ctx) throws Exception {
92 LOG.info("Activating SLI");
97 props = new Properties();
98 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
100 if (propPath == null) {
101 String propDir = System.getenv(SDNC_CONFIG_DIR);
102 if (propDir == null) {
104 propDir = "/opt/sdnc/data/properties";
106 propPath = propDir + "/svclogic.properties";
107 LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
110 File propFile = new File(propPath);
112 if (!propFile.exists()) {
114 throw new ConfigurationException(
115 "Missing configuration properties file : "
120 props.load(new FileInputStream(propFile));
121 } catch (Exception e) {
122 throw new ConfigurationException(
123 "Could not load properties file " + propPath, e);
128 if (registrations == null) {
130 registrations = new LinkedList<ServiceRegistration>();
133 // Advertise SvcLogicService
134 svcLogicServiceImpl = new SvcLogicServiceImpl();
136 LOG.info("SLI: Registering service " + SvcLogicService.NAME
137 + " in bundle " + ctx.getBundle().getSymbolicName());
138 ServiceRegistration reg = ctx.registerService(SvcLogicService.NAME,
139 svcLogicServiceImpl, null);
140 registrations.add(reg);
142 // Initialize SvcLogicStore
144 SvcLogicStore store = getStore();
145 registerNodeTypes(store);
146 } catch (ConfigurationException e) {
147 LOG.warn("Could not initialize SvcLogicScore", e);
150 LOG.info("SLI - done registering services");
154 public void stop(BundleContext ctx) throws Exception {
156 if (registrations != null) {
157 for (ServiceRegistration reg : registrations) {
158 ServiceReference regRef = reg.getReference();
159 /* Don't bother to remove node types from table
160 String nodeType = (String) regRef.getProperty("nodeType");
161 if (nodeType != null) {
162 LOG.info("SLI - unregistering node type " + nodeType);
163 store.unregisterNodeType(nodeType);
168 registrations = null;
172 public static SvcLogicStore getStore() throws SvcLogicException {
173 // Create and initialize SvcLogicStore object - used to access
174 // saved service logic.
176 SvcLogicStore store = null;
179 Driver dvr = new Driver();
180 store = SvcLogicStoreFactory.getSvcLogicStore(props);
181 } catch (Exception e) {
182 throw new ConfigurationException(
183 "Could not get service logic store", e);
189 } catch (Exception e) {
190 throw new ConfigurationException(
191 "Could not get service logic store", e);
197 private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
202 // Advertise built-in node executors
203 LOG.info("SLI : Registering built-in node executors");
204 Hashtable propTable = new Hashtable();
206 for (String nodeType : BUILTIN_NODES.keySet()) {
207 LOG.info("SLI - registering node type " + nodeType);
209 propTable.put("nodeType", nodeType);
211 ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
212 BUILTIN_NODES.get(nodeType), propTable);
213 registrations.add(reg);
215 store.registerNodeType(nodeType);
217 LOG.info("SLI - registering node executor");
219 ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));