9be03e9d48bdbd05cbec650d77c763a36fcd7672
[ccsdk/sli/adaptors.git] / mdsal-resource / provider / src / main / java / org / openecomp / sdnc / sli / resource / mdsal / MdsalResourceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.resource.mdsal;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.LinkedList;
27 import java.util.Properties;
28
29 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
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 MdsalResourceActivator implements BundleActivator {
38
39
40
41     private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
42
43     public LinkedList<ServiceRegistration> registrations = new LinkedList<ServiceRegistration>();
44
45     private static final Logger LOG = LoggerFactory
46             .getLogger(MdsalResourceActivator.class);
47
48     @Override
49     public void start(BundleContext ctx) throws Exception {
50
51         // Read properties
52         Properties props = new Properties();
53
54         String propDir = System.getenv(SDNC_CONFIG_DIR);
55         if (propDir == null) {
56
57             propDir = "/opt/sdnc/data/properties";
58         }
59         String propPath = propDir + "/mdsal-resource.properties";
60
61
62         File propFile = new File(propPath);
63
64         if (!propFile.exists()) {
65
66             throw new ConfigurationException(
67                     "Missing configuration properties file : "
68                             + propFile);
69         }
70         try {
71
72             props.load(new FileInputStream(propFile));
73         } catch (Exception e) {
74             throw new ConfigurationException(
75                     "Could not load properties file " + propPath, e);
76
77         }
78
79         String sdncUser = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-user", "admin");
80         String sdncPasswd = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-passwd", "admin");
81         String sdncHost = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-host", "localhost");
82         String sdncProtocol = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-protocol", "https");
83         String sdncPort = props.getProperty("org.openecomp.sdnc.sli.resource.mdsal.sdnc-port", "8443");
84
85         // Advertise MD-SAL resource adaptors
86         SvcLogicResource impl = new ConfigResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
87
88         LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
89         registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
90
91         impl = new OperationalResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
92
93         LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
94         registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
95     }
96
97     @Override
98     public void stop(BundleContext ctx) throws Exception {
99
100         for (ServiceRegistration registration : registrations)
101         {
102             registration.unregister();
103         }
104     }
105
106 }