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");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
\r
22 package org.openecomp.sdnc.sli.provider;
\r
24 import java.io.File;
\r
25 import java.io.FileInputStream;
\r
26 import java.net.URL;
\r
27 import java.util.HashMap;
\r
28 import java.util.Hashtable;
\r
29 import java.util.LinkedList;
\r
30 import java.util.Map;
\r
31 import java.util.Properties;
\r
33 import org.openecomp.sdnc.sli.ConfigurationException;
\r
34 import org.openecomp.sdnc.sli.SvcLogicAdaptor;
\r
35 import org.openecomp.sdnc.sli.SvcLogicException;
\r
36 import org.openecomp.sdnc.sli.SvcLogicStore;
\r
37 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
\r
38 import org.osgi.framework.BundleActivator;
\r
39 import org.osgi.framework.BundleContext;
\r
40 import org.osgi.framework.ServiceReference;
\r
41 import org.osgi.framework.ServiceRegistration;
\r
42 import org.slf4j.Logger;
\r
43 import org.slf4j.LoggerFactory;
\r
45 import com.mysql.jdbc.Driver;
\r
47 public class SvcLogicActivator implements BundleActivator {
\r
49 private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
\r
50 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
\r
52 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
\r
54 put("block", new BlockNodeExecutor());
\r
55 put("call", new CallNodeExecutor());
\r
56 put("configure", new ConfigureNodeExecutor());
\r
57 put("delete", new DeleteNodeExecutor());
\r
58 put("execute", new ExecuteNodeExecutor());
\r
59 put("exists", new ExistsNodeExecutor());
\r
60 put("for", new ForNodeExecutor());
\r
61 put("get-resource", new GetResourceNodeExecutor());
\r
62 put("is-available", new IsAvailableNodeExecutor());
\r
63 put("notify", new NotifyNodeExecutor());
\r
64 put("record", new RecordNodeExecutor());
\r
65 put("release", new ReleaseNodeExecutor());
\r
66 put("reserve", new ReserveNodeExecutor());
\r
67 put("return", new ReturnNodeExecutor());
\r
68 put("save", new SaveNodeExecutor());
\r
69 put("set", new SetNodeExecutor());
\r
70 put("switch", new SwitchNodeExecutor());
\r
71 put("update", new UpdateNodeExecutor());
\r
72 put("break", new BreakNodeExecutor());
\r
73 put("while", new WhileNodeExecutor());
\r
77 private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
\r
79 private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
\r
81 private static final Logger LOG = LoggerFactory
\r
82 .getLogger(SvcLogicActivator.class);
\r
84 private static Properties props = null;
\r
86 private static BundleContext bundleCtx = null;
\r
88 private static SvcLogicService svcLogicServiceImpl = null;
\r
91 public void start(BundleContext ctx) throws Exception {
\r
93 LOG.info("Activating SLI");
\r
98 props = new Properties();
\r
99 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
\r
101 if (propPath == null) {
\r
102 String propDir = System.getenv(SDNC_CONFIG_DIR);
\r
103 if (propDir == null) {
\r
105 propDir = "/opt/sdnc/data/properties";
\r
107 propPath = propDir + "/svclogic.properties";
\r
108 LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
\r
111 File propFile = new File(propPath);
\r
113 if (!propFile.exists()) {
\r
115 throw new ConfigurationException(
\r
116 "Missing configuration properties file : "
\r
121 props.load(new FileInputStream(propFile));
\r
122 } catch (Exception e) {
\r
123 throw new ConfigurationException(
\r
124 "Could not load properties file " + propPath, e);
\r
129 if (registrations == null) {
\r
131 registrations = new LinkedList<ServiceRegistration>();
\r
134 // Advertise SvcLogicService
\r
135 svcLogicServiceImpl = new SvcLogicServiceImpl();
\r
137 LOG.info("SLI: Registering service " + SvcLogicService.NAME
\r
138 + " in bundle " + ctx.getBundle().getSymbolicName());
\r
139 ServiceRegistration reg = ctx.registerService(SvcLogicService.NAME,
\r
140 svcLogicServiceImpl, null);
\r
141 registrations.add(reg);
\r
143 // Initialize SvcLogicStore
\r
145 SvcLogicStore store = getStore();
\r
146 registerNodeTypes(store);
\r
147 } catch (ConfigurationException e) {
\r
148 LOG.warn("Could not initialize SvcLogicScore", e);
\r
151 LOG.info("SLI - done registering services");
\r
155 public void stop(BundleContext ctx) throws Exception {
\r
157 if (registrations != null) {
\r
158 for (ServiceRegistration reg : registrations) {
\r
159 ServiceReference regRef = reg.getReference();
\r
160 /* Don't bother to remove node types from table
\r
161 String nodeType = (String) regRef.getProperty("nodeType");
\r
162 if (nodeType != null) {
\r
163 LOG.info("SLI - unregistering node type " + nodeType);
\r
164 store.unregisterNodeType(nodeType);
\r
169 registrations = null;
\r
173 public static SvcLogicStore getStore() throws SvcLogicException {
\r
174 // Create and initialize SvcLogicStore object - used to access
\r
175 // saved service logic.
\r
177 SvcLogicStore store = null;
\r
180 Driver dvr = new Driver();
\r
181 store = SvcLogicStoreFactory.getSvcLogicStore(props);
\r
182 } catch (Exception e) {
\r
183 throw new ConfigurationException(
\r
184 "Could not get service logic store", e);
\r
190 } catch (Exception e) {
\r
191 throw new ConfigurationException(
\r
192 "Could not get service logic store", e);
\r
198 private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
\r
200 if (store == null) {
\r
203 // Advertise built-in node executors
\r
204 LOG.info("SLI : Registering built-in node executors");
\r
205 Hashtable propTable = new Hashtable();
\r
207 for (String nodeType : BUILTIN_NODES.keySet()) {
\r
208 LOG.info("SLI - registering node type " + nodeType);
\r
210 propTable.put("nodeType", nodeType);
\r
212 ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
\r
213 BUILTIN_NODES.get(nodeType), propTable);
\r
214 registrations.add(reg);
\r
216 store.registerNodeType(nodeType);
\r
218 LOG.info("SLI - registering node executor");
\r
220 ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
\r