Containerization feature of SO
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / cloud / CloudConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.cloud;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Optional;
27
28 import javax.annotation.PostConstruct;
29
30 import com.fasterxml.jackson.annotation.JsonProperty;
31 import com.fasterxml.jackson.annotation.JsonRootName;
32
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;
39
40 /**
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)
46  *
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.
49  *
50  * This class also contains methods to query cloud sites and/or identity
51  * services by ID.
52  *
53  */
54
55 @Configuration
56 @JsonRootName("cloud_config")
57 @ConfigurationProperties(prefix="cloud_config")
58 public class CloudConfig {
59         
60     private static final String CLOUD_SITE_VERSION = "2.5";
61     private static final String DEFAULT_CLOUD_SITE_ID = "default";
62     
63     @JsonProperty("identity_services")
64     private Map<String, CloudIdentity> identityServices = new HashMap<>();
65     
66     @JsonProperty("cloud_sites")
67     private Map <String, CloudSite> cloudSites = new HashMap<>();
68     
69     @JsonProperty("cloudify_managers")
70     private Map <String, CloudifyManager> cloudifyManagers = new HashMap<>();
71
72     @PostConstruct
73     private void init() {
74         for (Entry<String, CloudIdentity> entry : identityServices.entrySet()) {
75                 entry.getValue().setId(entry.getKey());
76         }
77         
78         for (Entry<String, CloudSite> entry : cloudSites.entrySet()) {
79                 entry.getValue().setId(entry.getKey());
80         }
81         
82         for (Entry<String, CloudifyManager> entry : cloudifyManagers.entrySet()) {
83                 entry.getValue().setId(entry.getKey());
84         }
85     }
86     
87     /**
88      * Get a map of all identity services that have been loaded.
89      */
90     public Map<String, CloudIdentity> getIdentityServices() {
91         return identityServices;
92     }
93
94     /**
95      * Get a map of all cloud sites that have been loaded.
96      */
97     public Map<String, CloudSite> getCloudSites() {
98         return cloudSites;
99     }
100
101         /**
102          * Get a Map of all CloudifyManagers that have been loaded.
103          * @return the Map
104          */
105     public Map<String,CloudifyManager> getCloudifyManagers() {
106          return cloudifyManagers;
107     }
108     
109     /**
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.
114      * 
115      * @param id the ID to match
116      * @return an Optional of CloudSite object.
117      */
118      public synchronized Optional<CloudSite> getCloudSite(String id) {
119         if (id == null) {
120             return Optional.empty();
121         }
122         if (cloudSites.containsKey(id)) {
123             return Optional.ofNullable(cloudSites.get(id));
124         } else {
125                 return getCloudSiteWithClli(id);
126         }
127     }
128     
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();
133        }
134        return null;
135     }
136
137     /**
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.
140      * 
141      * @param clli
142      *            the CLLI to match
143      * @param version
144      *            the version to match; may be null in which case any version
145      *            matches
146      * @return a CloudSite, or null of no match found
147      */
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())))
151                 .findAny();
152         if (cloudSiteOptional.isPresent()) {
153                 return cloudSiteOptional;
154         } else {
155                 return getDefaultCloudSite(clli);
156         }
157     }
158
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);
166             clone.setId(clli);
167             return Optional.of(clone);
168         } else {
169             return Optional.empty();
170         }
171     }
172
173     /**
174      * Get a specific CloudIdentity, based on an ID.
175      * 
176      * @param id
177      *            the ID to match
178      * @return a CloudIdentity, or null of no match found
179      */
180     public CloudIdentity getIdentityService(String id) {
181                 return identityServices.get(id);
182     }
183
184         /**
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
188          */
189         public CloudifyManager getCloudifyManager (String id) {
190                         return cloudifyManagers.get(id);
191         }
192         
193         @Override
194         public String toString() {
195                 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
196                                 .append("identityServices", getIdentityServices()).append("cloudSites", getCloudSites()).toString();
197         }
198         
199         @Override
200         public boolean equals(final Object other) {
201                 if (other == null) {
202                         return false;
203                 }
204                 if (!getClass().equals(other.getClass())) {
205                         return false;
206                 }
207                 CloudConfig castOther = (CloudConfig) other;
208                 return new EqualsBuilder().append(getIdentityServices(), castOther.getIdentityServices())
209                                 .append(getCloudSites(), castOther.getCloudSites()).isEquals();
210         }
211         
212         @Override
213         public int hashCode() {
214                 return new HashCodeBuilder(1, 31).append(getIdentityServices()).append(getCloudSites()).toHashCode();
215         }
216 }