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.onap.so.cloud;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import java.util.Optional;
28 import javax.annotation.PostConstruct;
30 import com.fasterxml.jackson.annotation.JsonProperty;
31 import com.fasterxml.jackson.annotation.JsonRootName;
33 import org.apache.commons.lang3.builder.ToStringBuilder;
34 import org.apache.commons.lang3.builder.ToStringStyle;
35 import org.springframework.boot.context.properties.ConfigurationProperties;
36 import org.springframework.context.annotation.Configuration;
37 import org.apache.commons.lang3.builder.HashCodeBuilder;
38 import org.apache.commons.lang3.builder.EqualsBuilder;
41 * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
42 * configuration file to Java. The CloudConfig contains information about
43 * Openstack cloud configurations. It includes:
44 * - CloudIdentity objects,representing DCP nodes (Openstack Identity Service)
45 * - CloudSite objects, representing LCP nodes (Openstack Compute & other services)
47 * Note that this is only used to access Cloud Configurations loaded from a JSON
48 * config file, so there are no explicit property setters.
50 * This class also contains methods to query cloud sites and/or identity
56 @JsonRootName("cloud_config")
57 @ConfigurationProperties(prefix="cloud_config")
58 public class CloudConfig {
60 private static final String CLOUD_SITE_VERSION = "2.5";
61 private static final String DEFAULT_CLOUD_SITE_ID = "default";
63 @JsonProperty("identity_services")
64 private Map<String, CloudIdentity> identityServices = new HashMap<>();
66 @JsonProperty("cloud_sites")
67 private Map <String, CloudSite> cloudSites = new HashMap<>();
69 @JsonProperty("cloudify_managers")
70 private Map <String, CloudifyManager> cloudifyManagers = new HashMap<>();
74 for (Entry<String, CloudIdentity> entry : identityServices.entrySet()) {
75 entry.getValue().setId(entry.getKey());
78 for (Entry<String, CloudSite> entry : cloudSites.entrySet()) {
79 entry.getValue().setId(entry.getKey());
82 for (Entry<String, CloudifyManager> entry : cloudifyManagers.entrySet()) {
83 entry.getValue().setId(entry.getKey());
88 * Get a map of all identity services that have been loaded.
90 public Map<String, CloudIdentity> getIdentityServices() {
91 return identityServices;
95 * Get a map of all cloud sites that have been loaded.
97 public Map<String, CloudSite> getCloudSites() {
102 * Get a Map of all CloudifyManagers that have been loaded.
105 public Map<String,CloudifyManager> getCloudifyManagers() {
106 return cloudifyManagers;
110 * Get a specific CloudSites, based on an ID. The ID is first checked
111 * against the regions, and if no match is found there, then against
112 * individual entries to try and find one with a CLLI that matches the ID
113 * and an AIC version of 2.5.
115 * @param id the ID to match
116 * @return an Optional of CloudSite object.
118 public synchronized Optional<CloudSite> getCloudSite(String id) {
120 return Optional.empty();
122 if (cloudSites.containsKey(id)) {
123 return Optional.ofNullable(cloudSites.get(id));
125 return getCloudSiteWithClli(id);
129 public String getCloudSiteId(CloudSite cloudSite) {
130 for(Entry<String, CloudSite> entry : this.getCloudSites().entrySet()){
131 if(entry.getValue().equals(cloudSite))
132 return entry.getKey();
138 * Get a specific CloudSites, based on a CLLI and (optional) version, which
139 * will be matched against the aic_version field of the CloudSite.
144 * the version to match; may be null in which case any version
146 * @return a CloudSite, or null of no match found
148 private Optional<CloudSite> getCloudSiteWithClli(String clli) {
149 Optional <CloudSite> cloudSiteOptional = cloudSites.values().stream().filter(cs ->
150 cs.getClli() != null && clli.equals(cs.getClli()) && (CLOUD_SITE_VERSION.equals(cs.getAicVersion())))
152 if (cloudSiteOptional.isPresent()) {
153 return cloudSiteOptional;
155 return getDefaultCloudSite(clli);
159 private Optional<CloudSite> getDefaultCloudSite(String clli) {
160 Optional<CloudSite> cloudSiteOpt = cloudSites.values().stream()
161 .filter(cs -> cs.getId().equalsIgnoreCase(DEFAULT_CLOUD_SITE_ID)).findAny();
162 if (cloudSiteOpt.isPresent()) {
163 CloudSite defaultCloudSite = cloudSiteOpt.get();
164 CloudSite clone = new CloudSite(defaultCloudSite);
165 clone.setRegionId(clli);
167 return Optional.of(clone);
169 return Optional.empty();
174 * Get a specific CloudIdentity, based on an ID.
178 * @return a CloudIdentity, or null of no match found
180 public CloudIdentity getIdentityService(String id) {
181 return identityServices.get(id);
185 * Get a specific CloudifyManager, based on an ID.
186 * @param id the ID to match
187 * @return a CloudifyManager, or null of no match found
189 public CloudifyManager getCloudifyManager (String id) {
190 return cloudifyManagers.get(id);
194 public String toString() {
195 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
196 .append("identityServices", getIdentityServices()).append("cloudSites", getCloudSites()).toString();
200 public boolean equals(final Object other) {
204 if (!getClass().equals(other.getClass())) {
207 CloudConfig castOther = (CloudConfig) other;
208 return new EqualsBuilder().append(getIdentityServices(), castOther.getIdentityServices())
209 .append(getCloudSites(), castOther.getCloudSites()).isEquals();
213 public int hashCode() {
214 return new HashCodeBuilder(1, 31).append(getIdentityServices()).append(getCloudSites()).toHashCode();