2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.iaas.impl;
27 import java.io.IOException;
28 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import org.openecomp.appc.Constants;
35 import org.openecomp.appc.configuration.Configuration;
36 import org.openecomp.appc.configuration.ConfigurationFactory;
37 import org.openecomp.appc.i18n.Msg;
38 import org.openecomp.appc.pool.Allocator;
39 import org.openecomp.appc.pool.Destructor;
40 import org.openecomp.appc.pool.Pool;
41 import org.openecomp.appc.pool.PoolSpecificationException;
42 import com.att.cdp.exceptions.ContextConnectionException;
43 import com.att.cdp.exceptions.ZoneException;
44 import com.att.cdp.zones.Context;
45 import com.att.cdp.zones.ContextFactory;
46 import com.att.cdp.zones.Provider;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFManager;
49 import com.woorea.openstack.connector.JaxRs20Connector;
50 //import com.sun.jersey.api.client.ClientHandlerException;
51 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
54 * This class maintains a cache of tenants within a specific provider.
56 * Providers may be multi-tenant, such as OpenStack, where the available services and resources vary from one tenant to
57 * another. Therefore, the provider cache maintains a cache of tenants and the service catalogs for each, as well as the
58 * credentials used to access the tenants, and a pool of Context objects for each tenant. The context pool allows use of
59 * the CDP abstraction layer to access the services of the provider within the specific tenant.
62 public class TenantCache implements Allocator<Context>, Destructor<Context> {
64 public static final String POOL_PROVIDER_NAME = "pool.provider.name";
65 public static final String POOL_TENANT_NAME = "pool.tenant.name";
66 //public static final String CLIENT_CONNECTOR_CLASS = "com.woorea.openstack.connector.JerseyConnector";
67 public static final String CLIENT_CONNECTOR_CLASS = "com.woorea.openstack.connector.JaxRs20Connector";
69 * The provider we are part of
71 private ProviderCache provider;
74 * The password used to authenticate
76 private String password;
79 * The context pools by region used to access this tenant
81 private Map<String /* region */, Pool<Context>> pools = new HashMap<>();
86 private String tenantId;
91 private String tenantName;
94 * The user id used to authenticate
96 private String userid;
99 * The configuration of this adapter
101 private Configuration configuration;
104 * The service catalog for this provider
106 private ServiceCatalog catalog;
109 * Set to true when the cache has been initialized
111 private boolean initialized;
116 private EELFLogger logger;
119 * Construct the cache of tenants for the specified provider
124 public TenantCache(ProviderCache provider) {
125 configuration = ConfigurationFactory.getConfiguration();
126 logger = EELFManager.getInstance().getLogger(getClass());
127 this.provider = provider;
128 configuration = ConfigurationFactory.getConfiguration();
132 * @return True when the cache has been initialized. A tenant cache is initialized when the service catalog for the
133 * tenant on the specified provider has been loaded and processed.
135 public boolean isInitialized() {
140 * Initializes the tenant cache.
142 * This method authenticates to the provider and obtains the service catalog. For the service catalog we can
143 * determine all supported regions for this provider, as well as all published services and their endpoints. We will
144 * cache and maintain a copy of the service catalog for later queries.
147 * Once the catalog has been obtained, we create a context pool for each region defined. The context allows access
148 * to services of a single region only, so we need a separate context by region. It is possible to operate on
149 * resources that span regions, but to do so will require acquiring a context for each region of interest.
152 * The context pool maintains the reusable context objects and allocates them as needed. This class is registered as
153 * the allocator and destructor for the pool, so that we can create a new context when needed, and close it when no
157 public void initialize() {
158 logger.debug("Initializing TenantCache");
160 int min = configuration.getIntegerProperty(Constants.PROPERTY_MIN_POOL_SIZE);
161 int max = configuration.getIntegerProperty(Constants.PROPERTY_MAX_POOL_SIZE);
162 int delay = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_DELAY);
163 int limit = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_LIMIT);
165 String url = provider.getIdentityURL();
166 String tenant = tenantName == null ? tenantId : tenantName;
167 Properties properties = configuration.getProperties();
170 while (attempt <= limit) {
172 catalog = new ServiceCatalog(url, tenant, userid, password, properties);
173 tenantId = catalog.getTenantId();
174 tenantName = catalog.getTenantName();
176 for (String region : catalog.getRegions()) {
178 Pool<Context> pool = new Pool<>(min, max);
179 pool.setProperty(ContextFactory.PROPERTY_IDENTITY_URL, url);
180 pool.setProperty(ContextFactory.PROPERTY_TENANT, tenantName);
181 pool.setProperty(ContextFactory.PROPERTY_CLIENT_CONNECTOR_CLASS, CLIENT_CONNECTOR_CLASS);
182 pool.setProperty(ContextFactory.PROPERTY_RETRY_DELAY,
183 configuration.getProperty(Constants.PROPERTY_RETRY_DELAY));
184 pool.setProperty(ContextFactory.PROPERTY_RETRY_LIMIT,
185 configuration.getProperty(Constants.PROPERTY_RETRY_LIMIT));
186 pool.setProperty(ContextFactory.PROPERTY_REGION, region);
187 if (properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS) != null) {
188 pool.setProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS,
189 properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS));
191 pool.setAllocator(this);
192 pool.setDestructor(this);
193 pools.put(region, pool);
194 logger.debug(String.format("Put pool for region %s", region));
195 } catch (PoolSpecificationException e) {
196 logger.error("Error creating pool", e);
202 } catch (ContextConnectionException e) {
204 if (attempt <= limit) {
205 logger.error(Msg.CONNECTION_FAILED_RETRY, provider.getProviderName(), url, tenantName, tenantId, e.getMessage(), Integer.toString(delay), Integer.toString(attempt),
206 Integer.toString(limit));
209 Thread.sleep(delay * 1000L);
210 } catch (InterruptedException ie) {
214 } catch ( ZoneException e) {
215 logger.error(e.getMessage());
221 logger.error(Msg.CONNECTION_FAILED, provider.getProviderName(), url);
226 * This method accepts a fully qualified compute node URL and uses that to determine which region of the provider
227 * hosts that compute node.
230 * The parsed URL of the compute node
231 * @return The region name, or null if no region of this tenant hosts that compute node.
233 public String determineRegion(VMURL url) {
234 logger.debug(String.format("Attempting to determine VM region for %s", url));
235 String region = null;
236 Pattern urlPattern = Pattern.compile("[^:]+://([^:/]+)(?::([0-9]+)).*");
239 for (Endpoint endpoint : catalog.getEndpoints(ServiceCatalog.COMPUTE_SERVICE)) {
240 String endpointUrl = endpoint.getPublicURL();
241 Matcher matcher = urlPattern.matcher(endpointUrl);
242 if (matcher.matches()) {
243 if (url.getHost().equals(matcher.group(1))) {
244 if (url.getPort() != null) {
245 if (!url.getPort().equals(matcher.group(2))) {
250 region = endpoint.getRegion();
256 logger.debug(String.format("Region for %s is %s", url, region));
261 * @return the value of provider
263 public ProviderCache getProvider() {
269 * the value for provider
271 public void setProvider(ProviderCache provider) {
272 this.provider = provider;
276 * @return the value of password
278 public String getPassword() {
284 * the value for password
286 public void setPassword(String password) {
287 this.password = password;
291 * @return the value of tenantId
293 public String getTenantId() {
299 * the value for tenantId
301 public void setTenantId(String tenantId) {
302 this.tenantId = tenantId;
306 * @return the value of tenantName
308 public String getTenantName() {
314 * the value for tenantName
316 public void setTenantName(String tenantName) {
317 this.tenantName = tenantName;
321 * @return the value of userid
323 public String getUserid() {
329 * the value for userid
331 public void setUserid(String userid) {
332 this.userid = userid;
336 * @return the value of pools
338 public Map<String, Pool<Context>> getPools() {
343 * @see org.openecomp.appc.pool.Allocator#allocate(org.openecomp.appc.pool.Pool)
345 @SuppressWarnings("unchecked")
347 public Context allocate(Pool<Context> pool) {
348 logger.debug("Allocationg context for pool");
349 Class<? extends Provider> providerClass;
351 providerClass = (Class<? extends Provider>) Class.forName("com.att.cdp.openstack.OpenStackProvider");
352 // String providerType = provider.getProviderType();
354 // Context context = ContextFactory.getContext(providerType, pool.getProperties());
355 Context context = ContextFactory.getContext(providerClass, pool.getProperties());
356 context.login(userid, password);
358 } catch (IllegalStateException | IllegalArgumentException | ZoneException | ClassNotFoundException e) {
359 logger.debug("Failed to allocate context for pool", e);
366 * @see org.openecomp.appc.pool.Destructor#destroy(java.lang.Object, org.openecomp.appc.pool.Pool)
369 public void destroy(Context context, Pool<Context> pool) {
372 } catch (IOException e) {
378 * @return the service catalog for this provider
380 public ServiceCatalog getServiceCatalog() {