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.FileReader;
24 import java.io.IOException;
25 import java.util.HashMap;
27 import java.util.Map.Entry;
29 import org.codehaus.jackson.JsonParseException;
30 import org.codehaus.jackson.annotate.JsonProperty;
31 import org.codehaus.jackson.map.DeserializationConfig;
32 import org.codehaus.jackson.map.JsonMappingException;
33 import org.codehaus.jackson.map.ObjectMapper;
34 import org.codehaus.jackson.map.annotate.JsonRootName;
35 import org.openecomp.mso.logger.MessageEnum;
36 import org.openecomp.mso.logger.MsoLogger;
37 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
40 * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
41 * configuration file to Java. The CloudConfig contains information about
42 * Openstack cloud configurations. It includes:
43 * - CloudIdentity objects,representing DCP nodes (Openstack Identity Service)
44 * - CloudSite objects, representing LCP nodes (Openstack Compute & other services)
46 * Note that this is only used to access Cloud Configurations loaded from a JSON
47 * config file, so there are no explicit property setters.
49 * This class also contains methods to query cloud sites and/or identity
54 @JsonRootName("cloud_config")
55 public class CloudConfig {
57 private boolean validCloudConfig = false;
58 @JsonProperty("identity_services")
59 private Map<String, CloudIdentity> identityServices = new HashMap<>();
60 @JsonProperty("cloud_sites")
61 private Map<String, CloudSite> cloudSites = new HashMap<>();
63 private static ObjectMapper mapper = new ObjectMapper();
65 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
67 protected String configFilePath;
69 protected int refreshTimerInMinutes;
71 public CloudConfig() {
72 mapper.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
73 mapper.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
77 * Get a Map of all IdentityServices that have been loaded.
81 public synchronized Map<String, CloudIdentity> getIdentityServices() {
82 return identityServices;
86 * Get a Map of all CloudSites that have been loaded.
90 public synchronized Map<String, CloudSite> getCloudSites() {
95 * Get a specific CloudSites, based on an ID. The ID is first checked
96 * against the regions, and if no match is found there, then against
97 * individual entries to try and find one with a CLLI that matches the ID
98 * and an AIC version of 2.5.
102 * @return a CloudSite, or null of no match found
104 public synchronized CloudSite getCloudSite(String id) {
106 if (cloudSites.containsKey(id)) {
107 return cloudSites.get(id);
109 // check for id == CLLI now as well
110 return getCloudSiteWithClli(id, "2.5");
116 * Get a specific CloudSites, based on a CLLI and (optional) version, which
117 * will be matched against the aic_version field of the CloudSite.
122 * the version to match; may be null in which case any version
124 * @return a CloudSite, or null of no match found
126 public synchronized CloudSite getCloudSiteWithClli(String clli, String version) {
128 // New with 1610 - find cloud site called "DEFAULT" - return that
129 // object,with the name modified to match what they asked for. We're
130 // looping thru the cloud sites anyway - so save off the default one in case we
132 CloudSite defaultCloudSite = null;
133 for (CloudSite cs : cloudSites.values()) {
134 if (cs.getClli() != null && clli.equals(cs.getClli())) {
135 if (version == null || version.equals(cs.getAic_version())) {
138 } else if ("default".equalsIgnoreCase(cs.getId())) {
139 // save it off in case we need it
140 defaultCloudSite = cs.clone();
143 // If we get here - we didn't find a match - so return the default
145 if (defaultCloudSite != null) {
146 defaultCloudSite.setRegionId(clli);
147 defaultCloudSite.setId(clli);
149 return defaultCloudSite;
155 * Get a specific CloudIdentity, based on an ID.
159 * @return a CloudIdentity, or null of no match found
161 public synchronized CloudIdentity getIdentityService(String id) {
162 if (identityServices.containsKey(id)) {
163 return identityServices.get(id);
168 protected synchronized void reloadPropertiesFile() throws JsonParseException, JsonMappingException, IOException, MsoCloudIdentityNotFound {
169 this.loadCloudConfig(this.configFilePath, this.refreshTimerInMinutes);
172 protected synchronized void loadCloudConfig(String configFile, int refreshTimer)
173 throws JsonParseException, JsonMappingException, IOException, MsoCloudIdentityNotFound {
175 FileReader reader = null;
176 configFilePath = configFile;
177 this.refreshTimerInMinutes = refreshTimer;
179 CloudConfig cloudConfig = null;
180 this.validCloudConfig=false;
183 reader = new FileReader(configFile);
184 // Parse the JSON input into a CloudConfig
186 cloudConfig = mapper.readValue(reader, CloudConfig.class);
188 this.cloudSites = cloudConfig.cloudSites;
189 this.identityServices = cloudConfig.identityServices;
191 // Copy Cloud Identity IDs to CloudIdentity objects
192 for (Entry<String, CloudIdentity> entry : cloudConfig.getIdentityServices().entrySet()) {
193 entry.getValue().setId(entry.getKey());
196 // Copy Cloud Site IDs to CloudSite objects, and set up internal
197 // pointers to their corresponding identity service.
198 for (Entry<String, CloudSite> entry : cloudConfig.getCloudSites().entrySet()) {
199 CloudSite s = entry.getValue();
200 s.setId(entry.getKey());
201 CloudIdentity cloudIdentity = cloudConfig.getIdentityService(s.getIdentityServiceId());
202 s.setIdentityService(cloudIdentity);
203 if (cloudIdentity == null) {
204 throw new MsoCloudIdentityNotFound(s.getId()+" Cloud site refers to a non-existing identity service: "+s.getIdentityServiceId());
207 this.validCloudConfig=true;
211 if (reader != null) {
214 } catch (IOException e) {
215 LOGGER.debug("Exception while closing reader for file:" + configFilePath, e);
220 public String getConfigFilePath() {
221 return configFilePath;
225 * @return the validCouldConfig
227 public synchronized boolean isValidCloudConfig() {
228 return validCloudConfig;
232 public synchronized CloudConfig clone() {
233 CloudConfig ccCopy = new CloudConfig();
234 for (Entry<String, CloudIdentity> e : identityServices.entrySet()) {
236 ccCopy.identityServices.put(e.getKey(), e.getValue().clone());
239 for (Entry<String, CloudSite> e : cloudSites.entrySet()) {
241 ccCopy.cloudSites.put(e.getKey(), e.getValue().clone());
243 ccCopy.configFilePath = this.configFilePath;
244 ccCopy.refreshTimerInMinutes = this.refreshTimerInMinutes;
245 ccCopy.validCloudConfig = this.validCloudConfig;
250 * @see java.lang.Object#hashCode()
253 public int hashCode() {
254 final int prime = 31;
256 result = prime * result + ((cloudSites == null) ? 0 : cloudSites.hashCode());
257 result = prime * result + ((configFilePath == null) ? 0 : configFilePath.hashCode());
258 result = prime * result + ((identityServices == null) ? 0 : identityServices.hashCode());
259 result = prime * result + refreshTimerInMinutes;
260 result = prime * result + (validCloudConfig ? 1231 : 1237);
265 * @see java.lang.Object#equals(java.lang.Object)
268 public boolean equals(Object obj) {
273 if (getClass() != obj.getClass())
275 CloudConfig other = (CloudConfig) obj;
276 if (cloudSites == null) {
277 if (other.cloudSites != null)
279 } else if (!cloudSites.equals(other.cloudSites))
281 if (configFilePath == null) {
282 if (other.configFilePath != null)
284 } else if (!configFilePath.equals(other.configFilePath))
286 if (identityServices == null) {
287 if (other.identityServices != null)
289 } else if (!identityServices.equals(other.identityServices))
291 if (refreshTimerInMinutes != other.refreshTimerInMinutes)
293 if (validCloudConfig != other.validCloudConfig)