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());
72 put("break", new BreakNodeExecutor());
77 private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
79 private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
81 private static final Logger LOG = LoggerFactory
82 .getLogger(SvcLogicActivator.class);
84 private static Properties props = null;
86 private static BundleContext bundleCtx = null;
88 private static SvcLogicService svcLogicServiceImpl = null;
91 public void start(BundleContext ctx) throws Exception {
93 LOG.info("Activating SLI");
98 props = new Properties();
99 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
101 if (propPath == null) {
102 String propDir = System.getenv(SDNC_CONFIG_DIR);
103 if (propDir == null) {
105 propDir = "/opt/sdnc/data/properties";
107 propPath = propDir + "/svclogic.properties";
108 LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
111 File propFile = new File(propPath);
113 if (!propFile.exists()) {
115 throw new ConfigurationException(
116 "Missing configuration properties file : "
121 props.load(new FileInputStream(propFile));
122 } catch (Exception e) {
123 throw new ConfigurationException(
124 "Could not load properties file " + propPath, e);
129 if (registrations == null) {
131 registrations = new LinkedList<ServiceRegistration>();
134 // Advertise SvcLogicService
135 svcLogicServiceImpl = new SvcLogicServiceImpl();
137 LOG.info("SLI: Registering service " + SvcLogicService.NAME
138 + " in bundle " + ctx.getBundle().getSymbolicName());
139 ServiceRegistration reg = ctx.registerService(SvcLogicService.NAME,
140 svcLogicServiceImpl, null);
141 registrations.add(reg);
143 // Initialize SvcLogicStore
145 SvcLogicStore store = getStore();
146 registerNodeTypes(store);
147 } catch (ConfigurationException e) {
148 LOG.warn("Could not initialize SvcLogicScore", e);
151 LOG.info("SLI - done registering services");
155 public void stop(BundleContext ctx) throws Exception {
157 if (registrations != null) {
158 for (ServiceRegistration reg : registrations) {
159 ServiceReference regRef = reg.getReference();
160 /* Don't bother to remove node types from table
161 String nodeType = (String) regRef.getProperty("nodeType");
162 if (nodeType != null) {
163 LOG.info("SLI - unregistering node type " + nodeType);
164 store.unregisterNodeType(nodeType);
169 registrations = null;
173 public static SvcLogicStore getStore() throws SvcLogicException {
174 // Create and initialize SvcLogicStore object - used to access
175 // saved service logic.
177 SvcLogicStore store = null;
180 Driver dvr = new Driver();
181 store = SvcLogicStoreFactory.getSvcLogicStore(props);
182 } catch (Exception e) {
183 throw new ConfigurationException(
184 "Could not get service logic store", e);
190 } catch (Exception e) {
191 throw new ConfigurationException(
192 "Could not get service logic store", e);
198 private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
203 // Advertise built-in node executors
204 LOG.info("SLI : Registering built-in node executors");
205 Hashtable propTable = new Hashtable();
207 for (String nodeType : BUILTIN_NODES.keySet()) {
208 LOG.info("SLI - registering node type " + nodeType);
210 propTable.put("nodeType", nodeType);
212 ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
213 BUILTIN_NODES.get(nodeType), propTable);
214 registrations.add(reg);
216 store.registerNodeType(nodeType);
218 LOG.info("SLI - registering node executor");
220 ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));