2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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();
80 // find aaiclient config file
81 File[] files = findFiles(configDirectory, DEFAULT_CONFIG_FILE_NAME);
83 // read the aai config data
84 if(files != null && files.length > 0) {
85 LOG.debug("AAIService config file exists and it is named :" + files[0].getAbsolutePath() );
86 try ( InputStream input = new FileInputStream(files[0])) {
87 properties.load(input);
88 LOG.debug("Loaded AAI Client properties from " + files[0].getAbsolutePath());
89 } catch (IOException exc) {
90 LOG.warn("Problem loading AAI Client properties from " + files[0].getAbsolutePath(), exc);
92 int size = properties.keySet().size() ;
94 LOG.debug(files[0].getAbsolutePath() + " contained no entries. Adding the default entry");
95 properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
99 LOG.debug("No configuration entries were found. Adding the default entry");
100 properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
103 Set<String> entrySet = properties. stringPropertyNames();
106 // initialize AAI Service for each aai client property files
107 for(String entry : entrySet) {
108 value = properties.getProperty(entry);
109 if(value != null && !value.isEmpty()) {
111 final String fileName = value;
113 File[] propertyFileList = findFiles(configDirectory, fileName);
115 for(File propertiesFile : propertyFileList) {
116 LOG.info(propertiesFile.getName());
117 // Advertise AAI resource adaptor
118 AAIClient impl = null;
120 case DEFAULT_KEYWORD:
121 impl = new AAIService(propertiesFile.toURI().toURL());
124 impl = new AAITrinityService(propertiesFile.toURI().toURL());
127 LOG.error("Invalid configuration keyword '"+entry+"' detected in aaiclient.config. Aborting initialization");
130 String regName = impl.getClass().getName();
132 LOG.debug("Registering AAIService service "+regName);
133 ServiceRegistration registration = ctx.registerService(regName, impl, null);
134 registrationSet.add(registration);
143 public void start1(BundleContext ctx) throws Exception {
145 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
146 String propertiesPath = null;
148 if (sdnConfigDirectory == null || sdnConfigDirectory.isEmpty()) {
149 String filename = DEFAULT_SDNC_PROPERTY_FILE;
150 File file = new File(filename);
152 propertiesPath = filename;
153 LOG.info("Using property file (1): " + propertiesPath);
155 filename = BVC_PROPERTY_FILE;
156 file = new File(filename);
158 propertiesPath = filename;
159 LOG.info("Using property file (1): " + propertiesPath);
161 throw new ConfigurationException("Cannot find config file - "+filename+" and "+SDNC_CONFIG_DIR+" is unset");
165 propertiesPath = sdnConfigDirectory + "/aaiclient.properties";
166 LOG.info("Environment variable " + SDNC_CONFIG_DIR + " set, - calculated path " + propertiesPath);
169 File propFile = new File(propertiesPath);
170 if(!propFile.exists()) {
171 String filename = DEFAULT_SDNC_PROPERTY_FILE;
172 File file = new File(filename);
174 propertiesPath = filename;
175 LOG.info("Using property file (1): " + propertiesPath);
177 filename = BVC_PROPERTY_FILE;
178 file = new File(filename);
180 propertiesPath = filename;
181 LOG.info("Using property file (1): " + propertiesPath);
183 LOG.error("AnAI Service Property file " + propertiesPath + "does not exist.");
184 throw new ConfigurationException("Cannot find config file - "+propertiesPath+" and " + SDNC_CONFIG_DIR + " is unset.");
189 // Advertise AAI resource adaptor
190 AAIClient impl = new AAIService(propFile.toURI().toURL());
191 String regName = impl.getClass().getName();
193 LOG.debug("Registering AAIService service "+regName);
194 ServiceRegistration registration = ctx.registerService(regName, impl, null);
195 registrationSet.add(registration);
199 public void stop(BundleContext ctx) throws Exception {
201 Set<ServiceRegistration> localRegistrationSet = new HashSet<ServiceRegistration>();
202 localRegistrationSet.addAll(registrationSet);
204 for(ServiceRegistration registration : localRegistrationSet) {
205 if (registration != null) {
207 AAIService aaiService = (AAIService)ctx.getService(registration.getReference());
208 registration.unregister();
209 registrationSet.remove(registration);
210 if(aaiService != null) {
211 aaiService.cleanUp();
213 } catch(Exception exc) {
214 if(LOG.isDebugEnabled())
215 LOG.debug(exc.getMessage());
221 private File[] findFiles(File configDirectory, final String filter) {
222 File[] files = configDirectory.listFiles(new FilenameFilter() {
223 public boolean accept(File dir, String name) {
224 return name.equalsIgnoreCase(filter);