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;
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.locks.ReentrantReadWriteLock;
27 import javax.ejb.ConcurrencyManagement;
28 import javax.ejb.ConcurrencyManagementType;
29 import javax.ejb.LocalBean;
30 import javax.ejb.Schedule;
31 import javax.ejb.Singleton;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.Response;
38 import org.openecomp.mso.logger.MessageEnum;
39 import org.openecomp.mso.logger.MsoLogger;
40 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
41 import org.openecomp.mso.openstack.utils.MsoHeatUtils;
42 import org.openecomp.mso.openstack.utils.MsoKeystoneUtils;
43 import org.openecomp.mso.openstack.utils.MsoNeutronUtils;
45 import com.fasterxml.jackson.core.JsonParseException;
46 import com.fasterxml.jackson.databind.JsonMappingException;
49 * This class returns a cloud Config instances
52 @Singleton(name = "CloudConfigFactory")
53 @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
56 public class CloudConfigFactory implements Serializable {
58 private static final long serialVersionUID = 2956662716453261085L;
60 private CloudConfig cloudConfigCache = new CloudConfig();
62 protected static String prefixMsoPropertiesPath = System.getProperty ("mso.config.path");
64 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66 private int refreshTimer;
68 private static final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
71 if (prefixMsoPropertiesPath == null) {
72 prefixMsoPropertiesPath = "";
76 public void initializeCloudConfig (String filePath, int refreshTimer) throws MsoCloudIdentityNotFound {
77 rwl.writeLock ().lock ();
79 cloudConfigCache.loadCloudConfig (prefixMsoPropertiesPath + filePath, refreshTimer);
80 LOGGER.info (MessageEnum.RA_CONFIG_LOAD, prefixMsoPropertiesPath + filePath, "", "");
81 } catch (JsonParseException e) {
82 LOGGER.error (MessageEnum.RA_CONFIG_EXC, "Error parsing cloud config file " + filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonParseException", e);
83 } catch (JsonMappingException e) {
84 LOGGER.error (MessageEnum.RA_CONFIG_EXC, "Error parsing cloud config file " + filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonMappingException", e);
85 } catch (IOException e) {
86 LOGGER.error (MessageEnum.RA_CONFIG_NOT_FOUND, filePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - config not found", e);
88 rwl.writeLock ().unlock ();
92 public CloudConfig getCloudConfig () {
93 rwl.readLock ().lock ();
95 if (cloudConfigCache.isValidCloudConfig()) {
96 return cloudConfigCache.clone ();
98 return new CloudConfig();
101 rwl.readLock ().unlock ();
106 * This method is not intended to be called, it's used to refresh the config
109 * @return true if Properties have been reloaded, false otherwise
111 @Schedule(minute = "*/1", hour = "*", persistent = false)
112 public void reloadCloudConfig () {
115 if (!rwl.writeLock ().tryLock () && !rwl.writeLock ().tryLock (30L, TimeUnit.SECONDS)) {
116 LOGGER.debug ("Busy write lock on mso cloud config factory, skipping the reloading");
119 } catch (InterruptedException e1) {
120 LOGGER.debug ("Interrupted while trying to acquire write lock on cloud config factory, skipping the reloading");
121 Thread.currentThread ().interrupt ();
125 //LOGGER.debug ("Processing a reload of the mso properties file entries");
128 if (refreshTimer <= 1) {
129 CloudConfig oldCloudConfig = null;
130 if (cloudConfigCache.isValidCloudConfig()) {
131 oldCloudConfig = cloudConfigCache.clone();
133 cloudConfigCache.reloadPropertiesFile ();
134 refreshTimer = cloudConfigCache.refreshTimerInMinutes;
135 if (!cloudConfigCache.equals(oldCloudConfig)) {
136 LOGGER.info (MessageEnum.RA_CONFIG_LOAD, prefixMsoPropertiesPath + cloudConfigCache.configFilePath, "", "");
143 } catch (JsonParseException e) {
144 LOGGER.error (MessageEnum.RA_CONFIG_EXC,
145 "Error parsing cloud config file " + cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonParseException",
147 } catch (JsonMappingException e) {
148 LOGGER.error (MessageEnum.RA_CONFIG_EXC,
149 "Error parsing cloud config file " + cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - JsonMappingException",
151 } catch (IOException e) {
152 LOGGER.error (MessageEnum.RA_CONFIG_NOT_FOUND, cloudConfigCache.configFilePath, "", "", MsoLogger.ErrorCode.DataError, "Exception - config not found", e);
154 } catch (Exception e) {
155 LOGGER.error (MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. Global issue while reloading", "", "", MsoLogger.ErrorCode.DataError, "Exception - Global issue while reloading\"", e);
157 rwl.writeLock ().unlock ();
163 @Produces("text/plain")
164 public Response showCloudConfig () {
165 CloudConfig cloudConfig = this.getCloudConfig();
166 StringBuffer response = new StringBuffer();
167 response.append("Cloud Sites:\n");
168 for (CloudSite site : cloudConfig.getCloudSites().values()) {
169 response.append(site.toString()).append("\n");
171 response.append("\n\nCloud Identity Services:\n");
172 for (CloudIdentity identity : cloudConfig.getIdentityServices().values()) {
173 response.append(identity.toString()).append("\n");
175 return Response.status(200).entity(response).build();
179 @Path("/resetClientCaches")
180 @Produces("text/plain")
181 public Response resetClientCaches () {
182 // Reset all cached clients/credentials
183 MsoKeystoneUtils.adminCacheReset ();
184 MsoHeatUtils.heatCacheReset ();
185 MsoNeutronUtils.neutronCacheReset ();
186 String response = "Client caches reset. All entries removed.";
187 return Response.status (200).entity (response).build ();
191 @Path("/cleanupClientCaches")
192 @Produces("text/plain")
193 public Response cleanupClientCaches () {
194 // Reset all cached clients/credentials
195 MsoKeystoneUtils.adminCacheCleanup ();
196 MsoHeatUtils.heatCacheCleanup ();
197 MsoNeutronUtils.neutronCacheCleanup ();
198 String response = "Client caches cleaned up. All expired entries removed.";
199 return Response.status (200).entity (response).build ();
203 @Path("/encryptPassword/{pwd}")
204 @Produces("text/plain")
205 public Response encryptPassword (@PathParam("pwd") String pwd) {
206 String encryptedPassword = CloudIdentity.encryptPassword (pwd);
208 String response = "Encrypted Password = " + encryptedPassword;
209 return Response.status (200).entity (response).build ();