b32ca1833f2d96f87778ac0dddeaa81fc2244791
[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.Optional;
24
25 import com.fasterxml.jackson.annotation.JsonRootName;
26
27 import org.onap.so.db.catalog.beans.CloudSite;
28 import org.onap.so.db.catalog.beans.CloudifyManager;
29 import org.onap.so.db.catalog.client.CatalogDbClient;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32
33 /**
34  * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
35  * configuration file to Java. The CloudConfig contains information about
36  * Openstack cloud configurations. It includes: 
37  * - CloudIdentity objects,representing DCP nodes (Openstack Identity Service) 
38  * - CloudSite objects, representing LCP nodes (Openstack Compute & other services)
39  *
40  * Note that this is only used to access Cloud Configurations loaded from a JSON
41  * config file, so there are no explicit property setters.
42  *
43  * This class also contains methods to query cloud sites and/or identity
44  * services by ID.
45  *
46  */
47
48 @JsonRootName("cloud_config")
49 @Component
50 public class CloudConfig {
51         
52     private static final String CLOUD_SITE_VERSION = "2.5";
53     private static final String DEFAULT_CLOUD_SITE_ID = "DEFAULT";
54
55     @Autowired
56     private CatalogDbClient catalogDbClient;
57
58     /**
59      * Get a specific CloudSites, based on an ID. The ID is first checked
60      * against the regions, and if no match is found there, then against
61      * individual entries to try and find one with a CLLI that matches the ID
62      * and an AIC version of 2.5.
63      * 
64      * @param id the ID to match
65      * @return an Optional of CloudSite object.
66      */
67      public synchronized Optional<CloudSite> getCloudSite(String id) {
68          if (id == null) {
69              return Optional.empty();
70          }
71          CloudSite cloudSite = catalogDbClient.getCloudSite(id);
72
73          if (cloudSite != null) {
74              return Optional.of(cloudSite);
75          } else {
76              return getCloudSiteWithClli(id);
77          }
78      }
79     
80     /**
81      * Get a specific CloudSites, based on a CLLI and (optional) version, which
82      * will be matched against the aic_version field of the CloudSite.
83      * 
84      * @param clli
85      *            the CLLI to match
86      * @return a CloudSite, or null of no match found
87      */
88     private Optional<CloudSite> getCloudSiteWithClli(String clli) {
89         Optional <CloudSite> cloudSiteOptional = Optional.ofNullable(catalogDbClient.getCloudSiteByClliAndAicVersion(clli,CLOUD_SITE_VERSION));
90         if (cloudSiteOptional.isPresent()) {
91                 return cloudSiteOptional;
92         } else {
93                 return getDefaultCloudSite(clli);
94         }
95     }
96
97     private Optional<CloudSite> getDefaultCloudSite(String clli) {
98         Optional<CloudSite> cloudSiteOpt = Optional.ofNullable(catalogDbClient.getCloudSite(DEFAULT_CLOUD_SITE_ID));
99         if (cloudSiteOpt.isPresent()) {
100             CloudSite defaultCloudSite = cloudSiteOpt.get();
101             CloudSite clone = new CloudSite(defaultCloudSite);
102             clone.setRegionId(clli);
103             clone.setId(clli);
104             return Optional.of(clone);
105         } else {
106             return Optional.empty();
107         }
108     }
109
110         /**
111          * Get a specific CloudifyManager, based on an ID.
112          * @param id the ID to match
113          * @return a CloudifyManager, or null of no match found
114          */
115         public CloudifyManager getCloudifyManager (String id) {
116         return catalogDbClient.getCloudifyManager(id);
117         }
118 }