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