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