Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli.provider;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.util.HashMap;
26 import java.util.Hashtable;
27 import java.util.LinkedList;
28 import java.util.Map;
29 import java.util.Properties;
30
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;
42
43 import com.mysql.jdbc.Driver;
44
45 public class SvcLogicActivator implements BundleActivator {
46
47         private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES";
48         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
49
50         private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
51                 {
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());
71
72                 }
73         };
74
75         private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
76
77         private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
78         
79         private static final Logger LOG = LoggerFactory
80                         .getLogger(SvcLogicActivator.class);
81         
82         private static Properties props = null;
83
84         private static BundleContext bundleCtx = null;
85         
86         private static SvcLogicService svcLogicServiceImpl = null;
87         
88         @Override
89         public void start(BundleContext ctx) throws Exception {
90
91                 LOG.info("Activating SLI");
92                 
93                 bundleCtx = ctx;
94
95                 // Read properties
96                 props = new Properties();
97                 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
98                 
99                 if (propPath == null) {
100                         String propDir = System.getenv(SDNC_CONFIG_DIR);
101                         if (propDir == null) {
102                                 
103                                 propDir = "/opt/sdnc/data/properties";
104                         }
105                         propPath = propDir + "/svclogic.properties";
106                         LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
107                 }
108                 
109                 File propFile = new File(propPath);
110                 
111                 if (!propFile.exists()) {
112                         
113                         throw new ConfigurationException(
114                                         "Missing configuration properties file : "
115                                                         + propFile);
116                 }
117                 try {
118                         
119                         props.load(new FileInputStream(propFile));
120                 } catch (Exception e) {
121                         throw new ConfigurationException(
122                                         "Could not load properties file " + propPath, e);
123
124                 }
125
126
127                 if (registrations == null) {
128
129                         registrations = new LinkedList<ServiceRegistration>();
130                 }
131
132                 // Advertise SvcLogicService
133                 svcLogicServiceImpl = new SvcLogicServiceImpl();
134
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);
140
141                 // Initialize SvcLogicStore
142                 try {
143                         SvcLogicStore store = getStore();
144                         registerNodeTypes(store);
145                 } catch (ConfigurationException e) {
146                         LOG.warn("Could not initialize SvcLogicScore", e);
147                 }
148
149                 LOG.info("SLI - done registering services");
150         }
151
152         @Override
153         public void stop(BundleContext ctx) throws Exception {
154
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);
163                                 }
164                                 */
165                                 reg.unregister();
166                         }
167                         registrations = null;
168                 }
169         }
170         
171         public static SvcLogicStore getStore() throws SvcLogicException {
172                 // Create and initialize SvcLogicStore object - used to access
173                 // saved service logic.
174                 
175                 SvcLogicStore store = null;
176                 
177                 try {
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);
183
184                 }
185
186                 try {
187                         store.init(props);
188                 } catch (Exception e) {
189                         throw new ConfigurationException(
190                                         "Could not get service logic store", e);
191                 }
192                 
193                 return(store);
194         }
195         
196         private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
197                 
198                 if (store == null) {
199                         return;
200                 }
201                 // Advertise built-in node executors
202                 LOG.info("SLI : Registering built-in node executors");
203                 Hashtable propTable = new Hashtable();
204
205                 for (String nodeType : BUILTIN_NODES.keySet()) {
206                         LOG.info("SLI - registering node type " + nodeType);
207                         propTable.clear();
208                         propTable.put("nodeType", nodeType);
209
210                         ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
211                                         BUILTIN_NODES.get(nodeType), propTable);
212                         registrations.add(reg);
213
214                         store.registerNodeType(nodeType);
215                         
216                         LOG.info("SLI - registering node executor");
217                         
218                         ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
219
220                 }
221                 
222         }
223
224 }