e1fa90709fb56466d5a1ab7c810cdd34a246c677
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / AAIServiceActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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
80                 // find aaiclient config file
81                 File[] files = findFiles(configDirectory, DEFAULT_CONFIG_FILE_NAME);
82
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);
91                         } finally {
92                                 int size = properties.keySet().size() ;
93                                 if(size == 0) {
94                                         LOG.debug(files[0].getAbsolutePath() + " contained no entries. Adding the default entry");
95                                         properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
96                                 }
97                         }
98                 } else {
99                         LOG.debug("No configuration entries were found. Adding the default entry");
100                         properties.put(DEFAULT_KEYWORD, DEFAULT_PROPERTY_FILE_NAME);
101                 }
102
103                 Set<String> entrySet = properties. stringPropertyNames();
104                 String value = null;
105
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()) {
110
111                                 final String fileName = value;
112
113                                 File[] propertyFileList = findFiles(configDirectory, fileName);
114
115                                 for(File propertiesFile : propertyFileList) {
116                                         LOG.info(propertiesFile.getName());
117                                         // Advertise AAI resource adaptor
118                                         AAIClient impl = null;
119                                         switch(entry) {
120                                         case DEFAULT_KEYWORD:
121                                                 impl = new AAIService(propertiesFile.toURI().toURL());
122                                                 break;
123                                         case "trinity":
124                                                 impl = new AAITrinityService(propertiesFile.toURI().toURL());
125                                                 break;
126                                         default:
127                                                 LOG.error("Invalid configuration keyword '"+entry+"' detected in aaiclient.config. Aborting initialization");
128                                                 continue;
129                                         }
130                                         String regName = impl.getClass().getName();
131
132                                         LOG.debug("Registering AAIService service "+regName);
133                                         ServiceRegistration registration = ctx.registerService(regName, impl, null);
134                                         registrationSet.add(registration);
135
136                                 }
137                         }
138                 }
139         }
140
141 //      @Override
142         @Deprecated
143         public void start1(BundleContext ctx) throws Exception {
144
145                 String sdnConfigDirectory = System.getenv(SDNC_CONFIG_DIR);
146                 String propertiesPath = null;
147
148                 if (sdnConfigDirectory == null || sdnConfigDirectory.isEmpty()) {
149                         String filename = DEFAULT_SDNC_PROPERTY_FILE;
150                 File file = new File(filename);
151                 if(file.exists()) {
152                         propertiesPath = filename;
153                         LOG.info("Using property file (1): " + propertiesPath);
154                 } else {
155                         filename = BVC_PROPERTY_FILE;
156                         file = new File(filename);
157                         if(file.exists()) {
158                                 propertiesPath = filename;
159                                 LOG.info("Using property file (1): " + propertiesPath);
160                         } else {
161                                 throw new ConfigurationException("Cannot find config file - "+filename+" and "+SDNC_CONFIG_DIR+" is unset");
162                         }
163                 }
164         } else {
165                 propertiesPath = sdnConfigDirectory + "/aaiclient.properties";
166                 LOG.info("Environment variable " + SDNC_CONFIG_DIR + " set, - calculated path " + propertiesPath);
167         }
168
169                 File propFile = new File(propertiesPath);
170                 if(!propFile.exists()) {
171                         String filename = DEFAULT_SDNC_PROPERTY_FILE;
172                 File file = new File(filename);
173                 if(file.exists()) {
174                         propertiesPath = filename;
175                         LOG.info("Using property file (1): " + propertiesPath);
176                 } else {
177                         filename = BVC_PROPERTY_FILE;
178                         file = new File(filename);
179                         if(file.exists()) {
180                                 propertiesPath = filename;
181                                 LOG.info("Using property file (1): " + propertiesPath);
182                         } else {
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.");
185                         }
186                 }
187                 }
188
189                 // Advertise AAI resource adaptor
190                 AAIClient impl = new AAIService(propFile.toURI().toURL());
191                 String regName = impl.getClass().getName();
192
193                 LOG.debug("Registering AAIService service "+regName);
194                 ServiceRegistration registration = ctx.registerService(regName, impl, null);
195                 registrationSet.add(registration);
196         }
197
198         @Override
199         public void stop(BundleContext ctx) throws Exception {
200
201                 Set<ServiceRegistration> localRegistrationSet = new HashSet<ServiceRegistration>();
202                 localRegistrationSet.addAll(registrationSet);
203
204                 for(ServiceRegistration registration : localRegistrationSet) {
205                         if (registration != null) {
206                                 try {
207                                         AAIService aaiService = (AAIService)ctx.getService(registration.getReference());
208                                 registration.unregister();
209                                 registrationSet.remove(registration);
210                                         if(aaiService != null) {
211                                                 aaiService.cleanUp();
212                                         }
213                                 } catch(Exception exc) {
214                                         if(LOG.isDebugEnabled())
215                                                 LOG.debug(exc.getMessage());
216                                 }
217                         }
218                 }
219         }
220
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);
225                     }
226                 });
227
228                 return files;
229         }
230 }