Initial OpenECOMP MSO commit
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / CloudConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.openecomp.mso.cloud;
22
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
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
36 import org.openecomp.mso.logger.MsoLogger;
37
38 /**
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).
42  * It includes:
43  * - CloudIdentity objects, representing DCP nodes (Openstack Identity Service)
44  * - CloudSite objects, representing LCP nodes (Openstack Compute & other services)
45  *
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.
48  *
49  * This class also contains methods to query cloud sites and/or identity
50  * services by ID.
51  *
52  */
53
54 @JsonRootName("cloud_config")
55 public class CloudConfig {
56
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> ();
61
62     private static ObjectMapper mapper = new ObjectMapper ();
63
64     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
65
66     protected String configFilePath;
67
68     protected int refreshTimerInMinutes;
69
70     public CloudConfig () {
71          mapper.enable (DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
72          mapper.enable (DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
73     }
74
75         /**
76          * Get a Map of all IdentityServices that have been loaded.
77          * @return the Map
78          */
79     public synchronized Map <String, CloudIdentity> getIdentityServices () {
80         return identityServices;
81     }
82
83         /**
84          * Get a Map of all CloudSites that have been loaded.
85          * @return the Map
86          */
87         public synchronized Map <String, CloudSite> getCloudSites () {
88                 return cloudSites;
89         }
90
91         /**
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
97          */
98         public synchronized CloudSite getCloudSite (String id) {
99                 if (id != null) {
100                         if (cloudSites.containsKey (id)) {
101                                 return cloudSites.get (id);
102                         }
103                         // check for id == CLLI now as well
104                         return getCloudSiteWithClli(id, "2.5");
105                 }
106                 return null;
107         }
108
109         /**
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
115          */
116         public synchronized CloudSite getCloudSiteWithClli(String clli, String version) {
117                 if (clli != null) {
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())) {
125                                 return cs;
126                                 }
127                         } else if (cs.getId().equalsIgnoreCase("default")) {
128                                 // save it off in case we need it
129                                 defaultCloudSite = cs.clone();
130                         }
131                 }
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);
136             }
137                 return defaultCloudSite;
138                 }
139         return null;
140         }
141
142         /**
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
146          */
147         public synchronized CloudIdentity getIdentityService (String id) {
148                 if (identityServices.containsKey (id)) {
149                         return identityServices.get (id);
150                 }
151                 return null;
152         }
153
154     protected synchronized void reloadPropertiesFile() throws JsonParseException, JsonMappingException, IOException  {
155                 this.loadCloudConfig(this.configFilePath, this.refreshTimerInMinutes);
156         }
157
158     protected synchronized void loadCloudConfig (String configFile, int refreshTimer) throws JsonParseException, JsonMappingException, IOException {
159  
160         FileReader reader=null;
161         configFilePath=configFile;
162         this.refreshTimerInMinutes = refreshTimer;
163  
164         CloudConfig cloudConfig = null;
165  
166         try {
167                 reader = new FileReader (configFile);
168                 // Parse the JSON input into a CloudConfig
169
170                 cloudConfig = mapper.readValue (reader, CloudConfig.class);
171
172                 this.cloudSites = cloudConfig.cloudSites;
173                 this.identityServices = cloudConfig.identityServices;
174
175                 // Copy Cloud Identity IDs to CloudIdentity objects
176                 for (Entry <String, CloudIdentity> entry : cloudConfig.getIdentityServices ().entrySet ()) {
177                     entry.getValue ().setId (entry.getKey ());
178                 }
179
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 ()));
186                 }
187         } finally {
188                 try {
189                                 if (reader != null) {
190                                         reader.close();
191                                 }
192                         } catch (IOException e) {
193                                 LOGGER.debug("Exception while closing reader for file:" + configFilePath, e);
194                         }
195         }
196     }
197
198         public String getConfigFilePath() {
199                 return configFilePath;
200         }
201
202         @Override
203         public synchronized CloudConfig clone() {
204                 CloudConfig ccCopy = new CloudConfig();
205                 for (Entry<String,CloudIdentity> e:identityServices.entrySet()) {
206
207                         ccCopy.identityServices.put(e.getKey(), e.getValue().clone());
208                 }
209
210                 for (Entry<String,CloudSite> e:cloudSites.entrySet()) {
211
212                         ccCopy.cloudSites.put(e.getKey(), e.getValue().clone());
213                 }
214
215                 ccCopy.configFilePath = this.configFilePath;
216
217                 ccCopy.refreshTimerInMinutes = this.refreshTimerInMinutes;
218
219                 return ccCopy;
220         }
221
222         @Override
223         public int hashCode() {
224                 final int prime = 31;
225                 int result = 1;
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;
230                 return result;
231         }
232
233         @Override
234         public boolean equals(Object obj) {
235                 if (this == obj)
236                         return true;
237                 if (obj == null)
238                         return false;
239                 if (getClass() != obj.getClass())
240                         return false;
241                 CloudConfig other = (CloudConfig) obj;
242                 if (cloudSites == null) {
243                         if (other.cloudSites != null)
244                                 return false;
245                 } else if (!cloudSites.equals(other.cloudSites))
246                         return false;
247                 if (configFilePath == null) {
248                         if (other.configFilePath != null)
249                                 return false;
250                 } else if (!configFilePath.equals(other.configFilePath))
251                         return false;
252                 if (identityServices == null) {
253                         if (other.identityServices != null)
254                                 return false;
255                 } else if (!identityServices.equals(other.identityServices))
256                         return false;
257                 if (refreshTimerInMinutes != other.refreshTimerInMinutes)
258                         return false;
259                 return true;
260         }
261 }
262