diff options
author | ChrisC <cc697w@intl.att.com> | 2017-01-31 13:57:24 +0100 |
---|---|---|
committer | ChrisC <cc697w@intl.att.com> | 2017-01-31 14:55:11 +0100 |
commit | 2e984294ac28c6f2ede290c38164c5d536ccaf4a (patch) | |
tree | 5eba5a929b7a961c98749fa69e03cfea58e1a724 /openstack-client/src/main/java/com/woorea/openstack | |
parent | 86c0f28c8ed469486b64d6422dc53e3a7bcc8adb (diff) |
Initial OpenECOMP MSO OpenStack SDK lib commit
Change-Id: Ieaacb2b2c0dcc469669880e73f0cda9fa59a6c5a
Signed-off-by: ChrisC <cc697w@intl.att.com>
Diffstat (limited to 'openstack-client/src/main/java/com/woorea/openstack')
16 files changed, 747 insertions, 0 deletions
diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/Entity.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/Entity.java new file mode 100644 index 0000000..42eb5fd --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/Entity.java @@ -0,0 +1,54 @@ +package com.woorea.openstack.base.client; + +import java.io.InputStream; + + +public class Entity<T> { + + private T entity; + + private String contentType; + + public static <T> Entity<T> json(T entity) { + return new Entity<T>(entity, "application/json"); + } + + public static <T> Entity<T> stream(T entity) { + return new Entity<T>(entity, "application/octet-stream"); + } + + public Entity(T entity, String contentType) { + super(); + this.entity = entity; + this.contentType = contentType; + } + + /** + * @return the entity + */ + public T getEntity() { + return entity; + } + + /** + * @param entity the entity to set + */ + public void setEntity(T entity) { + this.entity = entity; + } + + /** + * @return the contentType + */ + public String getContentType() { + return contentType; + } + + /** + * @param contentType the contentType to set + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/HttpMethod.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/HttpMethod.java new file mode 100644 index 0000000..6c62ffb --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/HttpMethod.java @@ -0,0 +1,5 @@ +package com.woorea.openstack.base.client; + +public enum HttpMethod { + HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS, TRACE +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackBaseException.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackBaseException.java new file mode 100644 index 0000000..1827ae1 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackBaseException.java @@ -0,0 +1,46 @@ +/*
+ * ============LICENSE_START==========================================
+ * ===================================================================
+ * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END============================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ *
+ */
+
+package com.woorea.openstack.base.client;
+
+/**
+ * A common abstract parent of all Openstack Exception types, allowing
+ * calling classes the choice to catch all error exceptions together.
+ */
+public abstract class OpenStackBaseException extends RuntimeException
+{
+ private static final long serialVersionUID = 1L;
+
+ /*
+ * Implement only the basic constructors
+ */
+ public OpenStackBaseException () {}
+
+ public OpenStackBaseException(String message) {
+ super(message);
+ }
+
+ public OpenStackBaseException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClient.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClient.java new file mode 100644 index 0000000..57a6628 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClient.java @@ -0,0 +1,90 @@ +package com.woorea.openstack.base.client; + +/* + * Modifications copyright (c) 2017 AT&T Intellectual Property + */ + +import java.util.Properties; +import java.util.ServiceLoader; + +public class OpenStackClient { + + protected String endpoint; + + protected OpenStackTokenProvider tokenProvider; + + protected static int AUTHENTICATION_RETRIES = 1; + + protected OpenStackClientConnector connector; + + protected Properties properties = new Properties(); + + protected static OpenStackClientConnector DEFAULT_CONNECTOR; + + static { + ServiceLoader<OpenStackClientConnector> connectorLoader; + connectorLoader = ServiceLoader.load(OpenStackClientConnector.class); + + for (OpenStackClientConnector clientConnector : connectorLoader) { + DEFAULT_CONNECTOR = clientConnector; + break; + } + } + + public OpenStackClient(String endpoint) { + this.endpoint = endpoint; + this.connector = DEFAULT_CONNECTOR; + } + + public OpenStackClient(String endpoint, OpenStackClientConnector connector) { + this.endpoint = endpoint; + this.connector = (connector == null) ? DEFAULT_CONNECTOR : connector; + } + + public <T> OpenStackResponse request(OpenStackRequest<T> request) { + OpenStackResponseException authException = null; + System.out.println("Openstack query:"+request.toString()); + for (int i = 0; i <= AUTHENTICATION_RETRIES; i++) { + request.endpoint(endpoint); + + if (tokenProvider != null) { + request.header("X-Auth-Token", tokenProvider.getToken()); + } + + try { + return connector.request(request); + } catch (OpenStackResponseException e) { + if (e.getStatus() != OpenStackResponseStatus.NOT_AUTHORIZED + || tokenProvider == null) { + throw e; + } + authException = e; + tokenProvider.expireToken(); + } + } + + throw authException; + } + + public <T> T execute(OpenStackRequest<T> request) { + OpenStackResponse response = request(request); + return (request.returnType() != null && request.returnType() != Void.class) ? response.getEntity(request.returnType()) : null; + } + + public void property(String property, String value) { + properties.put(property, value); + } + + public void setTokenProvider(OpenStackTokenProvider tokenProvider) { + this.tokenProvider = tokenProvider; + } + + public void token(String token) { + setTokenProvider(new OpenStackSimpleTokenProvider(token)); + } + + public <R> OpenStackRequest<R> get(String path, Class<R> returnType) { + return new OpenStackRequest<R>(this, HttpMethod.GET, path, null, returnType); + } + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClientConnector.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClientConnector.java new file mode 100644 index 0000000..417b844 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackClientConnector.java @@ -0,0 +1,8 @@ +package com.woorea.openstack.base.client; + + +public interface OpenStackClientConnector { + + public <T> OpenStackResponse request(OpenStackRequest<T> request); + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackCommand.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackCommand.java new file mode 100644 index 0000000..5ef291f --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackCommand.java @@ -0,0 +1,8 @@ +package com.woorea.openstack.base.client; +//package org.openstack.base.client; +// +//public interface OpenStackCommand<R> { +// +// OpenStackRequest createRequest(OpenStackClient connector); +// +//} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackConnectException.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackConnectException.java new file mode 100644 index 0000000..aabcd1f --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackConnectException.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START========================================== + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + * + */ + +package com.woorea.openstack.base.client; + +/** + * Custom RuntimeException to report connection errors to Openstack endpoints. + * Must be a RuntimeException to conform with OpenstackClient interface, which + * does not declare specific Exceptions. + */ +public class OpenStackConnectException extends OpenStackBaseException { + + private static final long serialVersionUID = 7294957362769575271L; + + public OpenStackConnectException(String message) { + super(message); + } + + public OpenStackConnectException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackRequest.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackRequest.java new file mode 100644 index 0000000..94a545f --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackRequest.java @@ -0,0 +1,142 @@ +package com.woorea.openstack.base.client; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; + +public class OpenStackRequest<R> { + + private OpenStackClient client; + + public OpenStackRequest() { + + } + + public OpenStackRequest(OpenStackClient client, HttpMethod method, CharSequence path, Entity<?> entity, Class<R> returnType) { + this.client = client; + this.method = method; + this.path = new StringBuilder(path); + this.entity = entity; + this.returnType = returnType; + header("Accept", "application/json"); + } + + private String endpoint; + + private HttpMethod method; + + private StringBuilder path = new StringBuilder(); + + private Map<String, List<Object>> headers = new HashMap<String, List<Object>>(); + + private Entity<?> entity; + + private Class<R> returnType; + + public OpenStackRequest<R> endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + public String endpoint() { + return endpoint; + } + + public OpenStackRequest<R> method(HttpMethod method) { + this.method = method; + return this; + } + + public HttpMethod method() { + return method; + } + + public OpenStackRequest<R> path(String path) { + this.path.append(path); + return this; + } + + public String path() { + return path.toString(); + } + + public OpenStackRequest<R> header(String name, Object value) { + if(value != null) { + headers.put(name, Arrays.asList(value)); + } + return this; + } + + public Map<String, List<Object>> headers() { + return headers; + } + + public <T> Entity<T> entity(T entity, String contentType) { + return new Entity<T>(entity, contentType); + } + + public Entity<?> entity() { + return entity; + } + + public <T> Entity<T> json(T entity) { + return entity(entity, "application/json"); + } + + public void returnType(Class<R> returnType) { + this.returnType = returnType; + } + + public Class<R> returnType() { + return returnType; + } + + public R execute() { + return client.execute(this); + } + + public OpenStackResponse request() { + return client.request(this); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "OpenStackRequest [endpoint=" + endpoint + ", method=" + method + + ", path=" + path + ", headers=" + headers + ", entity=" + + entity + ", returnType=" + returnType + "]"; + } + + private Map<String, List<Object> > queryParams = new LinkedHashMap<String, List<Object> >(); + + public Map<String, List<Object> > queryParams() { + return queryParams; + } + + public OpenStackRequest<R> queryParam(String key, Object value) { + if (queryParams.containsKey(key)) { + List<Object> values = queryParams.get(key); + values.add(value); + } else { + List<Object> values = new ArrayList<Object>(); + values.add(value); + queryParams.put(key, values); + } + + return this; + } + + protected static String buildPath(String ... elements) { + StringBuilder stringBuilder = new StringBuilder(); + for (String element : elements) { + stringBuilder.append(element); + } + + return stringBuilder.toString(); + } +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponse.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponse.java new file mode 100644 index 0000000..1e09320 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponse.java @@ -0,0 +1,22 @@ +package com.woorea.openstack.base.client; + +/* + * Modifications copyright (c) 2017 AT&T Intellectual Property + */ + +import java.io.InputStream; +import java.util.Map; + +public interface OpenStackResponse { + + public <T> T getEntity(Class<T> returnType); + + public <T> T getErrorEntity(Class<T> returnType); + + public InputStream getInputStream(); + + public String header(String name); + + public Map<String, String> headers(); + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseException.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseException.java new file mode 100644 index 0000000..355c300 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseException.java @@ -0,0 +1,42 @@ +package com.woorea.openstack.base.client; + +/* + * Modifications copyright (c) 2017 AT&T Intellectual Property + */ + +public class OpenStackResponseException extends OpenStackBaseException { + + private static final long serialVersionUID = 7294957362769575271L; + + protected String message; + + protected int status; + + // Make the response available for exception handling (includes body) + protected OpenStackResponse response; + + public OpenStackResponseException(String message, int status) { + this.message = message; + this.status = status; + this.response = null; + } + + public OpenStackResponseException(String message, int status, OpenStackResponse response) { + this.message = message; + this.status = status; + this.response = response; + } + + public String getMessage() { + return message; + } + + public int getStatus() { + return status; + } + + public OpenStackResponse getResponse() { + return response; + } + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseStatus.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseStatus.java new file mode 100644 index 0000000..032a511 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackResponseStatus.java @@ -0,0 +1,11 @@ +package com.woorea.openstack.base.client; + +public class OpenStackResponseStatus { + + public static final int OK = 200; + + public static final int NOT_AUTHORIZED = 401; + + public static final int CONFLICT = 409; + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackSimpleTokenProvider.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackSimpleTokenProvider.java new file mode 100644 index 0000000..5ea8d76 --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackSimpleTokenProvider.java @@ -0,0 +1,20 @@ +package com.woorea.openstack.base.client; + +public class OpenStackSimpleTokenProvider implements OpenStackTokenProvider { + + String token; + + public OpenStackSimpleTokenProvider(String token) { + this.token = token; + } + + @Override + public String getToken() { + return this.token; + } + + @Override + public void expireToken() { + } + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackTokenProvider.java b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackTokenProvider.java new file mode 100644 index 0000000..0a8f1bf --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/base/client/OpenStackTokenProvider.java @@ -0,0 +1,9 @@ +package com.woorea.openstack.base.client; + +public interface OpenStackTokenProvider { + + String getToken(); + + void expireToken(); + +} diff --git a/openstack-client/src/main/java/com/woorea/openstack/common/client/AbstractOpenStackClient.java b/openstack-client/src/main/java/com/woorea/openstack/common/client/AbstractOpenStackClient.java new file mode 100644 index 0000000..e48ec0b --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/common/client/AbstractOpenStackClient.java @@ -0,0 +1,125 @@ +package com.woorea.openstack.common.client; +//package org.openstack.common.client; +// +//import java.io.IOException; +//import java.util.logging.Logger; +// +//import javax.ws.rs.client.ClientRequestContext; +//import javax.ws.rs.client.ClientRequestFilter; +//import javax.ws.rs.client.Entity; +//import javax.ws.rs.client.Invocation.Builder; +//import javax.ws.rs.client.WebTarget; +//import javax.ws.rs.core.MediaType; +// +//import org.glassfish.jersey.filter.LoggingFilter; +//import org.openstack.connector.OpenStack; +// +//public class AbstractOpenStackClient { +// +// protected String endpointURL; +// +// protected String token; +// +// protected LoggingFilter loggingFilter; +// +// protected ClientRequestFilter tokenFilter = new ClientRequestFilter() { +// +// @Override +// public void filter(ClientRequestContext requestContext) throws IOException { +// requestContext.getHeaders().putSingle("X-Auth-Token", token); +// } +// }; +// +// public AbstractOpenStackClient(String endpointURL, String token) { +// this.endpointURL = endpointURL; +// this.token = token; +// } +// +// public AbstractOpenStackClient(String endpointURL) { +// this(endpointURL, null); +// } +// +// /** +// * @param token the token to set +// */ +// public void setToken(String token) { +// this.token = token; +// } +// +// public OpenStackRequest request(String uri, String... mediaTypes) { +// WebTarget endpoint = OpenStack.CLIENT.target(endpointURL); +// if(token != null) { +// endpoint.register(tokenFilter); +// } +// return new OpenStackRequest(endpoint.path(uri).request(mediaTypes)); +// } +// +// public OpenStackRequest request(String uri) { +// return request(uri, MediaType.APPLICATION_JSON); +// } +// +// protected WebTarget create(String endpoint) { +// WebTarget target = OpenStack.CLIENT.target(endpoint); +// if(loggingFilter != null) { +// target.register(loggingFilter); +// } +// if(token != null) { +// target.register(tokenFilter); +// } +// return target; +// } +// +// public void enableLogging(Logger logger, int entitySize) { +// loggingFilter = new LoggingFilter(logger, entitySize); +// } +// +// public void disableLogging() { +// loggingFilter = null; +// } +// +// public static class OpenStackRequest { +// +// private Builder builder; +// +// private OpenStackRequest(Builder builder) { +// this.builder = builder; +// } +// +// public <ResponseType> ResponseType execute(String method, Class<ResponseType> type) { +// return builder.method(method, type); +// } +// +// public <RequestType, ResponseType> ResponseType execute(String method, Entity<RequestType> data, Class<ResponseType> type) { +// return builder.method(method, data, type); +// } +// +// public void execute(String method) { +// builder.method(method); +// } +// +// public <RequestType> void execute(String method, Entity<RequestType> data) { +// builder.method(method, data, Void.class); +// } +// +// public <ResponseType> ResponseType get(Class<ResponseType> type) { +// return execute("GET", type); +// } +// +// public <ResponseType> ResponseType postJson(Object data, Class<ResponseType> type) { +// return execute("POST", Entity.json(data), type); +// } +// +// public <ResponseType> ResponseType putJson(Object data, Class<ResponseType> type) { +// return execute("PUT", Entity.json(data), type); +// } +// +// public <ResponseType> ResponseType patchJson(Object data, Class<ResponseType> type) { +// return execute("PATCH", Entity.json(data), type); +// } +// +// public void delete() { +// execute("DELETE", Void.class); +// } +// } +// +//} diff --git a/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSession.java b/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSession.java new file mode 100644 index 0000000..a56a3af --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSession.java @@ -0,0 +1,107 @@ +package com.woorea.openstack.common.session; +//package org.openstack.common.session; +// +//import java.io.Serializable; +// +//import org.openstack.keystone.model.Access; +//import org.openstack.keystone.model.Authentication; +// +//public class OpenStackSession implements Serializable { +// +// private String authenticationURL; +// +// private Authentication authentication; +// +// private String identityAdministrationURL; +// +// private String identityAdministrationToken; +// +// private Access access; +// +// private boolean admin; +// +// /** +// * @return the authenticationURL +// */ +// public String getAuthenticationURL() { +// return authenticationURL; +// } +// +// /** +// * @param authenticationURL the authenticationURL to set +// */ +// public void setAuthenticationURL(String authenticationURL) { +// this.authenticationURL = authenticationURL; +// } +// +// /** +// * @return the authentication +// */ +// public Authentication getAuthentication() { +// return authentication; +// } +// +// /** +// * @param authentication the authentication to set +// */ +// public void setAuthentication(Authentication authentication) { +// this.authentication = authentication; +// } +// +// /** +// * @return the identityAdministrationURL +// */ +// public String getIdentityAdministrationURL() { +// return identityAdministrationURL; +// } +// +// /** +// * @param identityAdministrationURL the identityAdministrationURL to set +// */ +// public void setIdentityAdministrationURL(String identityAdministrationURL) { +// this.identityAdministrationURL = identityAdministrationURL; +// } +// +// /** +// * @return the identityAdministrationToken +// */ +// public String getIdentityAdministrationToken() { +// return identityAdministrationToken; +// } +// +// /** +// * @param identityAdministrationToken the identityAdministrationToken to set +// */ +// public void setIdentityAdministrationToken(String identityAdministrationToken) { +// this.identityAdministrationToken = identityAdministrationToken; +// } +// +// /** +// * @return the access +// */ +// public Access getAccess() { +// return access; +// } +// +// /** +// * @param access the access to set +// */ +// public void setAccess(Access access) { +// this.access = access; +// } +// +// /** +// * @return the admin +// */ +// public boolean isAdmin() { +// return admin; +// } +// +// /** +// * @param admin the admin to set +// */ +// public void setAdmin(boolean admin) { +// this.admin = admin; +// } +// +//} diff --git a/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSessionHolder.java b/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSessionHolder.java new file mode 100644 index 0000000..ea81f7c --- /dev/null +++ b/openstack-client/src/main/java/com/woorea/openstack/common/session/OpenStackSessionHolder.java @@ -0,0 +1,16 @@ +package com.woorea.openstack.common.session; +//package org.openstack.common.session; +// +//public class OpenStackSessionHolder { +// +// private static final ThreadLocal<OpenStackSession> HOLDER = new ThreadLocal<OpenStackSession>(); +// +// public static OpenStackSession getSession() { +// return HOLDER.get(); +// } +// +// public static void setSession(OpenStackSession session) { +// HOLDER.set(session); +// } +// +//} |