Convert tabs to spaces
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / sli / adaptors / aai / AAIServiceActivator.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.onap.ccsdk.sli.adaptors.aai;
23
24 import java.io.File;
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;
31 import java.util.Set;
32
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;
39
40 public class AAIServiceActivator implements BundleActivator {
41
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";
45
46         private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
47
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";
50
51         private Set<ServiceRegistration> registrationSet = new HashSet<ServiceRegistration>();
52
53         private static final Logger LOG = LoggerFactory.getLogger(AAIServiceActivator.class);
54
55         @Override
56         public void start(BundleContext ctx) throws Exception {
57
58                 System.setProperty("aaiclient.runtime", "OSGI");
59
60                 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
61
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/";
67                 }
68
69                 LOG.debug("Configuration directory used : " + sdnConfigDirectory);
70
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.");
75                         return;
76                 }
77
78                 Properties properties = new Properties();
79                 InputStream input = null;
80
81                 // find aaiclient config file
82                 File[] files = findFiles(configDirectory, DEFAULT_CONFIG_FILE_NAME);
83
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() );
87                         try {
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);
93                         } finally {
94                                 if(input != null ) {
95                                         try {
96                                                 input.close();
97                                         } catch(Exception exc) {
98                                                 // ignore
99                                         }
100                                 }
101                                 int size = properties.keySet().size() ;
102                                 if(size == 0) {
103                                         LOG.debug(files[0].getAbsolutePath() + " contained no entries. Adding the default entry");
104                                         properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
105                                 }
106                         }
107                 } else {
108                         LOG.debug("No configuration entries were found. Adding the default entry");
109                         properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
110                 }
111
112                 Set<String> entrySet = properties. stringPropertyNames();
113                 String value = null;
114
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()) {
119
120                                 final String fileName = value;
121
122                                 File[] propertyFileList = findFiles(configDirectory, fileName);
123
124                                 for(File propertiesFile : propertyFileList) {
125                                         LOG.info(propertiesFile.getName());
126                                         // Advertise AAI resource adaptor
127                                         AAIClient impl = null;
128                                         switch(entry) {
129                                         case DEFAULT_KEYWORD:
130                                                 impl = new AAIService(propertiesFile.toURI().toURL());
131                                                 break;
132                                         case "trinity":
133                                                 impl = new AAITrinityService(propertiesFile.toURI().toURL());
134                                                 break;
135                                         default:
136                                                 LOG.error("Invalid configuration keyword '"+entry+"' detected in aaiclient.config. Aborting initialization");
137                                                 continue;
138                                         }
139                                         String regName = impl.getClass().getName();
140
141                                         LOG.debug("Registering AAIService service "+regName);
142                                         ServiceRegistration registration = ctx.registerService(regName, impl, null);
143                                         registrationSet.add(registration);
144
145                                 }
146                         }
147                 }
148         }
149
150 //      @Override
151         @Deprecated
152         public void start1(BundleContext ctx) throws Exception {
153
154                 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
155                 String propertiesPath = null;
156
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);
163                 } else {
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);
169                         } else {
170                                 throw new ConfigurationException("Cannot find config file - "+filename+" and "+SDNC_CONFIG_DIR+" is unset");
171                         }
172                 }
173         } else {
174                 propertiesPath = sdnConfigDirectory + "/aaiclient.properties";
175                 LOG.info("Environment variable " + SDNC_CONFIG_DIR + " set, - calculated path " + propertiesPath);
176         }
177
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);
185                 } else {
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);
191                         } else {
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.");
194                         }
195                 }
196                 }
197
198                 // Advertise AAI resource adaptor
199                 AAIClient impl = new AAIService(propFile.toURI().toURL());
200                 String regName = impl.getClass().getName();
201
202                 LOG.debug("Registering AAIService service "+regName);
203                 ServiceRegistration registration = ctx.registerService(regName, impl, null);
204                 registrationSet.add(registration);
205         }
206
207         @Override
208         public void stop(BundleContext ctx) throws Exception {
209
210                 Set<ServiceRegistration> localRegistrationSet = new HashSet<ServiceRegistration>();
211                 localRegistrationSet.addAll(registrationSet);
212
213                 for(ServiceRegistration registration : localRegistrationSet) {
214                         if (registration != null) {
215                                 try {
216                                         AAIService aaiService = (AAIService)ctx.getService(registration.getReference());
217                                 registration.unregister();
218                                 registrationSet.remove(registration);
219                                         if(aaiService != null) {
220                                                 aaiService.cleanUp();
221                                         }
222                                 } catch(Exception exc) {
223                                         if(LOG.isDebugEnabled())
224                                                 LOG.debug(exc.getMessage());
225                                 }
226                         }
227                 }
228         }
229
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);
234                     }
235                 });
236
237                 return files;
238         }
239 }