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