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