Refactor dblib
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / openecomp / sdnc / sli / SliPluginUtils / SliPluginUtilsActivator.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.openecomp.sdnc.sli.SliPluginUtils;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Properties;
30
31 import org.osgi.framework.BundleActivator;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.ServiceRegistration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class SliPluginUtilsActivator implements BundleActivator {
38     @SuppressWarnings("rawtypes") private List<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
39
40     private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtilsActivator.class);
41     private static final String SDNC_ROOT_DIR = "SDNC_CONFIG_DIR";
42     private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
43
44     @Override
45     public void start(BundleContext ctx) throws Exception {
46         SliPluginUtils plugin = new SliPluginUtils(new Properties());
47         LOG.info("Registering service " + plugin.getClass().getName());
48         registrations.add(ctx.registerService(plugin.getClass().getName(), plugin, null));
49
50         SliStringUtils sliStringUtils_Plugin = new SliStringUtils();
51         LOG.info("Registering service " + sliStringUtils_Plugin.getClass().getName());
52         registrations.add(ctx.registerService(sliStringUtils_Plugin.getClass().getName(), sliStringUtils_Plugin, null));
53
54         try {
55             String path = System.getenv(SDNC_ROOT_DIR) + File.separator + DME2_PROPERTIES_FILE_NAME;
56             DME2 dmePlugin = initDme2(path);
57             if (dmePlugin != null) {
58                 LOG.info("Registering service " + dmePlugin.getClass().getName());
59                 registrations.add(ctx.registerService(dmePlugin.getClass().getName(), dmePlugin, null));
60             }
61         } catch (Exception e) {
62             LOG.error("DME2 plugin could not be started", e);
63         }
64     }
65
66     public DME2 initDme2(String pathToDmeProperties) {
67         Properties dme2properties = new Properties();
68         String loadPropertiesErrorMessage = "Couldn't load DME2 properties at path " + pathToDmeProperties;
69         File dme2propertiesFile = new File(pathToDmeProperties);
70
71         try {
72             dme2properties.load(new FileReader(dme2propertiesFile));
73             String proxyUrlProperty = dme2properties.getProperty("proxyUrl");
74             String[] proxyUrls = proxyUrlProperty.split(",");
75             DME2 dmePlugin = new DME2(dme2properties.getProperty("aafUserName"), dme2properties.getProperty("aafPassword"), dme2properties.getProperty("envContext"), dme2properties.getProperty("routeOffer"), proxyUrls, dme2properties.getProperty("commonServiceVersion"));
76             dmePlugin.setPartner(dme2properties.getProperty("partner"));
77             return dmePlugin;
78         } catch (FileNotFoundException e) {
79             LOG.error(loadPropertiesErrorMessage);
80         } catch (IOException e) {
81             LOG.error(loadPropertiesErrorMessage);
82         }
83         LOG.error("Couldn't create DME2 plugin");
84         return null;
85     }
86
87     @Override
88     public void stop(BundleContext ctx) throws Exception {
89         for (@SuppressWarnings("rawtypes") ServiceRegistration registration : registrations) {
90             registration.unregister();
91             registration = null;
92         }
93     }
94 }