Roll to initial Honolulu release version
[ccsdk/sli/adaptors.git] / mdsal-resource / provider / src / main / java / org / onap / ccsdk / sli / adaptors / resource / mdsal / MdsalResourceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.adaptors.resource.mdsal;
25  
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.util.LinkedList;
29 import java.util.Properties;
30
31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.osgi.framework.BundleActivator;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.ServiceRegistration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class MdsalResourceActivator implements BundleActivator {
40
41
42
43     private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
44
45     public LinkedList<ServiceRegistration> registrations = new LinkedList<>();
46
47     private static final Logger LOG = LoggerFactory
48             .getLogger(MdsalResourceActivator.class);
49
50     @Override
51     public void start(BundleContext ctx) throws Exception {
52
53         // Read properties
54         Properties props = new Properties();
55
56         String propDir = System.getenv(SDNC_CONFIG_DIR);
57         if (propDir == null) {
58
59             propDir = "/opt/sdnc/data/properties";
60         }
61         String propPath = propDir + "/mdsal-resource.properties";
62
63
64         File propFile = new File(propPath);
65
66         if (!propFile.exists()) {
67
68             throw new ConfigurationException(
69                     "Missing configuration properties file : "
70                             + propFile);
71         }
72         try {
73
74             props.load(new FileInputStream(propFile));
75         } catch (Exception e) {
76             throw new ConfigurationException(
77                     "Could not load properties file " + propPath, e);
78
79         }
80
81         String sdncUser = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-user", "admin");
82         String sdncPasswd = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-passwd", "admin");
83         String sdncHost = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-host", "localhost");
84         String sdncProtocol = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-protocol", "https");
85         String sdncPort = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-port", "8443");
86
87         // Advertise MD-SAL resource adaptors
88         SvcLogicResource impl = new ConfigResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
89
90         LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
91         registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
92
93         impl = new OperationalResource(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd);
94
95         LOG.debug("Registering MdsalResource service "+impl.getClass().getName());
96         registrations.add(ctx.registerService(impl.getClass().getName(), impl, null));
97     }
98
99     @Override
100     public void stop(BundleContext ctx) throws Exception {
101
102         for (ServiceRegistration registration : registrations)
103         {
104             registration.unregister();
105         }
106     }
107
108 }