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