diff options
Diffstat (limited to 'openstack-client/src/main/java/com/woorea/openstack/base')
13 files changed, 499 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(); + +} |