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;
36 import org.openecomp.mso.logger.MsoLogger;
39 * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
40 * configuration file to Java. The CloudConfig contains information about
41 * Openstack cloud configurations (in particular for the NVP/AIC cloud).
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
47 * JSON 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 @JsonProperty("identity_services")
58 private Map <String, CloudIdentity> identityServices = new HashMap <String, CloudIdentity> ();
59 @JsonProperty("cloud_sites")
60 private Map <String, CloudSite> cloudSites = new HashMap <String, CloudSite> ();
62 private static ObjectMapper mapper = new ObjectMapper ();
64 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66 protected String configFilePath;
68 protected int refreshTimerInMinutes;
70 public CloudConfig () {
71 mapper.enable (DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
72 mapper.enable (DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
76 * Get a Map of all IdentityServices that have been loaded.
79 public synchronized Map <String, CloudIdentity> getIdentityServices () {
80 return identityServices;
84 * Get a Map of all CloudSites that have been loaded.
87 public synchronized Map <String, CloudSite> getCloudSites () {
92 * Get a specific CloudSites, based on an ID. The ID is first checked against
93 * the regions, and if no match is found there, then against individual entries
94 * to try and find one with a CLLI that matches the ID and an AIC version of 2.5.
95 * @param id the ID to match
96 * @return a CloudSite, or null of no match found
98 public synchronized CloudSite getCloudSite (String id) {
100 if (cloudSites.containsKey (id)) {
101 return cloudSites.get (id);
103 // check for id == CLLI now as well
104 return getCloudSiteWithClli(id, "2.5");
110 * Get a specific CloudSites, based on a CLLI and (optional) version, which will be matched
111 * against the aic_version field of the CloudSite.
112 * @param clli the CLLI to match
113 * @param version the version to match; may be null in which case any version matches
114 * @return a CloudSite, or null of no match found
116 public synchronized CloudSite getCloudSiteWithClli(String clli, String version) {
118 // New with 1610 - find cloud site called "DEFAULT" - return that object,
119 // with the name modified to match what they asked for. We're looping thru
120 // the cloud sites anyway - so save off the default one in case we need it.
121 CloudSite defaultCloudSite = null;
122 for (CloudSite cs : cloudSites.values()) {
123 if (cs.getClli() != null && clli.equals(cs.getClli())) {
124 if (version == null || version.equals(cs.getAic_version())) {
127 } else if (cs.getId().equalsIgnoreCase("default")) {
128 // save it off in case we need it
129 defaultCloudSite = cs.clone();
132 // If we get here - we didn't find a match - so return the default cloud site
133 if (defaultCloudSite != null) {
134 defaultCloudSite.setRegionId(clli);
135 defaultCloudSite.setId(clli);
137 return defaultCloudSite;
143 * Get a specific CloudIdentity, based on an ID.
144 * @param id the ID to match
145 * @return a CloudIdentity, or null of no match found
147 public synchronized CloudIdentity getIdentityService (String id) {
148 if (identityServices.containsKey (id)) {
149 return identityServices.get (id);
154 protected synchronized void reloadPropertiesFile() throws JsonParseException, JsonMappingException, IOException {
155 this.loadCloudConfig(this.configFilePath, this.refreshTimerInMinutes);
158 protected synchronized void loadCloudConfig (String configFile, int refreshTimer) throws JsonParseException, JsonMappingException, IOException {
160 FileReader reader=null;
161 configFilePath=configFile;
162 this.refreshTimerInMinutes = refreshTimer;
164 CloudConfig cloudConfig = null;
167 reader = new FileReader (configFile);
168 // Parse the JSON input into a CloudConfig
170 cloudConfig = mapper.readValue (reader, CloudConfig.class);
172 this.cloudSites = cloudConfig.cloudSites;
173 this.identityServices = cloudConfig.identityServices;
175 // Copy Cloud Identity IDs to CloudIdentity objects
176 for (Entry <String, CloudIdentity> entry : cloudConfig.getIdentityServices ().entrySet ()) {
177 entry.getValue ().setId (entry.getKey ());
180 // Copy Cloud Site IDs to CloudSite objects, and set up internal
181 // pointers to their corresponding identity service.
182 for (Entry <String, CloudSite> entry : cloudConfig.getCloudSites ().entrySet ()) {
183 CloudSite s = entry.getValue ();
184 s.setId (entry.getKey ());
185 s.setIdentityService (cloudConfig.getIdentityService (s.getIdentityServiceId ()));
189 if (reader != null) {
192 } catch (IOException e) {
193 LOGGER.debug("Exception while closing reader for file:" + configFilePath, e);
198 public String getConfigFilePath() {
199 return configFilePath;
203 public synchronized CloudConfig clone() {
204 CloudConfig ccCopy = new CloudConfig();
205 for (Entry<String,CloudIdentity> e:identityServices.entrySet()) {
207 ccCopy.identityServices.put(e.getKey(), e.getValue().clone());
210 for (Entry<String,CloudSite> e:cloudSites.entrySet()) {
212 ccCopy.cloudSites.put(e.getKey(), e.getValue().clone());
215 ccCopy.configFilePath = this.configFilePath;
217 ccCopy.refreshTimerInMinutes = this.refreshTimerInMinutes;
223 public int hashCode() {
224 final int prime = 31;
226 result = prime * result + ((cloudSites == null) ? 0 : cloudSites.hashCode());
227 result = prime * result + ((configFilePath == null) ? 0 : configFilePath.hashCode());
228 result = prime * result + ((identityServices == null) ? 0 : identityServices.hashCode());
229 result = prime * result + refreshTimerInMinutes;
234 public boolean equals(Object obj) {
239 if (getClass() != obj.getClass())
241 CloudConfig other = (CloudConfig) obj;
242 if (cloudSites == null) {
243 if (other.cloudSites != null)
245 } else if (!cloudSites.equals(other.cloudSites))
247 if (configFilePath == null) {
248 if (other.configFilePath != null)
250 } else if (!configFilePath.equals(other.configFilePath))
252 if (identityServices == null) {
253 if (other.identityServices != null)
255 } else if (!identityServices.equals(other.identityServices))
257 if (refreshTimerInMinutes != other.refreshTimerInMinutes)