ffbdaf0a01271861368d1709629b2020f0e4377d
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / openecomp / appc / adapter / iaas / impl / ProviderCache.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.iaas.impl;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 /**
31  * This class maintains a cache of information by provider, where a provider is identified by both a type and an
32  * identity URL used to connect to that provider.
33  * <p>
34  * Providers may be multi-tenant, such as OpenStack, where the available services and resources vary from one tenant to
35  * another. Therefore, the provider cache maintains a cache of tenants and the service catalogs for each, as well as the
36  * credentials used to access the tenants, and a pool of Context objects for each tenant. The context pool allows use of
37  * the CDP abstraction layer to access the services of the provider within the specific tenant.
38  * </p>
39  */
40 public class ProviderCache {
41
42     /**
43      * The type of provider (e.g., OpenStackProvider) used to setup the CDP abstraction layer and load the appropriate
44      * support
45      */
46     private String providerType;
47
48     /**
49      * The URL of the provider's identity service or whatever service is used to login and authenticate to the provider
50      */
51     private String identityURL;
52
53     /**
54      * A string used to identify the provider instance
55      */
56     private String providerName;
57
58     /**
59      * The map of tenant cache objects by tenant id
60      */
61     private Map<String /* tenant id */, TenantCache> tenants = new HashMap<String, TenantCache>();
62
63     /**
64      * @return the value of providerType
65      */
66     public String getProviderType() {
67         return providerType;
68     }
69
70     /**
71      * This method is called to initialize the provider cache, set up the context pools for each of the tenants,
72      * discover all of the regions supported on the provider, and load all of the service catalogs for each provider.
73      */
74     public void initialize() {
75         for (Map.Entry<String, TenantCache> entry: tenants.entrySet()) { 
76             entry.getValue().initialize(); 
77         }
78     }
79
80     /**
81      * @param providerType
82      *            the value for providerType
83      */
84     public void setProviderType(String providerType) {
85         this.providerType = providerType;
86     }
87
88     /**
89      * @return the value of identityURL
90      */
91     public String getIdentityURL() {
92         return identityURL;
93     }
94
95     /**
96      * @param identityURL
97      *            the value for identityURL
98      */
99     public void setIdentityURL(String identityURL) {
100         this.identityURL = identityURL;
101     }
102
103     /**
104      * @return the value of providerName
105      */
106     public String getProviderName() {
107         return providerName;
108     }
109
110     /**
111      * @param providerName
112      *            the value for providerName
113      */
114     public void setProviderName(String providerName) {
115         this.providerName = providerName;
116     }
117
118     /**
119      * @return the value of tenants
120      */
121     public Map<String, TenantCache> getTenants() {
122         return tenants;
123     }
124     
125     /**
126      * This method is a helper to return a specific TenantCache
127      * 
128         * @param tenantId
129         * @return
130      */
131     public TenantCache getTenant(String tenantId){        
132         return tenants.get(tenantId);       
133     }
134     
135     // Previously there was no way to add additional tenants to the tenant cache
136     /**
137      * This method is used to add a tenant to the provider cache
138      * 
139         * @param tenantId
140         * @param UserId
141         * @param password
142         * @return the new initialized TenantCache or null if unsuccessful
143         */
144     public TenantCache addTenant(String tenantId, String tenantName, String userId, String password){
145         if(tenantId != null || tenantName != null && userId != null && password != null){        
146             TenantCache tenant = new TenantCache(this);
147             if(tenantId != null){
148                 tenant.setTenantId(tenantId);
149             }
150             if(tenantName != null){
151                 tenant.setTenantName(tenantName);
152             }
153             tenant.setUserid(userId);
154             tenant.setPassword(password);
155             
156             if(identityURL != null){
157                 tenant.initialize();
158             }
159             
160             if (tenant.isInitialized()) {
161                 tenants.put(tenant.getTenantId(), tenant);
162                 return tenant;
163             }
164         }
165         return null;
166     }
167 }