From f8a8d5192b1e5013d9e2f699be54b072ef39d5f0 Mon Sep 17 00:00:00 2001 From: talasila Date: Wed, 8 Feb 2017 10:13:29 -0500 Subject: Initial OpenECOMP UI/DMaapBC commit Change-Id: Ia492e1b88311b9bed4c31f593b28deaaad73b7e4 Signed-off-by: talasila --- .../dcae/dmaap/bcapi/client/DmaapBcRestClient.java | 1480 ++++++++++++++++++++ .../dmaap/bcapi/client/HttpStatusAndResponse.java | 66 + .../dmaap/bcapi/client/IRestClientConstants.java | 35 + .../dmaap/bcapi/client/SimpleRestClientBase.java | 273 ++++ 4 files changed, 1854 insertions(+) create mode 100644 dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/DmaapBcRestClient.java create mode 100644 dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/HttpStatusAndResponse.java create mode 100644 dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/IRestClientConstants.java create mode 100644 dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/SimpleRestClientBase.java (limited to 'dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap') diff --git a/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/DmaapBcRestClient.java b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/DmaapBcRestClient.java new file mode 100644 index 0000000..f8a58e9 --- /dev/null +++ b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/DmaapBcRestClient.java @@ -0,0 +1,1480 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller REST Client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dcae.dmaap.bcapi.client; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.client.utils.URIBuilder; +import org.openecomp.dcae.dmaapbc.model.DR_Node; +import org.openecomp.dcae.dmaapbc.model.DR_Pub; +import org.openecomp.dcae.dmaapbc.model.DR_Sub; +import org.openecomp.dcae.dmaapbc.model.DcaeLocation; +import org.openecomp.dcae.dmaapbc.model.Dmaap; +import org.openecomp.dcae.dmaapbc.model.DmaapObject; +import org.openecomp.dcae.dmaapbc.model.ErrorResponse; +import org.openecomp.dcae.dmaapbc.model.Feed; +import org.openecomp.dcae.dmaapbc.model.MR_Client; +import org.openecomp.dcae.dmaapbc.model.MR_Cluster; +import org.openecomp.dcae.dmaapbc.model.Topic; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Provides methods to communicate with the DMaaP Bus Controller REST API. This + * hides all JSON; instead it accepts and returns Java objects. + */ +public class DmaapBcRestClient extends SimpleRestClientBase { + + private static Logger logger = LoggerFactory.getLogger(DmaapBcRestClient.class); + + // Omit leading and trailing slashes here + private static final String DCAELOCATIONS = "dcaeLocations"; + private static final String DMAAP = "dmaap"; + private static final String DR_NODES = "dr_nodes"; + private static final String DR_PUBS = "dr_pubs"; + private static final String DR_SUBS = "dr_subs"; + private static final String FEEDS = "feeds"; + private static final String TOPICS = "topics"; + private static final String MR_CLUSTERS = "mr_clusters"; + private static final String MR_CLIENTS = "mr_clients"; + + /** + * Reusable JSON (de)serializer + */ + private final ObjectMapper mapper; + + /** + * URL of the DMAAP REST endpoint + */ + private final String dmaapRestUrl; + + /** + * Constructor that configures the client for the specified endpoint using + * no authentication. + * + * @param dmaapRestUrl + */ + public DmaapBcRestClient(final String dmaapRestUrl) { + super(); + this.dmaapRestUrl = dmaapRestUrl; + this.mapper = new ObjectMapper(); + // Don't serialize null-value fields in objects + this.mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * Constructor that onfigures the client for the specified endpoint using + * the specified username and password for basic HTTP authentication. + * + * @param dmaapRestUrl + * @param username + * @param password + */ + public DmaapBcRestClient(final String dmaapRestUrl, final String username, final String password) { + super(username, password); + this.dmaapRestUrl = dmaapRestUrl; + this.mapper = new ObjectMapper(); + // Don't serialize null-value fields in objects + this.mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * Configures the behavior of the JSON deserializer used to build business + * objects (e.g., a Feed) from REST responses. + * + * @param failOnUnknownProperties + * If true, rejects JSON responses with unexpected fields + * (default behavior); if false, ignores unexpected fields. + */ + public void setFailOnUnknownProperties(boolean failOnUnknownProperties) { + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties); + } + + /** + * Gets the DMaaP endpoint URL that is used by methods in this class. + * + * @return dmaapEndpointUrl + */ + public String getDmaapRestUrl() { + return this.dmaapRestUrl; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of DCAE locations. + * + * @return List of DmaapObject: list contains DcaeLocation object(s) on + * success; a single ErrorResponse object if the remote site rejects + * the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getDcaeLocations() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DCAELOCATIONS)); + logger.debug("getDcaeLocations: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDcaeLocations: unexpected null response"); + + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getDcaeLocations: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets the DCAE location with the specified name. + * + * @param locName + * name of the location to get + * @return DmaapObject: a DcaeLocation object on success; an ErrorResponse + * object if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getDcaeLocation(final String locName) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DCAELOCATIONS, locName)); + logger.debug("getDcaeLocation: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDcaeLocation: unexpected null response"); + + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), DcaeLocation.class); + } catch (Exception ex) { + logger.debug("getDcaeLocation: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a DCAE location in DMaaP. + * + * @param dcaeLoc + * DcaeLocation to be created + * @return Status and response: expect 200 and a DcaeLocation on success; a + * code and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postDcaeLocation(DcaeLocation dcaeLoc) throws Exception { + String jsonBody = mapper.writeValueAsString(dcaeLoc); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(DCAELOCATIONS), jsonBody); + if (hsr == null) + throw new Exception("postDcaeLocation: unexpected null response"); + logger.debug("postDcaeLocation: resp is {}", hsr); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postDcaeLocation: null response body"); + return response; + } + try { + DcaeLocation resp = mapper.readValue(hsr.getResponseString(), DcaeLocation.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDcaeLocation: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the DCAE location with the specified name. + * + * @param locName + * Name of the location to delete + * @return Status and response: expect 204 and a DcaeLocation on success; a + * code and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteDcaeLocation(final String locName) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(DCAELOCATIONS, locName)); + logger.debug("deleteDcaeLocation: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteDcaeLocation: unexpected null response"); + // Returns a loc on success, error message string on error. + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteDcaeLocation: null response body"); + return response; + } + try { + DcaeLocation resp = mapper.readValue(hsr.getResponseString(), DcaeLocation.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteDcaeLocation: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a DCAE location. + * + * @param dcaeLoc + * DCAE Location to be updated + * @return Status and response; expect 200 and a DcaeLocation on success, a + * string error on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putDcaeLocation(DcaeLocation dcaeLoc) throws Exception { + String jsonBody = mapper.writeValueAsString(dcaeLoc); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(DCAELOCATIONS, dcaeLoc.getDcaeLocationName()), jsonBody); + logger.debug("putDcaeLocation: resp is {}", hsr); + if (hsr == null) + throw new Exception("putDcaeLocation: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putDcaeLocation: null response body"); + return response; + } + try { + DcaeLocation resp = mapper.readValue(hsr.getResponseString(), DcaeLocation.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putDcaeLocation: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets the DMAAP instance for this DCAE deployment. + * + * @return DmaapObject: a Dmaap object on success; an ErrorResponse object + * if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getDmaap() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DMAAP)); + logger.debug("getDmaap: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDmaap: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), Dmaap.class); + } catch (Exception ex) { + logger.debug("getDmaap: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a new DMaaP set system wide configuration settings for the + * dcaeEnvironment + * + * @param dmaap + * @return Status and response: expect 200 and a Dmaap on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postDmaap(Dmaap dmaap) throws Exception { + String jsonBody = mapper.writeValueAsString(dmaap); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(DMAAP), jsonBody); + if (hsr == null) + throw new Exception("postDmaap: unexpected null response"); + logger.debug("postDmaap: resp is {}", hsr); + // Returns ? on success, error message string on error. + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postDmaap: null response body"); + return response; + } + try { + Dmaap resp = mapper.readValue(hsr.getResponseString(), Dmaap.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDmaap: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates DMaaP system wide configuration settings for the dcaeEnvironment. + * + * @param dmaap + * @return Status and response; expect 200 and a DR_Pub on success; a code + * and and ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putDmaap(Dmaap dmaap) throws Exception { + String jsonBody = mapper.writeValueAsString(dmaap); + // Oddly, this PUT has no ID parameter in the URL + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(DMAAP), jsonBody); + if (hsr == null) + throw new Exception("putDmaap: unexpected null response"); + logger.debug("putDmaap: resp is {}", hsr); + // Returns ? on success, error message string on error. + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putDmaap: null response body"); + return response; + } + try { + Dmaap resp = mapper.readValue(hsr.getResponseString(), Dmaap.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putDmaap: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + // NO DELETE_DMAAP METHOD + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of data router nodes. + * + * @return List of DmaapObject: list contains DR_Node object(s) on success; + * a single ErrorResponse object if the remote site rejects the + * request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getDRNodes() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_NODES)); + logger.debug("getDRNodes: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRNodes: unexpected null response"); + + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getDRNodes: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets a data router node with the specified ID. + * + * @param fqdn + * Name of the node to get + * @return DmaapObject: a DR_Node object on success; an ErrorResponse object + * if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getDRNode(final String fqdn) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_NODES, fqdn)); + logger.debug("getDRNode: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRNode: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), DR_Node.class); + } catch (Exception ex) { + logger.debug("getDRNode: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a data router node. + * + * @param drNode + * Node to be created + * @return Status and response: expect 200 and a DR_Node on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postDRNode(DR_Node drNode) throws Exception { + String jsonBody = mapper.writeValueAsString(drNode); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(DR_NODES), jsonBody); + logger.debug("postDRNode: resp is {}", hsr); + if (hsr == null) + throw new Exception("postDRNode: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postDRNode: null response body"); + return response; + } + try { + DR_Node resp = mapper.readValue(hsr.getResponseString(), DR_Node.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDRNode: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a data router node. + * + * @param drNode + * Node to be updated + * @return Status and response: expect 200 and a DR_Node on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putDRNode(DR_Node drNode) throws Exception { + String jsonBody = mapper.writeValueAsString(drNode); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(DR_NODES, drNode.getFqdn()), jsonBody); + logger.debug("putDRNode: resp is {}", hsr); + if (hsr == null) + throw new Exception("putDRNode: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putDRNode: null response body"); + return response; + } + try { + DR_Node resp = mapper.readValue(hsr.getResponseString(), DR_Node.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDRNode: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the data router node with the specified FQDN. + * + * @param fqdn + * Name of the node to delete + * @return Status and response: expect 204 and a null on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteDRNode(final String fqdn) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(DR_NODES, fqdn)); + logger.debug("deleteDRNode: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteDRNode: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteDRNode: null response body"); + return response; + } + try { + DR_Node resp = mapper.readValue(hsr.getResponseString(), DR_Node.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteDRNode: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of data router publishers. + * + * @return List of DmaapObject: list contains DR_Pub object(s) on success; a + * single ErrorResponse object if the remote site rejects the + * request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getDRPubs() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_PUBS)); + logger.debug("getDRPubs: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRPubs: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getDRPubs: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets a data router publisher with the specified ID. + * + * @param pubId + * ID of the publisher to get + * @return DmaapObject: a DR_Pub object on success; an ErrorResponse object + * if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getDRPub(final String pubId) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_PUBS, pubId)); + logger.debug("getDRPub: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRPub: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), DR_Pub.class); + } catch (Exception ex) { + logger.debug("getDRPub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a data router publisher. + * + * @param drPub + * @return Status and response: expect 200 and a DR_Pub on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postDRPub(DR_Pub drPub) throws Exception { + String jsonBody = mapper.writeValueAsString(drPub); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(DR_PUBS), jsonBody); + logger.debug("postDRPub: resp is {}", hsr); + if (hsr == null) + throw new Exception("postDRPub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postDRPub: null response body"); + return response; + } + try { + DR_Pub resp = mapper.readValue(hsr.getResponseString(), DR_Pub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDRPub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a data router publisher. + * + * @param drPub + * Publisher to be updated + * @return Status and response: expect 200 and a DR_Pub on success, a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putDRPub(DR_Pub drPub) throws Exception { + String jsonBody = mapper.writeValueAsString(drPub); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(DR_PUBS, drPub.getPubId()), jsonBody); + logger.debug("putDRPub: resp is {}", hsr); + if (hsr == null) + throw new Exception("putDRPub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putDRPub: null response body"); + return response; + } + try { + DR_Pub resp = mapper.readValue(hsr.getResponseString(), DR_Pub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDRPub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the data router publisher with the specified ID. + * + * @param pubId + * ID of the publisher to delete + * @return Status and response: expect 204 and a null on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteDRPub(final String pubId) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(DR_PUBS, pubId)); + logger.debug("deleteDRPub: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteDRPub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteDRPub: null response body"); + return response; + } + try { + DR_Pub resp = mapper.readValue(hsr.getResponseString(), DR_Pub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteDRPub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of data router subscribers. + * + * @return List of DmaapObject: list contains DR_Sub object(s) on success; a + * single ErrorResponse object if the remote site rejects the + * request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getDRSubs() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_SUBS)); + logger.debug("getDRSubs: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRSubs: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getDRSubs: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets a data router subscriber with the specified ID. + * + * @param subId + * ID of the subscriber to get + * @return DmaapObject: a DR_Sub object on success; an ErrorResponse object + * if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getDRSub(final String subId) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(DR_SUBS, subId)); + logger.debug("getDRPub: resp is {}", hsr); + if (hsr == null) + throw new Exception("getDRSub: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), DR_Sub.class); + } catch (Exception ex) { + logger.debug("getDRSub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a data router subscriber. + * + * @param drSub + * @return Status and response: expect 200 and a DR_Sub on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postDRSub(DR_Sub drSub) throws Exception { + String jsonBody = mapper.writeValueAsString(drSub); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(DR_SUBS), jsonBody); + logger.debug("postDRSub: resp is {}", hsr); + if (hsr == null) + throw new Exception("postDRSub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postDRSub: null response body"); + return response; + } + try { + DR_Sub resp = mapper.readValue(hsr.getResponseString(), DR_Sub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postDRSub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a data router subscriber. + * + * @param drSub + * Subscriber to be updated + * @return Status and response; expect 200 and a DR_Sub on success, a string + * error on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putDRSub(DR_Sub drSub) throws Exception { + String jsonBody = mapper.writeValueAsString(drSub); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(DR_SUBS, drSub.getSubId()), jsonBody); + logger.debug("putDRSub: resp is {}", hsr); + if (hsr == null) + throw new Exception("putDRSub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putDRSub: null response body"); + return response; + } + try { + DR_Sub resp = mapper.readValue(hsr.getResponseString(), DR_Sub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putDRSub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the data router subscriber with the specified ID. + * + * @param subId + * ID of the subscriber to delete + * @return Status and response: expect 204 and a null on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteDRSub(final String subId) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(DR_SUBS, subId)); + logger.debug("deleteDRSub: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteDRSub: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteDRSub: null response body"); + return response; + } + try { + DR_Sub resp = mapper.readValue(hsr.getResponseString(), DR_Sub.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteDRSub: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of data router feeds. + * + * @return List of DmaapObject: list contains DcaeLocation object(s) on + * success; a single ErrorResponse object if the remote site rejects + * the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getFeeds() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(FEEDS)); + logger.debug("getFeeds: resp is {}", hsr); + if (hsr == null) + throw new Exception("getFeeds: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getFeeds: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets a data router feed with the specified ID. + * + * @param feedId + * ID of the feed to get + * @return DmaapObject: a Feed object on success; an ErrorResponse object if + * the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getFeed(final String feedId) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(FEEDS, feedId)); + logger.debug("getFeed: resp is {}", hsr); + if (hsr == null) + throw new Exception("getFeed: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), Feed.class); + } catch (Exception ex) { + logger.debug("getFeed: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a feed and adds any specified pubs and subs. + * + * @param feed + * @return Status and response: expect 200 and a Feed on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postFeed(Feed feed) throws Exception { + String jsonBody = mapper.writeValueAsString(feed); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(FEEDS), jsonBody); + logger.debug("postFeed: resp is {}", hsr); + if (hsr == null) + throw new Exception("postFeed: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postFeed: null response body"); + return response; + } + try { + Feed resp = mapper.readValue(hsr.getResponseString(), Feed.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postFeed: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a data router feed. + * + * @param feed + * Feed to be updated + * @return Status and response: expect 200 and a Feed on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putFeed(Feed feed) throws Exception { + String jsonBody = mapper.writeValueAsString(feed); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(FEEDS, feed.getFeedId()), jsonBody); + logger.debug("putFeed: resp is {}", hsr); + if (hsr == null) + throw new Exception("putFeed: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putFeed: null response body"); + return response; + } + try { + Feed resp = mapper.readValue(hsr.getResponseString(), Feed.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putFeed: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the data router feed with the specified ID. + * + * @param feedId + * ID of the feed to delete + * @return Status and response: expect 204 and a null on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteFeed(final String feedId) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(FEEDS, feedId)); + logger.debug("deleteFeed: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteFeed: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteFeed: null response body"); + return response; + } + try { + Feed resp = mapper.readValue(hsr.getResponseString(), Feed.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteFeed: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of message router topics. + * + * @return List of DmaapObject: list contains Topic object(s) on success; a + * single ErrorResponse object if the remote site rejects the + * request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getTopics() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(TOPICS)); + logger.debug("getTopics: resp is {}", hsr); + if (hsr == null) + throw new Exception("getTopics: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getTopics: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets the message router topic with the specified FQTN. + * + * @param fqtn + * Fully qualified topic name + * @return DmaapObject: a Topic object on success; an ErrorResponse object + * if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getTopic(final String fqtn) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(TOPICS, fqtn)); + logger.debug("getTopic: resp is {}", hsr); + if (hsr == null) + throw new Exception("getTopic: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), Topic.class); + } catch (Exception ex) { + logger.debug("getTopic: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a topic and grants appropriate permissions to specified pubs and + * subs. + * + * @param topic + * @return Status and response: expect 200 and a Topic on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postTopic(Topic topic) throws Exception { + String jsonBody = mapper.writeValueAsString(topic); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(TOPICS), jsonBody); + logger.debug("postTopic: resp is {}", hsr); + if (hsr == null) + throw new Exception("postTopic: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postTopic: null response body"); + return response; + } + try { + Topic resp = mapper.readValue(hsr.getResponseString(), Topic.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postTopic: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the message router topic with the specified FQTN. + * + * @param fqtn + * Fully qualified topic name to delete + * @return Status and response: expect 204 and a null on success; a code and + * an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteTopic(final String fqtn) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(TOPICS, fqtn)); + logger.debug("deleteTopic: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteTopic: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteTopic: null response body"); + return response; + } + try { + Topic resp = mapper.readValue(hsr.getResponseString(), Topic.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteTopic: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of message router clients. + * + * @return List of DmaapObject: list contains MR_Client object(s) on + * success; a single ErrorResponse object if the remote site rejects + * the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getMRClients() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(MR_CLIENTS)); + logger.debug("getMRClients: resp is {}", hsr); + if (hsr == null) + throw new Exception("getMRClients: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getMRClients: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets the message router client with the specified ID. + * + * @param mrClientId + * ID of the client to get + * @return DmaapObject: a MR_Client object on success; an ErrorResponse + * object if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getMRClient(final String mrClientId) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(MR_CLIENTS, mrClientId)); + logger.debug("getMRClient: resp is {}", hsr); + if (hsr == null) + throw new Exception("getMRClient: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), MR_Client.class); + } catch (Exception ex) { + logger.debug("getMRClient: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a message router client. + * + * @param mrClient + * @return Status and response: expect 200 and a MR_Client on success; a + * code and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postMRClient(MR_Client mrClient) throws Exception { + String jsonBody = mapper.writeValueAsString(mrClient); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(MR_CLIENTS), jsonBody); + logger.debug("postMRClient: resp is {}", hsr); + if (hsr == null) + throw new Exception("postMRClient: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postMRClient: null response body"); + return response; + } + try { + MR_Client resp = mapper.readValue(hsr.getResponseString(), MR_Client.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postMRClient: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a message router client. + * + * @param mrClient + * client to be updated + * @return Status and response; expect 200 and a MR_Client on success, a + * string error on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putMRClient(MR_Client mrClient) throws Exception { + String jsonBody = mapper.writeValueAsString(mrClient); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(MR_CLIENTS, mrClient.getMrClientId()), jsonBody); + logger.debug("putMRClient: resp is {}", hsr); + if (hsr == null) + throw new Exception("putMRClient: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putMRClient: null response body"); + return response; + } + try { + MR_Client resp = mapper.readValue(hsr.getResponseString(), MR_Client.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putMRClient: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the message router client with the specified ID. + * + * @param mrClientId + * ID of the client to delete + * @return Status and response; expect 204 and a null on success, a string + * error on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteMRClient(final String mrClientId) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(MR_CLIENTS, mrClientId)); + logger.debug("deleteMRClient: resp is {}", hsr); + if (hsr == null) + throw new Exception("deleteMRClient: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteMRClient: null response body"); + return response; + } + try { + MR_Client resp = mapper.readValue(hsr.getResponseString(), MR_Client.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteMRClient: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Gets a list of message router clusters. + * + * @return List of DmaapObject: list contains MR_Cluster object(s) on + * success; a single ErrorResponse object if the remote site rejects + * the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public List getMRClusters() throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(MR_CLUSTERS)); + logger.debug("getMRClusters: resp is {}", hsr); + if (hsr == null) + throw new Exception("getMRClusters: unexpected null response"); + List responseList = null; + try { + TypeReference> typeRef = new TypeReference>() { + }; + responseList = mapper.readValue(hsr.getResponseString(), typeRef); + } catch (Exception ex) { + logger.debug("getDcaeLocations: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + responseList = new ArrayList(); + responseList.add(errResp); + } + return responseList; + } + + /** + * Gets the message router cluster with the specified location name. + * + * @param dcaeLocName + * name of the cluster to get + * @return DmaapObject: a MR_Cluster object on success; an ErrorResponse + * object if the remote site rejects the request. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public DmaapObject getMRCluster(final String dcaeLocName) throws Exception { + HttpStatusAndResponse hsr = getRestContent(buildDmaapUri(MR_CLUSTERS, dcaeLocName)); + logger.debug("getMRCluster: resp is {}", hsr); + if (hsr == null) + throw new Exception("getMRCluster: unexpected null response"); + DmaapObject response = null; + try { + response = mapper.readValue(hsr.getResponseString(), MR_Cluster.class); + } catch (Exception ex) { + logger.debug("getMRCluster: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + response = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + } + return response; + } + + /** + * Creates a message router cluster. + * + * @param mrCluster + * @return Status and response: expect 200 and a MR_Cluster on success; a + * code and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse postMRCluster(MR_Cluster mrCluster) throws Exception { + String jsonBody = mapper.writeValueAsString(mrCluster); + HttpStatusAndResponse hsr = postRestContent(buildDmaapUri(MR_CLUSTERS), jsonBody); + logger.debug("postMRCluster: resp is {}", hsr); + if (hsr == null) + throw new Exception("postMRCluster: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("postMRCluster: null response body"); + return response; + } + try { + MR_Cluster resp = mapper.readValue(hsr.getResponseString(), MR_Cluster.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("postMRCluster: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Updates a message router cluster. + * + * @param mrCluster + * cluster to be updated + * @return Status and response; expect 200 and a MR_Cluster on success, a + * string error on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse putMRCluster(MR_Cluster mrCluster) throws Exception { + String jsonBody = mapper.writeValueAsString(mrCluster); + HttpStatusAndResponse hsr = putRestContent(buildDmaapUri(MR_CLUSTERS, mrCluster.getDcaeLocationName()), jsonBody); + logger.debug("putMRCluster: resp is {}", hsr); + if (hsr == null) + throw new Exception("putMRCluster: unexpected null response"); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("putMRCluster: null response body"); + return response; + } + try { + MR_Cluster resp = mapper.readValue(hsr.getResponseString(), MR_Cluster.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("putMRCluster: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + /** + * Deletes the message router cluster with the specified location name. + * + * @param dcaeLocName + * Location name of the cluster to delete + * @return Status and response: expect 204 and a null on success; a code + * and an ErrorResponse on failure. + * @throws Exception + * if host cannot be reached, response cannot be parsed, etc. + */ + public HttpStatusAndResponse deleteMRCluster(final String dcaeLocName) throws Exception { + HttpStatusAndResponse hsr = deleteRestContent(buildDmaapUri(MR_CLUSTERS, dcaeLocName)); + if (hsr == null) + throw new Exception("deleteMRCluster: unexpected null response"); + logger.debug("deleteMRCluster: resp is {}", hsr); + HttpStatusAndResponse response = new HttpStatusAndResponse(hsr.getStatusCode(), null); + if (hsr.getResponse() == null) { + logger.debug("deleteMRCluster: null response body"); + return response; + } + try { + MR_Cluster resp = mapper.readValue(hsr.getResponseString(), MR_Cluster.class); + response.setResponse(resp); + } catch (Exception ex) { + logger.debug("deleteMRCluster: trying to parse response as error: {}", ex.toString()); + // If this parse fails, let the exception be thrown + ErrorResponse errResp = mapper.readValue(hsr.getResponseString(), ErrorResponse.class); + response.setResponse(errResp); + } + return response; + } + + ///////////////////////////////////////////////////////////////////// + + /** + * Builds the URI for the DMaaP REST endpoint using configuration and the + * specified task and path parameter(s). Deals with extra or missing slashes + * to allow for some flexibility in the config file. + * + * @param requestPath + * Last part of endpoint path + * @param pathParam + * Additional path parameters in order; ignored if null or empty + * @return REST endpoint URI + * @throws Exception + * if the RESAT URL property is not found + */ + private URI buildDmaapUri(String task, String... pathParam) throws Exception { + if (dmaapRestUrl == null || dmaapRestUrl.length() == 0) + throw new Exception("buildUrlPath: unconfigured, must set dmaapEndpointUrl"); + StringBuilder sb = new StringBuilder(); + // Clean the base of any trailing slashes + sb.append(trimSlashes(dmaapRestUrl)); + sb.append('/'); + // task is controlled in this file, don't clean it. + sb.append(task); + if (pathParam != null) { + for (String pp : pathParam) { + sb.append('/'); + // path comes from the user, definitely clean it. + sb.append(trimSlashes(pp)); + } + } + String urlPath = sb.toString(); + URIBuilder uriBuilder = new URIBuilder(urlPath); + return uriBuilder.build(); + } + + /** + * Strips the specified string of leading and trailing forward-slash + * characters. + * + * @param s + * String to trim + * @return String without any leading or trailing '/' characters. + */ + private String trimSlashes(String s) { + while (s.length() > 0 && s.charAt(0) == '/') + s = s.substring(1, s.length()); + while (s.length() > 0 && s.charAt(s.length() - 1) == '/') + s = s.substring(0, s.length() - 1); + return s; + } + +} diff --git a/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/HttpStatusAndResponse.java b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/HttpStatusAndResponse.java new file mode 100644 index 0000000..3b8e97e --- /dev/null +++ b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/HttpStatusAndResponse.java @@ -0,0 +1,66 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller REST Client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dcae.dmaap.bcapi.client; + +/** + * Holds the status code and body that result from accessing an HTTP URL. + */ +public class HttpStatusAndResponse { + + private int statusCode; + private ResponseType response; + + public HttpStatusAndResponse(int status, ResponseType resp) { + this.statusCode = status; + this.response = resp; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(final int code) { + this.statusCode = code; + } + + public ResponseType getResponse() { + return response; + } + + public void setResponse(ResponseType response) { + this.response = response; + } + + /** + * Convenience method to avoid testing for null and calling .toString() + * + * @return String version of the response object; null if the object is + * null. + */ + public String getResponseString() { + return response == null ? null : response.toString(); + } + + @Override + public String toString() { + return "HttpStatusAndResponse[" + Integer.toString(statusCode) + ";" + + (response == null ? "" : response.toString()) + "]"; + } +} diff --git a/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/IRestClientConstants.java b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/IRestClientConstants.java new file mode 100644 index 0000000..10b8189 --- /dev/null +++ b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/IRestClientConstants.java @@ -0,0 +1,35 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller REST Client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dcae.dmaap.bcapi.client; + +public interface IRestClientConstants { + + /** + * Default configuration file to be found on classpath + */ + public static final String PROPERTY_FILE_NAME = "bc-rest-client.properties"; + + /** Base URL of the Bus Controller REST service API; e.g., + * + */ + public static final String DMAAP_BUS_CONTROLLER_REST_URL = "dmaap_bus_controller_rest_url"; + + +} diff --git a/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/SimpleRestClientBase.java b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/SimpleRestClientBase.java new file mode 100644 index 0000000..1e5b064 --- /dev/null +++ b/dcae_dmaapbc_client/src/main/java/org/openecomp/dcae/dmaap/bcapi/client/SimpleRestClientBase.java @@ -0,0 +1,273 @@ +/*- + * ================================================================================ + * DCAE DMaaP Bus Controller REST Client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * 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. + * ================================================================================ + */ +package org.openecomp.dcae.dmaap.bcapi.client; + +import java.io.IOException; +import java.net.URI; + +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthCache; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.config.RequestConfig.Builder; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides a basic client to access a REST endpoint, optionally using HTTP + * basic authentication. + * + * Caveat: If HTTPS access is used and the server uses a self-signed + * certificate, the local trust store must be extended appropriately. The client + * throws exceptions if the JVM cannot validate the server certificate. + */ +public class SimpleRestClientBase { + + private static Logger logger = LoggerFactory.getLogger(SimpleRestClientBase.class); + + /** + * Credentials for HTTP basic authentication (optional). + */ + private final String username; + private final String password; + + /** + * Timeouts (optional) + */ + private Integer connectTimeoutMs = null; + private Integer connectionRequestTimeoutMs = null; + private Integer socketTimeoutMs = null; + + /** + * Constructs a client that does not use any authentication and uses Apache HTTPD client default values for timeouts. + */ + public SimpleRestClientBase() { + this(null, null, null, null, null); + } + + /** + * Convenience constructor to build a client that uses the specified username and password for + * basic HTTP authentication on all requests. In other words, this client + * pre-emptively sends the "Basic" header instead of first trying the + * request without, failing, negotiating, then sending with credentials. + * + * @param username User name for basic HTTP authentication. + * @param password Password for basic HTTP authentication. + */ + public SimpleRestClientBase(final String username, final String password) { + this(username, password, null, null, null); + } + + /** + * Convenience constructor to build a client that uses the specified timeouts + * on all requests. + * + * @param connectTimeoutMs + * Connection timeout, in milliseconds + * @param connectionRequestTimeoutMs + * Connection request timeout, in milliseconds + * @param socketTimeoutMs + * Socket timeout, in milliseconds + */ + public SimpleRestClientBase(final Integer connectTimeoutMs, final Integer connectionRequestTimeoutMs, final Integer socketTimeoutMs) { + this(null, null, connectTimeoutMs, connectionRequestTimeoutMs, socketTimeoutMs); + } + + /** + * Constructs a client with the specified credentials and timeout values. + * @param username User name for basic HTTP authentication; ignored if null + * @param password Password for basic HTTP authentication; ignored if null + * @param connectTimeoutMs ignored if null + * @param connectionRequestTimeoutMs ignored if null + * @param socketTimeoutMs ignored if null + */ + public SimpleRestClientBase(final String username, final String password, final Integer connectTimeoutMs, final Integer connectionRequestTimeoutMs, final Integer socketTimeoutMs) { + this.username = username; + this.password = password; + this.connectTimeoutMs = null; + this.connectionRequestTimeoutMs = null; + this.socketTimeoutMs = null; + } + + + /** + * Constructs and sends a GET request for the URI. + * + * @param uri + * REST endpoint + * @return Result of the get + * @throws Exception + */ + public HttpStatusAndResponse getRestContent(final URI uri) throws Exception { + HttpGet httpGet = new HttpGet(uri); + return doRestRequest(httpGet); + } + + /** + * Constructs and sends a POST request using the specified body. + * + * @param uri + * REST endpoint + * @param json + * Content to post + * @return Result of the post; null if an error happens + * @throws Exception + */ + public HttpStatusAndResponse postRestContent(final URI uri, final String json) throws Exception { + HttpPost httpPost = new HttpPost(uri); + StringEntity postEntity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8)); + httpPost.setEntity(postEntity); + return doRestRequest(httpPost); + } + + /** + * Constructs and sends a PUT request using the specified body. + * + * @param uri + * REST endpoint + * @param json + * Content to put + * @return Result of the put; null if an error happens + * @throws Exception + */ + public HttpStatusAndResponse putRestContent(final URI uri, final String json) throws Exception { + HttpPut httpPut = new HttpPut(uri); + StringEntity postEntity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8)); + httpPut.setEntity(postEntity); + return doRestRequest(httpPut); + } + + /** + * Constructs and sends a DELETE request for the URI. + * + * @param uri + * REST endpoint + * @return Result of the get + * @throws Exception + */ + public HttpStatusAndResponse deleteRestContent(final URI uri) throws Exception { + HttpDelete httpDel = new HttpDelete(uri); + return doRestRequest(httpDel); + } + + /** + * Executes the specified request and gathers the response. + * + * @param request + * HttpGet, HttpPost, etc. + * @return Status code and response body + * @throws ClientProtocolException + * @throws IOException + */ + private HttpStatusAndResponse doRestRequest(final HttpRequestBase request) + throws ClientProtocolException, IOException { + + // Set up authentication if needed + final HttpClientContext context = HttpClientContext.create(); + if (this.username != null || this.password != null) { + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, credentials); + context.setCredentialsProvider(credentialsProvider); + + HttpHost host = new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()); + AuthCache authCache = new BasicAuthCache(); + authCache.put(host, new BasicScheme()); + context.setAuthCache(authCache); + } + final Builder requestConfigBuilder = RequestConfig.custom(); + if (connectionRequestTimeoutMs != null) + requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeoutMs); + if (connectTimeoutMs != null) + requestConfigBuilder.setConnectTimeout(connectTimeoutMs); + if (socketTimeoutMs != null) + requestConfigBuilder.setSocketTimeout(socketTimeoutMs); + RequestConfig requestConfig = requestConfigBuilder.build(); + final CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); + CloseableHttpResponse response = null; + String responseJson = null; + try { + response = httpClient.execute(request, context); + // Some methods return non-200 on success + logger.debug("doRestRequest: status is {}", response.getStatusLine()); + HttpEntity entity = response.getEntity(); + // This is common; don't warn + if (entity == null) { + logger.debug("doRestRequest: Entity is null"); + } else { + // entity content length is never set; + // this naively tries to read everything. + responseJson = EntityUtils.toString(entity); + EntityUtils.consume(entity); + // Don't give back empty string; + // it has no more meaning than null. + if (responseJson.length() == 0) + responseJson = null; + } + } finally { + if (response != null) + response.close(); + } + if (response == null) + return null; + return new HttpStatusAndResponse(response.getStatusLine().getStatusCode(), responseJson); + } + + /** + * Basic test invocation. + * + * @param args + * Expect 1 argument, the URL of a REST endpoint. + * @throws Exception + * if anything goes wrong + */ + public static void main(String[] args) throws Exception { + if (args.length != 1) + throw new IllegalArgumentException("Expect 1 argument: REST URL for GET"); + SimpleRestClientBase client = new SimpleRestClientBase(); + URIBuilder uriBuilder = new URIBuilder(args[0]); + URI uri = uriBuilder.build(); + HttpStatusAndResponse hsr = client.getRestContent(uri); + System.out.println("Response code is " + hsr.getStatusCode()); + System.out.println(hsr.getResponseString()); + System.out.println("main ends."); + } +} -- cgit 1.2.3-korg