removed code smells
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / impl / ProviderCache.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.iaas.impl;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 /**
30  * This class maintains a cache of information by provider, where a provider is identified by both a type and an
31  * identity URL used to connect to that provider.
32  * <p>
33  * Providers may be multi-tenant, such as OpenStack, where the available services and resources vary from one tenant to
34  * another. Therefore, the provider cache maintains a cache of tenants and the service catalogs for each, as well as the
35  * credentials used to access the tenants, and a pool of Context objects for each tenant. The context pool allows use of
36  * the CDP abstraction layer to access the services of the provider within the specific tenant.
37  * </p>
38  */
39 public class ProviderCache {
40
41     /**
42      * The type of provider (e.g., OpenStackProvider) used to setup the CDP abstraction layer and load the appropriate
43      * support
44      */
45     private String providerType;
46
47     /**
48      * The URL of the provider's identity service or whatever service is used to login and authenticate to the provider
49      */
50     private String identityURL;
51
52     /**
53      * A string used to identify the provider instance
54      */
55     private String providerName;
56
57     /**
58      * The map of tenant cache objects by tenant id
59      */
60     private Map<String /* tenant id */, TenantCache> tenants = new HashMap<>();
61
62     /**
63      * @return the value of providerType
64      */
65     public String getProviderType() {
66         return providerType;
67     }
68
69     /**
70      * This method is called to initialize the provider cache, set up the context pools for each of the tenants,
71      * discover all of the regions supported on the provider, and load all of the service catalogs for each provider.
72      */
73     public void initialize() {
74         for (Map.Entry<String, TenantCache> entry : tenants.entrySet()) {
75             entry.getValue().initialize();
76         }
77     }
78
79     /**
80      * @param providerType the value for providerType
81      */
82     public void setProviderType(String providerType) {
83         this.providerType = providerType;
84     }
85
86     /**
87      * @return the value of identityURL
88      */
89     public String getIdentityURL() {
90         return identityURL;
91     }
92
93     /**
94      * @param identityURL the value for identityURL
95      */
96     public void setIdentityURL(String identityURL) {
97         this.identityURL = identityURL;
98     }
99
100     /**
101      * @return the value of providerName
102      */
103     public String getProviderName() {
104         return providerName;
105     }
106
107     /**
108      * @param providerName the value for providerName
109      */
110     public void setProviderName(String providerName) {
111         this.providerName = providerName;
112     }
113
114     /**
115      * @return the value of tenants
116      */
117     public Map<String, TenantCache> getTenants() {
118         return tenants;
119     }
120
121     /**
122      * This method is a helper to return a specific TenantCache
123      * 
124      * @param tenantId
125      * @return
126      */
127     public TenantCache getTenant(String tenantId) {
128         return tenants.get(tenantId);
129     }
130
131     // Previously there was no way to add additional tenants to the tenant cache
132     /**
133      * This method is used to add a tenant to the provider cache
134      * 
135      * @param tenantId
136      * @param UserId
137      * @param password
138      * @return the new initialized TenantCache or null if unsuccessful
139      */
140     public TenantCache addTenant(String tenantId, String tenantName, String userId, String password, String domain) {
141         if (tenantId != null || tenantName != null && userId != null && password != null) {
142             TenantCache tenant = new TenantCache(this);
143             if (tenantId != null) {
144                 tenant.setTenantId(tenantId);
145             }
146             if (tenantName != null) {
147                 tenant.setTenantName(tenantName);
148             }
149             tenant.setUserid(userId);
150             tenant.setPassword(password);
151             tenant.setDomain(domain);
152
153             if (identityURL != null) {
154                 tenant.initialize();
155             }
156
157             if (tenant.isInitialized()) {
158                 tenants.put(tenant.getTenantId(), tenant);
159                 return tenant;
160             }
161         }
162         return null;
163     }
164 }