2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Copyright (C) 2017 Amdocs
\r
8 * ================================================================================
\r
9 * Modifications Copyright (C) 2019 Ericsson
\r
10 * =============================================================================
\r
11 * Licensed under the Apache License, Version 2.0 (the "License");
\r
12 * you may not use this file except in compliance with the License.
\r
13 * You may obtain a copy of the License at
\r
15 * http://www.apache.org/licenses/LICENSE-2.0
\r
17 * Unless required by applicable law or agreed to in writing, software
\r
18 * distributed under the License is distributed on an "AS IS" BASIS,
\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
20 * See the License for the specific language governing permissions and
\r
21 * limitations under the License.
\r
23 * ============LICENSE_END=========================================================
\r
26 package org.onap.appc.adapter.iaas.impl;
\r
28 import com.att.cdp.exceptions.ContextConnectionException;
\r
29 import com.att.cdp.exceptions.ZoneException;
\r
30 import com.att.cdp.openstack.util.ExceptionMapper;
\r
31 import com.att.cdp.pal.util.Time;
\r
32 import com.att.cdp.zones.ContextFactory;
\r
33 import com.att.cdp.zones.spi.RequestState;
\r
34 import com.att.eelf.configuration.EELFLogger;
\r
35 import com.att.eelf.configuration.EELFManager;
\r
36 import com.woorea.openstack.base.client.OpenStackBaseException;
\r
37 import com.woorea.openstack.base.client.OpenStackClientConnector;
\r
38 import com.woorea.openstack.base.client.OpenStackSimpleTokenProvider;
\r
39 import com.woorea.openstack.keystone.Keystone;
\r
40 import com.woorea.openstack.keystone.api.TokensResource;
\r
41 import com.woorea.openstack.keystone.model.Access;
\r
42 import com.woorea.openstack.keystone.model.Access.Service;
\r
43 import com.woorea.openstack.keystone.model.Access.Service.Endpoint;
\r
44 import com.woorea.openstack.keystone.model.Authentication;
\r
45 import com.woorea.openstack.keystone.model.Tenant;
\r
46 import com.woorea.openstack.keystone.model.authentication.UsernamePassword;
\r
48 import java.util.ArrayList;
\r
49 import java.util.Calendar;
\r
50 import java.util.Date;
\r
51 import java.util.HashMap;
\r
52 import java.util.List;
\r
53 import java.util.Map;
\r
54 import java.util.Properties;
\r
55 import java.util.Set;
\r
56 import java.util.concurrent.locks.Lock;
\r
57 import java.util.regex.Matcher;
\r
58 import java.util.regex.Pattern;
\r
61 * This class is used to capture and cache the service catalog for a specific OpenStack provider.
\r
63 * This is needed because the way the servers are represented in the ECOMP product is as their fully qualified URL's.
\r
64 * This is very problematic, because we cant identify their region from the URL, URL's change, and we cant identify the
\r
65 * versions of the service implementations. In otherwords, the URL does not provide us enough information.
\r
68 * The zone abstraction layer is designed to detect the versions of the services dynamically, and step up or down to
\r
69 * match those reported versions. In order to do that, we need to know before hand what region we are accessing (since
\r
70 * the supported versions may be different by regions). We will need to authenticate to the identity service in order to
\r
71 * do this, plus we have to duplicate the code supporting proxies and trusted hosts that exists in the abstraction
\r
72 * layer, but that cant be helped.
\r
75 * What we do to circumvent this is connect to the provider using the lowest supported identity api, and read the entire
\r
76 * service catalog into this object. Then, we parse the vm URL to extract the host and port and match that to the
\r
77 * compute services defined in the catalog. When we find a compute service that has the same host name and port,
\r
78 * whatever region that service is supporting is the region for that server.
\r
81 * While we really only need to do this for compute nodes, there is no telling what other situations may arise where the
\r
82 * full service catalog may be needed. Also, there is very little additional cost (additional RAM) associated with
\r
83 * caching the full service catalog since there is no way to list only a portion of it.
\r
86 public class ServiceCatalogV2 extends ServiceCatalog {
\r
88 protected static final EELFLogger loggerV2 = EELFManager.getInstance().getLogger(ServiceCatalogV2.class);
\r
91 * The Openstack Access object that manages the authenticated token and access control
\r
93 private Access access;
\r
96 * A map of endpoints for each service organized by service type
\r
98 private Map<String /* Service Type */, List<Service.Endpoint>> serviceEndpoints;
\r
101 * A map of service types that are published
\r
103 private Map<String /* Service Type */, Service> serviceTypes;
\r
106 * The tenant that we are accessing
\r
108 private Tenant tenant;
\r
111 * A "token provider" that manages the authentication token that we obtain when logging in
\r
113 private OpenStackSimpleTokenProvider tokenProvider;
\r
115 public ServiceCatalogV2(String identityURL, String tenantIdentifier, String principal, String credential,
\r
116 Properties properties) {
\r
117 super(identityURL, tenantIdentifier, principal, credential, null, properties);
\r
121 public void init() throws ZoneException {
\r
122 serviceTypes = new HashMap<>();
\r
123 serviceEndpoints = new HashMap<>();
\r
124 Class<?> connectorClass;
\r
125 OpenStackClientConnector connector;
\r
127 connectorClass = Class.forName(CLIENT_CONNECTOR_CLASS);
\r
128 connector = (OpenStackClientConnector) connectorClass.newInstance();
\r
129 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
\r
130 loggerV2.error(e.getMessage());
\r
133 Keystone keystone = getKeystone(identityURL, connector);
\r
135 String proxyHost = properties.getProperty(ContextFactory.PROPERTY_PROXY_HOST);
\r
136 String proxyPort = properties.getProperty(ContextFactory.PROPERTY_PROXY_PORT);
\r
137 String trustedHosts = properties.getProperty(ContextFactory.PROPERTY_TRUSTED_HOSTS, ""); //$NON-NLS-1$
\r
138 if (proxyHost != null && proxyHost.length() > 0) {
\r
139 keystone.getProperties().setProperty(com.woorea.openstack.common.client.Constants.PROXY_HOST, proxyHost);
\r
140 keystone.getProperties().setProperty(com.woorea.openstack.common.client.Constants.PROXY_PORT, proxyPort);
\r
142 if (trustedHosts != null) {
\r
143 keystone.getProperties().setProperty(com.woorea.openstack.common.client.Constants.TRUST_HOST_LIST,
\r
147 Authentication authentication = new UsernamePassword(principal, credential);
\r
148 TokensResource tokens = keystone.tokens();
\r
149 TokensResource.Authenticate authenticate = tokens.authenticate(authentication);
\r
150 if (projectIdentifier.length() == 32 && projectIdentifier.matches("[0-9a-fA-F]+")) { //$NON-NLS-1$
\r
151 authenticate = authenticate.withTenantId(projectIdentifier);
\r
153 authenticate = authenticate.withTenantName(projectIdentifier);
\r
157 * We have to set up the TrackRequest TLS collection for the ExceptionMapper
\r
160 RequestState.put(RequestState.PROVIDER, "OpenStackProvider");
\r
161 RequestState.put(RequestState.TENANT, projectIdentifier);
\r
162 RequestState.put(RequestState.PRINCIPAL, principal);
\r
165 access = authenticate.execute();
\r
166 //Ensure that access or the access token is not null before
\r
167 //checking local expiration or accessing the tenant information
\r
168 if (access == null || access.getToken() == null) {
\r
169 throw new NullPointerException("The access key used to access the provider or access token is null." +
\r
170 "Failed to init ServiceCatalogV2");
\r
172 expiresLocal = getLocalExpiration(access);
\r
173 tenant = access.getToken().getTenant();
\r
174 tokenProvider = new OpenStackSimpleTokenProvider(access.getToken().getId());
\r
175 keystone.setTokenProvider(tokenProvider);
\r
176 parseServiceCatalog(access.getServiceCatalog());
\r
177 } catch (OpenStackBaseException e) {
\r
178 ExceptionMapper.mapException(e);
\r
179 } catch (Exception ex) {
\r
180 throw new ContextConnectionException(ex.getMessage());
\r
188 public List<Service.Endpoint> getEndpoints(String serviceType) {
\r
189 Lock readLock = rwLock.readLock();
\r
192 return serviceEndpoints.get(serviceType);
\r
202 public String getProjectId() {
\r
203 Lock readLock = rwLock.readLock();
\r
206 return tenant.getId();
\r
216 public String getProjectName() {
\r
217 Lock readLock = rwLock.readLock();
\r
220 return tenant.getName();
\r
230 public Set<String> getRegions() {
\r
231 Lock readLock = rwLock.readLock();
\r
244 public List<String> getServiceTypes() {
\r
245 Lock readLock = rwLock.readLock();
\r
248 ArrayList<String> result = new ArrayList<>();
\r
249 result.addAll(serviceTypes.keySet());
\r
260 public String getVMRegion(VMURL url) {
\r
261 String region = null;
\r
266 Pattern urlPattern = Pattern.compile("[^:]+://([^:/]+)(?::([0-9]+)).*");
\r
268 for (Endpoint endpoint : getEndpoints(ServiceCatalog.COMPUTE_SERVICE)) {
\r
269 String endpointUrl = endpoint.getPublicURL();
\r
270 Matcher matcher = urlPattern.matcher(endpointUrl);
\r
271 if (!matcher.matches() ||
\r
272 !url.getHost().equals(matcher.group(1)) ||
\r
273 (url.getPort() != null && !url.getPort().equals(matcher.group(2))) ) {
\r
277 region = endpoint.getRegion();
\r
288 public boolean isServicePublished(String serviceType) {
\r
289 Lock readLock = rwLock.readLock();
\r
292 return serviceTypes.containsKey(serviceType);
\r
302 public String toString() {
\r
304 StringBuilder builder = new StringBuilder();
\r
305 Lock lock = rwLock.readLock();
\r
308 builder.append(String.format("Service Catalog: tenant %s, id[%s], description[%s]%n", tenant.getName(), //$NON-NLS-1$
\r
309 tenant.getId(), tenant.getDescription()));
\r
310 if (regions != null && !regions.isEmpty()) {
\r
311 builder.append(String.format("%d regions:%n", regions.size())); //$NON-NLS-1$
\r
312 for (String region : regions) {
\r
313 builder.append("\t" + region + "%n"); //$NON-NLS-1$ //$NON-NLS-2$
\r
316 builder.append(String.format("%d services:%n", serviceEndpoints.size())); //$NON-NLS-1$
\r
317 for(Map.Entry<String, List<Access.Service.Endpoint>> entry : serviceEndpoints.entrySet()){
\r
318 List<Service.Endpoint> endpoints = entry.getValue();
\r
319 Service service = serviceTypes.get(entry.getKey());
\r
321 builder.append(String.format("\t%s [%s] - %d endpoints%n", service.getType(), service.getName(), //$NON-NLS-1$
\r
322 endpoints.size()));
\r
323 for (Service.Endpoint endpoint : endpoints) {
\r
324 builder.append(String.format("\t\tRegion [%s], public URL [%s]%n", endpoint.getRegion(), //$NON-NLS-1$
\r
325 endpoint.getPublicURL()));
\r
332 return builder.toString();
\r
336 * Parses the service catalog and caches the results
\r
338 * @param services The list of services published by this provider
\r
340 private void parseServiceCatalog(List<Service> services) {
\r
341 Lock lock = rwLock.writeLock();
\r
344 serviceTypes.clear();
\r
345 serviceEndpoints.clear();
\r
348 for (Service service : services) {
\r
349 String type = service.getType();
\r
350 serviceTypes.put(type, service);
\r
352 List<Service.Endpoint> endpoints = service.getEndpoints();
\r
353 for (Service.Endpoint endpoint : endpoints) {
\r
354 List<Service.Endpoint> endpointList = serviceEndpoints.get(type);
\r
355 if (endpointList == null) {
\r
356 endpointList = new ArrayList<>();
\r
357 serviceEndpoints.put(type, endpointList);
\r
359 endpointList.add(endpoint);
\r
361 String region = endpoint.getRegion();
\r
362 if (!regions.contains(region)) {
\r
363 regions.add(region);
\r
373 * Computes the local time when the access token will expire, after which we will need to re-login to access the
\r
376 * @param accessKey The access key used to access the provider
\r
377 * @return The local time the key expires
\r
379 private static long getLocalExpiration(Access accessKey) {
\r
380 Date now = Time.getCurrentUTCDate();
\r
381 Calendar issued = accessKey.getToken().getIssued_at();
\r
382 Calendar expires = accessKey.getToken().getExpires();
\r
383 if (issued != null && expires != null) {
\r
384 long tokenLife = expires.getTimeInMillis() - issued.getTimeInMillis();
\r
385 return now.getTime() + tokenLife;
\r
387 return now.getTime();
\r
390 protected Keystone getKeystone(String identityUrl, OpenStackClientConnector connector) {
\r
391 return new Keystone(identityURL, connector);
\r