2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.adapter.iaas.impl;
24 import java.io.IOException;
25 import java.util.HashMap;
27 import java.util.Properties;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 import org.openecomp.appc.Constants;
32 import org.openecomp.appc.configuration.Configuration;
33 import org.openecomp.appc.configuration.ConfigurationFactory;
34 import org.openecomp.appc.i18n.Msg;
35 import org.openecomp.appc.pool.Allocator;
36 import org.openecomp.appc.pool.Destructor;
37 import org.openecomp.appc.pool.Pool;
38 import org.openecomp.appc.pool.PoolSpecificationException;
39 import com.att.cdp.exceptions.ContextConnectionException;
40 import com.att.cdp.exceptions.ZoneException;
41 import com.att.cdp.zones.Context;
42 import com.att.cdp.zones.ContextFactory;
43 import com.att.cdp.zones.Provider;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
47 import com.sun.jersey.api.client.ClientHandlerException;
48 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
51 * This class maintains a cache of tenants within a specific provider.
53 * Providers may be multi-tenant, such as OpenStack, where the available services and resources vary from one tenant to
54 * another. Therefore, the provider cache maintains a cache of tenants and the service catalogs for each, as well as the
55 * credentials used to access the tenants, and a pool of Context objects for each tenant. The context pool allows use of
56 * the CDP abstraction layer to access the services of the provider within the specific tenant.
59 public class TenantCache implements Allocator<Context>, Destructor<Context> {
61 public static final String POOL_PROVIDER_NAME = "pool.provider.name";
62 public static final String POOL_TENANT_NAME = "pool.tenant.name";
63 public static final String CLIENT_CONNECTOR_CLASS = "com.woorea.openstack.connector.JerseyConnector";
66 * The provider we are part of
68 private ProviderCache provider;
71 * The password used to authenticate
73 private String password;
76 * The context pools by region used to access this tenant
78 private Map<String /* region */, Pool<Context>> pools = new HashMap<>();
83 private String tenantId;
88 private String tenantName;
91 * The user id used to authenticate
93 private String userid;
96 * The configuration of this adapter
98 private Configuration configuration;
101 * The service catalog for this provider
103 private ServiceCatalog catalog;
106 * Set to true when the cache has been initialized
108 private boolean initialized;
113 private EELFLogger logger;
116 * Construct the cache of tenants for the specified provider
121 public TenantCache(ProviderCache provider) {
122 configuration = ConfigurationFactory.getConfiguration();
123 logger = EELFManager.getInstance().getLogger(getClass());
124 this.provider = provider;
125 configuration = ConfigurationFactory.getConfiguration();
129 * @return True when the cache has been initialized. A tenant cache is initialized when the service catalog for the
130 * tenant on the specified provider has been loaded and processed.
132 public boolean isInitialized() {
137 * Initializes the tenant cache.
139 * This method authenticates to the provider and obtains the service catalog. For the service catalog we can
140 * determine all supported regions for this provider, as well as all published services and their endpoints. We will
141 * cache and maintain a copy of the service catalog for later queries.
144 * Once the catalog has been obtained, we create a context pool for each region defined. The context allows access
145 * to services of a single region only, so we need a separate context by region. It is possible to operate on
146 * resources that span regions, but to do so will require acquiring a context for each region of interest.
149 * The context pool maintains the reusable context objects and allocates them as needed. This class is registered as
150 * the allocator and destructor for the pool, so that we can create a new context when needed, and close it when no
154 public void initialize() {
155 logger.debug("Initializing TenantCache");
157 int min = configuration.getIntegerProperty(Constants.PROPERTY_MIN_POOL_SIZE);
158 int max = configuration.getIntegerProperty(Constants.PROPERTY_MAX_POOL_SIZE);
159 int delay = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_DELAY);
160 int limit = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_LIMIT);
162 String url = provider.getIdentityURL();
163 String tenant = tenantName == null ? tenantId : tenantName;
164 Properties properties = configuration.getProperties();
167 while (attempt <= limit) {
169 catalog = new ServiceCatalog(url, tenant, userid, password, properties);
170 tenantId = catalog.getTenantId();
171 tenantName = catalog.getTenantName();
173 for (String region : catalog.getRegions()) {
175 Pool<Context> pool = new Pool<>(min, max);
176 pool.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, url);
177 pool.setProperty(ContextFactory.PROPERTY_TENANT, tenantName);
178 pool.setProperty(ContextFactory.PROPERTY_CLIENT_CONNECTOR_CLASS, CLIENT_CONNECTOR_CLASS);
179 pool.setProperty(ContextFactory.PROPERTY_RETRY_DELAY,
180 configuration.getProperty(Constants.PROPERTY_RETRY_DELAY));
181 pool.setProperty(ContextFactory.PROPERTY_RETRY_LIMIT,
182 configuration.getProperty(Constants.PROPERTY_RETRY_LIMIT));
183 pool.setProperty(ContextFactory.PROPERTY_REGION, region);
184 if (properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS) != null) {
185 pool.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS,
186 properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS));
188 pool.setAllocator(this);
189 pool.setDestructor(this);
190 pools.put(region, pool);
191 logger.debug(String.format("Put pool for region %s", region));
192 } catch (PoolSpecificationException e) {
193 logger.error("Error creating pool", e);
199 } catch (ContextConnectionException e) {
201 logger.error(Msg.CONNECTION_FAILED_RETRY, provider.getProviderName(), url, tenantName, tenantId, e.getMessage(), Integer.toString(delay), Integer.toString(attempt),
202 Integer.toString(limit));
205 Thread.sleep(delay * 1000L);
206 } catch (InterruptedException ie) {
209 } catch (ClientHandlerException | ZoneException e) {
210 logger.error(e.getMessage());
216 logger.error(Msg.CONNECTION_FAILED, provider.getProviderName(), url);
221 * This method accepts a fully qualified compute node URL and uses that to determine which region of the provider
222 * hosts that compute node.
225 * The parsed URL of the compute node
226 * @return The region name, or null if no region of this tenant hosts that compute node.
228 public String determineRegion(VMURL url) {
229 logger.debug(String.format("Attempting to determine VM region for %s", url));
230 String region = null;
231 Pattern urlPattern = Pattern.compile("[^:]+://([^:/]+)(?::([0-9]+)).*");
234 for (Endpoint endpoint : catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE)) {
235 String endpointUrl = endpoint.getPublicURL();
236 Matcher matcher = urlPattern.matcher(endpointUrl);
237 if (matcher.matches()) {
238 if (url.getHost().equals(matcher.group(1))) {
239 if (url.getPort() != null) {
240 if (!url.getPort().equals(matcher.group(2))) {
245 region = endpoint.getRegion();
251 logger.debug(String.format("Region for %s is %s", url, region));
256 * @return the value of provider
258 public ProviderCache getProvider() {
264 * the value for provider
266 public void setProvider(ProviderCache provider) {
267 this.provider = provider;
271 * @return the value of password
273 public String getPassword() {
279 * the value for password
281 public void setPassword(String password) {
282 this.password = password;
286 * @return the value of tenantId
288 public String getTenantId() {
294 * the value for tenantId
296 public void setTenantId(String tenantId) {
297 this.tenantId = tenantId;
301 * @return the value of tenantName
303 public String getTenantName() {
309 * the value for tenantName
311 public void setTenantName(String tenantName) {
312 this.tenantName = tenantName;
316 * @return the value of userid
318 public String getUserid() {
324 * the value for userid
326 public void setUserid(String userid) {
327 this.userid = userid;
331 * @return the value of pools
333 public Map<String, Pool<Context>> getPools() {
338 * @see org.openecomp.appc.pool.Allocator#allocate(org.openecomp.appc.pool.Pool)
340 @SuppressWarnings("unchecked")
342 public Context allocate(Pool<Context> pool) {
343 logger.debug("Allocationg context for pool");
344 Class<? extends Provider> providerClass;
346 providerClass = (Class<? extends Provider>) Class.forName("com.att.cdp.openstack.OpenStackProvider");
347 // String providerType = provider.getProviderType();
349 // Context context = ContextFactory.getContext(providerType, pool.getProperties());
350 Context context = ContextFactory.getContext(providerClass, pool.getProperties());
351 context.login(userid, password);
353 } catch (IllegalStateException | IllegalArgumentException | ZoneException | ClassNotFoundException e) {
354 logger.debug("Failed to allocate context for pool", e);
361 * @see org.openecomp.appc.pool.Destructor#destroy(java.lang.Object, org.openecomp.appc.pool.Pool)
364 public void destroy(Context context, Pool<Context> pool) {
367 } catch (IOException e) {
373 * @return the service catalog for this provider
375 public ServiceCatalog getServiceCatalog() {