62c6fdeaf16ca7eec1995bee0ba7b1b0b346a6ec
[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
73                 }
74         };
75
76         private static LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
77
78         private static HashMap<String, SvcLogicAdaptor> adaptorMap = null;
79         
80         private static final Logger LOG = LoggerFactory
81                         .getLogger(SvcLogicActivator.class);
82         
83         private static Properties props = null;
84
85         private static BundleContext bundleCtx = null;
86         
87         private static SvcLogicService svcLogicServiceImpl = null;
88         
89         @Override
90         public void start(BundleContext ctx) throws Exception {
91
92                 LOG.info("Activating SLI");
93                 
94                 bundleCtx = ctx;
95
96                 // Read properties
97                 props = new Properties();
98                 String propPath = System.getenv(SVCLOGIC_PROP_VAR);
99                 
100                 if (propPath == null) {
101                         String propDir = System.getenv(SDNC_CONFIG_DIR);
102                         if (propDir == null) {
103                                 
104                                 propDir = "/opt/sdnc/data/properties";
105                         }
106                         propPath = propDir + "/svclogic.properties";
107                         LOG.warn("Environment variable "+SVCLOGIC_PROP_VAR+" unset - defaulting to "+propPath);
108                 }
109                 
110                 File propFile = new File(propPath);
111                 
112                 if (!propFile.exists()) {
113                         
114                         throw new ConfigurationException(
115                                         "Missing configuration properties file : "
116                                                         + propFile);
117                 }
118                 try {
119                         
120                         props.load(new FileInputStream(propFile));
121                 } catch (Exception e) {
122                         throw new ConfigurationException(
123                                         "Could not load properties file " + propPath, e);
124
125                 }
126
127
128                 if (registrations == null) {
129
130                         registrations = new LinkedList<ServiceRegistration>();
131                 }
132
133                 // Advertise SvcLogicService
134                 svcLogicServiceImpl = new SvcLogicServiceImpl();
135
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);
141
142                 // Initialize SvcLogicStore
143                 try {
144                         SvcLogicStore store = getStore();
145                         registerNodeTypes(store);
146                 } catch (ConfigurationException e) {
147                         LOG.warn("Could not initialize SvcLogicScore", e);
148                 }
149
150                 LOG.info("SLI - done registering services");
151         }
152
153         @Override
154         public void stop(BundleContext ctx) throws Exception {
155
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);
164                                 }
165                                 */
166                                 reg.unregister();
167                         }
168                         registrations = null;
169                 }
170         }
171         
172         public static SvcLogicStore getStore() throws SvcLogicException {
173                 // Create and initialize SvcLogicStore object - used to access
174                 // saved service logic.
175                 
176                 SvcLogicStore store = null;
177                 
178                 try {
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);
184
185                 }
186
187                 try {
188                         store.init(props);
189                 } catch (Exception e) {
190                         throw new ConfigurationException(
191                                         "Could not get service logic store", e);
192                 }
193                 
194                 return(store);
195         }
196         
197         private static void registerNodeTypes(SvcLogicStore store) throws SvcLogicException {
198                 
199                 if (store == null) {
200                         return;
201                 }
202                 // Advertise built-in node executors
203                 LOG.info("SLI : Registering built-in node executors");
204                 Hashtable propTable = new Hashtable();
205
206                 for (String nodeType : BUILTIN_NODES.keySet()) {
207                         LOG.info("SLI - registering node type " + nodeType);
208                         propTable.clear();
209                         propTable.put("nodeType", nodeType);
210
211                         ServiceRegistration reg = bundleCtx.registerService(SvcLogicNodeExecutor.class.getName(),
212                                         BUILTIN_NODES.get(nodeType), propTable);
213                         registrations.add(reg);
214
215                         store.registerNodeType(nodeType);
216                         
217                         LOG.info("SLI - registering node executor");
218                         
219                         ((SvcLogicServiceImpl)svcLogicServiceImpl).registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
220
221                 }
222                 
223         }
224
225 }