2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.cloud;
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
29 import javax.ejb.ConcurrencyManagement;
30 import javax.ejb.ConcurrencyManagementType;
31 import javax.ejb.LocalBean;
32 import javax.ejb.Schedule;
33 import javax.ejb.Singleton;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Response;
40 import org.codehaus.jackson.JsonParseException;
41 import org.codehaus.jackson.map.JsonMappingException;
43 import org.openecomp.mso.logger.MsoLogger;
44 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
45 import org.openecomp.mso.openstack.utils.MsoHeatUtils;
46 import org.openecomp.mso.openstack.utils.MsoKeystoneUtils;
47 import org.openecomp.mso.openstack.utils.MsoNeutronUtils;
48 import org.openecomp.mso.properties.MsoPropertiesException;
49 import org.openecomp.mso.logger.MessageEnum;
52 * This class returns a cloud Config instances
57 @Singleton(name = "CloudConfigFactory")
58 @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
61 public class CloudConfigFactory implements Serializable {
63 private static final long serialVersionUID = 2956662716453261085L;
65 private static CloudConfig cloudConfigCache = new CloudConfig ();
67 protected static String prefixMsoPropertiesPath = System.getProperty ("mso.config.path");
69 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
71 private static int refreshTimer;
73 private static final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
76 if (prefixMsoPropertiesPath == null) {
77 prefixMsoPropertiesPath = "";
81 public void initializeCloudConfig (String filePath, int refreshTimer) throws MsoCloudIdentityNotFound {
83 rwl.writeLock ().lock ();
85 cloudConfigCache.loadCloudConfig (prefixMsoPropertiesPath + filePath, refreshTimer);
86 LOGGER.info (MessageEnum.RA_CONFIG_LOAD, prefixMsoPropertiesPath + filePath, "", "");
87 } catch (JsonParseException e) {
88 LOGGER.error (MessageEnum.RA_CONFIG_EXC, "Error parsing cloud config file " + filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonParseException", e);
89 } catch (JsonMappingException e) {
90 LOGGER.error (MessageEnum.RA_CONFIG_EXC, "Error parsing cloud config file " + filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonMappingException", e);
91 } catch (IOException e) {
92 LOGGER.error (MessageEnum.RA_CONFIG_NOT_FOUND, filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - config not found", e);
94 rwl.writeLock ().unlock ();
98 public void changeMsoPropertiesFilePath (String newMsoPropPath) {
99 rwl.writeLock ().lock ();
101 CloudConfigFactory.cloudConfigCache.configFilePath = prefixMsoPropertiesPath + newMsoPropPath;
103 rwl.writeLock ().unlock ();
107 public CloudConfigFactory () {
110 public CloudConfig getCloudConfig () {
111 rwl.readLock ().lock ();
113 if (cloudConfigCache.isValidCloudConfig()) {
114 return cloudConfigCache.clone ();
116 return new CloudConfig();
119 rwl.readLock ().unlock ();
124 * This method is not intended to be called, it's used to refresh the config
127 * @return true if Properties have been reloaded, false otherwise
129 @Schedule(minute = "*/1", hour = "*", persistent = false)
130 public void reloadCloudConfig () {
133 if (!rwl.writeLock ().tryLock () && !rwl.writeLock ().tryLock (30L, TimeUnit.SECONDS)) {
134 LOGGER.debug ("Busy write lock on mso cloud config factory, skipping the reloading");
137 } catch (InterruptedException e1) {
138 LOGGER.debug ("Interrupted while trying to acquire write lock on cloud config factory, skipping the reloading");
139 Thread.currentThread ().interrupt ();
143 //LOGGER.debug ("Processing a reload of the mso properties file entries");
146 if (refreshTimer <= 1) {
147 CloudConfig oldCloudConfig = null;
148 if (cloudConfigCache.isValidCloudConfig()) {
149 oldCloudConfig = cloudConfigCache.clone();
151 cloudConfigCache.reloadPropertiesFile ();
152 refreshTimer = cloudConfigCache.refreshTimerInMinutes;
153 if (!cloudConfigCache.equals(oldCloudConfig)) {
154 LOGGER.info (MessageEnum.RA_CONFIG_LOAD, prefixMsoPropertiesPath + cloudConfigCache.configFilePath, "", "");
161 } catch (JsonParseException e) {
162 LOGGER.error (MessageEnum.RA_CONFIG_EXC,
163 "Error parsing cloud config file " + cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonParseException",
165 } catch (JsonMappingException e) {
166 LOGGER.error (MessageEnum.RA_CONFIG_EXC,
167 "Error parsing cloud config file " + cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonMappingException",
169 } catch (IOException e) {
170 LOGGER.error (MessageEnum.RA_CONFIG_NOT_FOUND, cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - config not found", e);
172 } catch (Exception e) {
173 LOGGER.error (MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. Global issue while reloading", "", "", MsoLogger.ErrorCode.DataError, "Exception - Global issue while reloading\"", e);
175 rwl.writeLock ().unlock ();
181 @Produces("text/plain")
182 public Response showCloudConfig () {
183 CloudConfig cloudConfig = this.getCloudConfig ();
184 if (cloudConfig != null) {
185 StringBuffer response = new StringBuffer ();
186 response.append ("Cloud Sites:\n");
187 for (CloudSite site : cloudConfig.getCloudSites ().values ()) {
188 response.append (site.toString () + "\n");
191 response.append ("\n\nCloud Identity Services:\n");
192 for (CloudIdentity identity : cloudConfig.getIdentityServices ().values ()) {
193 response.append (identity.toString () + "\n");
196 return Response.status (200).entity (response).build ();
198 return Response.status (500).entity ("Cloud Config has not been loaded properly, this could be due to a bad JSON structure (Check the logs for additional details)").build ();
203 @Path("/resetClientCaches")
204 @Produces("text/plain")
205 public Response resetClientCaches () {
206 // Reset all cached clients/credentials
207 MsoKeystoneUtils.adminCacheReset ();
208 MsoHeatUtils.heatCacheReset ();
209 MsoNeutronUtils.neutronCacheReset ();
211 String response = "Client caches reset. All entries removed.";
212 return Response.status (200).entity (response).build ();
216 @Path("/cleanupClientCaches")
217 @Produces("text/plain")
218 public Response cleanupClientCaches () {
219 // Reset all cached clients/credentials
220 MsoKeystoneUtils.adminCacheCleanup ();
221 MsoHeatUtils.heatCacheCleanup ();
222 MsoNeutronUtils.neutronCacheCleanup ();
224 String response = "Client caches cleaned up. All expired entries removed";
225 return Response.status (200).entity (response).build ();
229 @Path("/encryptPassword/{pwd}")
230 @Produces("text/plain")
231 public Response encryptPassword (@PathParam("pwd") String pwd) {
232 String encryptedPassword = CloudIdentity.encryptPassword (pwd);
234 String response = "Encrypted Password = " + encryptedPassword;
235 return Response.status (200).entity (response).build ();