2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.adaptors.aai;
25 import java.io.FileInputStream;
26 import java.io.FilenameFilter;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.HashSet;
30 import java.util.Properties;
33 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
34 import org.osgi.framework.BundleActivator;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class AAIServiceActivator implements BundleActivator {
42 private static final String DEFAULT_CONFIG_FILE_NAME = "aaiclient.config";
43 private static final String DEFAULT_PROPERTY_FILE_NAME = "aaiclient.properties";
44 private static final String DEFAULT_KEYWORD = "default";
46 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
48 private static final String BVC_PROPERTY_FILE = "/opt/bvc/controller/configuration/aaiclient.properties";
49 private static final String DEFAULT_SDNC_PROPERTY_FILE = "/opt/sdnc/data/properties/aaiclient.properties";
51 private Set<ServiceRegistration> registrationSet = new HashSet<ServiceRegistration>();
53 private static final Logger LOG = LoggerFactory.getLogger(AAIServiceActivator.class);
56 public void start(BundleContext ctx) throws Exception {
58 System.setProperty("aaiclient.runtime", "OSGI");
60 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
62 // check SDNC CONFIG DIR system property
63 if(sdnConfigDirectory == null ) {
64 LOG.error("System property SDNC_CONFIG_DIR is not defined.");
65 LOG.info("Defaulting SDNC_CONFIG_DIR to '/opt/sdnc/data/properties/'");
66 sdnConfigDirectory = "/opt/sdnc/data/properties/";
69 LOG.debug("Configuration directory used : " + sdnConfigDirectory);
71 // check existance of properties directory
72 File configDirectory = new File(sdnConfigDirectory);
73 if(!configDirectory.exists() || !configDirectory.isDirectory()){
74 LOG.error("System property SDNC_CONFIG_DIR = '" + sdnConfigDirectory + "' does not point to a valid directory. AAIService will not be initialized.");
78 Properties properties = new Properties();
79 InputStream input = null;
81 // find aaiclient config file
82 File[] files = findFiles(configDirectory, DEFAULT_CONFIG_FILE_NAME);
84 // read the aai config data
85 if(files != null && files.length > 0) {
86 LOG.debug("AAIService config file exists and it is named :" + files[0].getAbsolutePath() );
88 input = new FileInputStream(files[0]);
89 properties.load(input);
90 LOG.debug("Loaded AAI Client properties from " + files[0].getAbsolutePath());
91 } catch (IOException exc) {
92 LOG.warn("Problem loading AAI Client properties from " + files[0].getAbsolutePath(), exc);
97 } catch(Exception exc) {
101 int size = properties.keySet().size() ;
103 LOG.debug(files[0].getAbsolutePath() + " contained no entries. Adding the default entry");
104 properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
108 LOG.debug("No configuration entries were found. Adding the default entry");
109 properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
112 Set<String> entrySet = properties. stringPropertyNames();
115 // initialize AAI Service for each aai client property files
116 for(String entry : entrySet) {
117 value = properties.getProperty(entry);
118 if(value != null && !value.isEmpty()) {
120 final String fileName = value;
122 File[] propertyFileList = findFiles(configDirectory, fileName);
124 for(File propertiesFile : propertyFileList) {
125 LOG.info(propertiesFile.getName());
126 // Advertise AAI resource adaptor
127 AAIClient impl = null;
129 case DEFAULT_KEYWORD:
130 impl = new AAIService(propertiesFile.toURI().toURL());
133 impl = new AAITrinityService(propertiesFile.toURI().toURL());
136 LOG.error("Invalid configuration keyword '"+entry+"' detected in aaiclient.config. Aborting initialization");
139 String regName = impl.getClass().getName();
141 LOG.debug("Registering AAIService service "+regName);
142 ServiceRegistration registration = ctx.registerService(regName, impl, null);
143 registrationSet.add(registration);
152 public void start1(BundleContext ctx) throws Exception {
154 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
155 String propertiesPath = null;
157 if (sdnConfigDirectory == null || sdnConfigDirectory.isEmpty()) {
158 String filename = DEFAULT_SDNC_PROPERTY_FILE;
159 File file = new File(filename);
160 if(file != null && file.exists()) {
161 propertiesPath = filename;
162 LOG.info("Using property file (1): " + propertiesPath);
164 filename = BVC_PROPERTY_FILE;
165 file = new File(filename);
166 if(file != null && file.exists()) {
167 propertiesPath = filename;
168 LOG.info("Using property file (1): " + propertiesPath);
170 throw new ConfigurationException("Cannot find config file - "+filename+" and "+SDNC_CONFIG_DIR+" is unset");
174 propertiesPath = sdnConfigDirectory + "/aaiclient.properties";
175 LOG.info("Environment variable " + SDNC_CONFIG_DIR + " set, - calculated path " + propertiesPath);
178 File propFile = new File(propertiesPath);
179 if(!propFile.exists()) {
180 String filename = DEFAULT_SDNC_PROPERTY_FILE;
181 File file = new File(filename);
182 if(file != null && file.exists()) {
183 propertiesPath = filename;
184 LOG.info("Using property file (1): " + propertiesPath);
186 filename = BVC_PROPERTY_FILE;
187 file = new File(filename);
188 if(file != null && file.exists()) {
189 propertiesPath = filename;
190 LOG.info("Using property file (1): " + propertiesPath);
192 LOG.error("AnAI Service Property file " + propertiesPath + "does not exist.");
193 throw new ConfigurationException("Cannot find config file - "+propertiesPath+" and " + SDNC_CONFIG_DIR + " is unset.");
198 // Advertise AAI resource adaptor
199 AAIClient impl = new AAIService(propFile.toURI().toURL());
200 String regName = impl.getClass().getName();
202 LOG.debug("Registering AAIService service "+regName);
203 ServiceRegistration registration = ctx.registerService(regName, impl, null);
204 registrationSet.add(registration);
208 public void stop(BundleContext ctx) throws Exception {
210 Set<ServiceRegistration> localRegistrationSet = new HashSet<ServiceRegistration>();
211 localRegistrationSet.addAll(registrationSet);
213 for(ServiceRegistration registration : localRegistrationSet) {
214 if (registration != null) {
216 AAIService aaiService = (AAIService)ctx.getService(registration.getReference());
217 registration.unregister();
218 registrationSet.remove(registration);
219 if(aaiService != null) {
220 aaiService.cleanUp();
222 } catch(Exception exc) {
223 if(LOG.isDebugEnabled())
224 LOG.debug(exc.getMessage());
230 private File[] findFiles(File configDirectory, final String filter) {
231 File[] files = configDirectory.listFiles(new FilenameFilter() {
232 public boolean accept(File dir, String name) {
233 return name.equalsIgnoreCase(filter);