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