From 8506f0b261083e945488e869803bcd5ab8b7ea84 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Wed, 13 Mar 2019 21:02:14 +0000 Subject: add message router publisher initial api and implementation of message router publisher Change-Id: Ic69d013bae8ab8c842e6ecd2eef41e10649d1009 Issue-ID: CCSDK-1163 Signed-off-by: Smokowski, Kevin (ks6305) --- message-router/README.md | 7 + message-router/pom.xml | 32 +++++ message-router/publisher/README.md | 8 ++ message-router/publisher/api/pom.xml | 27 ++++ .../messagerouter/publisher/api/PublisherApi.java | 5 + message-router/publisher/features/pom.xml | 58 ++++++++ .../features/src/main/feature/feature.xml | 16 +++ message-router/publisher/installer/pom.xml | 140 +++++++++++++++++++ .../src/assembly/assemble_installer_zip.xml | 59 ++++++++ .../src/assembly/assemble_mvnrepo_zip.xml | 49 +++++++ .../src/main/resources/scripts/install-feature.sh | 39 ++++++ message-router/publisher/pom.xml | 21 +++ message-router/publisher/provider/pom.xml | 39 ++++++ .../publisher/provider/impl/PublisherApiImpl.java | 149 +++++++++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 14 ++ message-router/publisher/sample.client/pom.xml | 39 ++++++ .../publisher/client/impl/ClientImpl.java | 33 +++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 16 +++ 18 files changed, 751 insertions(+) create mode 100755 message-router/README.md create mode 100755 message-router/pom.xml create mode 100755 message-router/publisher/README.md create mode 100755 message-router/publisher/api/pom.xml create mode 100755 message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java create mode 100755 message-router/publisher/features/pom.xml create mode 100755 message-router/publisher/features/src/main/feature/feature.xml create mode 100644 message-router/publisher/installer/pom.xml create mode 100644 message-router/publisher/installer/src/assembly/assemble_installer_zip.xml create mode 100644 message-router/publisher/installer/src/assembly/assemble_mvnrepo_zip.xml create mode 100644 message-router/publisher/installer/src/main/resources/scripts/install-feature.sh create mode 100755 message-router/publisher/pom.xml create mode 100755 message-router/publisher/provider/pom.xml create mode 100755 message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java create mode 100755 message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml create mode 100755 message-router/publisher/sample.client/pom.xml create mode 100755 message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java create mode 100755 message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml (limited to 'message-router') diff --git a/message-router/README.md b/message-router/README.md new file mode 100755 index 00000000..32848252 --- /dev/null +++ b/message-router/README.md @@ -0,0 +1,7 @@ +# CCSDK Message Router Extension + +This maven module builds re-usable code that interacts with message router + +## High level modules +- **publisher** used for sending messages to a dmaap message router topic +- **consumer** not yet created, this will be used for pulling messages from a dmaap message router topic diff --git a/message-router/pom.xml b/message-router/pom.xml new file mode 100755 index 00000000..0d1328d3 --- /dev/null +++ b/message-router/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + binding-parent + 1.2.1-SNAPSHOT + + + + org.onap.ccsdk.messagerouter + parent + 0.4.1-SNAPSHOT + pom + + + publisher + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + + + diff --git a/message-router/publisher/README.md b/message-router/publisher/README.md new file mode 100755 index 00000000..58c1bf47 --- /dev/null +++ b/message-router/publisher/README.md @@ -0,0 +1,8 @@ +# Publisher + +## Modules +- api - exports the publisher interface for clients and providers to import +- features - used for managing the feature repository for publisher +- installer - provides a simple install script +- provider - provides an implementation of the publisher api, this implementation assumes the controller has a single identity for publishing to DMAAP message router +- sample.client - a dummy client that posts a simple message to a configured topic during its initialization diff --git a/message-router/publisher/api/pom.xml b/message-router/publisher/api/pom.xml new file mode 100755 index 00000000..ef03f7fd --- /dev/null +++ b/message-router/publisher/api/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + publisher.aggregate + 0.4.1-SNAPSHOT + + + publisher.api + bundle + + + + + org.apache.felix + maven-bundle-plugin + + + org.onap.ccsdk.messagerouter.publisher.api + + + + + + diff --git a/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java b/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java new file mode 100755 index 00000000..6d4bddcd --- /dev/null +++ b/message-router/publisher/api/src/main/java/org/onap/ccsdk/messagerouter/publisher/api/PublisherApi.java @@ -0,0 +1,5 @@ +package org.onap.ccsdk.messagerouter.publisher.api; + +public interface PublisherApi { + public Boolean publish(String topic, String body); +} diff --git a/message-router/publisher/features/pom.xml b/message-router/publisher/features/pom.xml new file mode 100755 index 00000000..33a19b8c --- /dev/null +++ b/message-router/publisher/features/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + publisher.aggregate + 0.4.1-SNAPSHOT + + + publisher.features + pom + + + + + src/main/feature + true + ${project.build.directory}/feature + + + + + org.apache.maven.plugins + maven-resources-plugin + + + + resources + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + attach-artifacts + package + + attach-artifact + + + + + target/feature/feature.xml + xml + features + + + + + + + + + diff --git a/message-router/publisher/features/src/main/feature/feature.xml b/message-router/publisher/features/src/main/feature/feature.xml new file mode 100755 index 00000000..f5e9bb6e --- /dev/null +++ b/message-router/publisher/features/src/main/feature/feature.xml @@ -0,0 +1,16 @@ + + + + + + mvn:org.onap.ccsdk.messagerouter/publisher.api/${project.version} + mvn:org.onap.ccsdk.messagerouter/publisher.provider/${project.version} + + + + mvn:org.onap.ccsdk.messagerouter/sample.client/${project.version} + + + diff --git a/message-router/publisher/installer/pom.xml b/message-router/publisher/installer/pom.xml new file mode 100644 index 00000000..49113ae1 --- /dev/null +++ b/message-router/publisher/installer/pom.xml @@ -0,0 +1,140 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + publisher.aggregate + 0.4.1-SNAPSHOT + + + org.onap.ccsdk.messagerouter + publisher.installer + 0.4.1-SNAPSHOT + pom + + + messagerouter-publisher + messagerouter-publisher + mvn:${project.groupId}/publisher.features/${project.version}/xml/features + false + + + + + org.onap.ccsdk.messagerouter + publisher.features + ${project.version} + xml + features + + + org.onap.ccsdk.messagerouter + publisher.api + ${project.version} + + + org.onap.ccsdk.messagerouter + publisher.provider + ${project.version} + + + org.onap.ccsdk.messagerouter + sample.client + ${project.version} + + + + + + + maven-assembly-plugin + 2.6 + + + maven-repo-zip + + single + + package + + true + stage/${application.name}-${project.version} + + src/assembly/assemble_mvnrepo_zip.xml + + true + + + + installer-zip + + single + + package + + true + ${application.name}-${project.version}-installer + + src/assembly/assemble_installer_zip.xml + + false + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + + copy-dependencies + + prepare-package + + false + ${project.build.directory}/assembly/system + false + true + true + true + false + false + org.onap.ccsdk.messagerouter + provided + + + + + + maven-resources-plugin + 2.6 + + + copy-version + + copy-resources + + validate + + ${basedir}/target/stage + + + src/main/resources/scripts + + install-feature.sh + + true + + + + + + + + + + + \ No newline at end of file diff --git a/message-router/publisher/installer/src/assembly/assemble_installer_zip.xml b/message-router/publisher/installer/src/assembly/assemble_installer_zip.xml new file mode 100644 index 00000000..c6169a87 --- /dev/null +++ b/message-router/publisher/installer/src/assembly/assemble_installer_zip.xml @@ -0,0 +1,59 @@ + + + + + + installer_zip + + zip + + + + false + + + + target/stage/ + ${application.name} + 755 + + *.sh + + + + target/stage/ + ${application.name} + 644 + + *.sh + + + + + + + \ No newline at end of file diff --git a/message-router/publisher/installer/src/assembly/assemble_mvnrepo_zip.xml b/message-router/publisher/installer/src/assembly/assemble_mvnrepo_zip.xml new file mode 100644 index 00000000..377b5b15 --- /dev/null +++ b/message-router/publisher/installer/src/assembly/assemble_mvnrepo_zip.xml @@ -0,0 +1,49 @@ + + + + + + repo + + zip + + + + false + + + + target/assembly/ + . + + + + + + + + \ No newline at end of file diff --git a/message-router/publisher/installer/src/main/resources/scripts/install-feature.sh b/message-router/publisher/installer/src/main/resources/scripts/install-feature.sh new file mode 100644 index 00000000..15dc0c27 --- /dev/null +++ b/message-router/publisher/installer/src/main/resources/scripts/install-feature.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# openECOMP : SDN-C +# ================================================================================ +# Copyright (C) 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========================================================= +### + +ODL_HOME=${ODL_HOME:-/opt/opendaylight/current} +ODL_KARAF_CLIENT=${ODL_KARAF_CLIENT:-${ODL_HOME}/bin/client} +INSTALLERDIR=$(dirname $0) + +REPOZIP=${INSTALLERDIR}/${features.boot}-${project.version}.zip + +if [ -f ${REPOZIP} ] +then + unzip -d ${ODL_HOME} ${REPOZIP} +else + echo "ERROR : repo zip ($REPOZIP) not found" + exit 1 +fi + +${ODL_KARAF_CLIENT} feature:repo-add ${features.repositories} +${ODL_KARAF_CLIENT} feature:install ${features.boot} \ No newline at end of file diff --git a/message-router/publisher/pom.xml b/message-router/publisher/pom.xml new file mode 100755 index 00000000..eded68fd --- /dev/null +++ b/message-router/publisher/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + parent + 0.4.1-SNAPSHOT + + + publisher.aggregate + pom + + + api + features + provider + sample.client + installer + + diff --git a/message-router/publisher/provider/pom.xml b/message-router/publisher/provider/pom.xml new file mode 100755 index 00000000..ca51aea4 --- /dev/null +++ b/message-router/publisher/provider/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + publisher.aggregate + 0.4.1-SNAPSHOT + + + publisher.provider + bundle + + + + org.onap.ccsdk.messagerouter + publisher.api + ${project.version} + + + org.slf4j + slf4j-api + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.onap.ccsdk.messagerouter.publisher.provider.impl + + + + + + diff --git a/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java new file mode 100755 index 00000000..3e8ab333 --- /dev/null +++ b/message-router/publisher/provider/src/main/java/org/onap/ccsdk/messagerouter/publisher/provider/impl/PublisherApiImpl.java @@ -0,0 +1,149 @@ +package org.onap.ccsdk.messagerouter.publisher.provider.impl; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.SocketException; +import java.net.URL; +import java.util.Base64; + +import org.onap.ccsdk.messagerouter.publisher.api.PublisherApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PublisherApiImpl implements PublisherApi { + private static final Logger logger = LoggerFactory.getLogger(PublisherApiImpl.class); + protected final Integer DEFAULT_CONNECT_TIMEOUT = 30000; // will be treated as 30 seconds + protected final Integer DEFAULT_READ_TIMEOUT = 180000; // will be treated as 3 minutes + private String authorizationString; + protected Integer connectTimeout; + protected Integer readTimeout; + protected String baseUrl; + protected String username; + protected String[] hosts; + private String password; + + public void setUsername(String username) { + this.username = username; + buildAuthorizationString(); + } + + public void setPassword(String password) { + this.password = password; + buildAuthorizationString(); + } + + public void setHost(String hostString) { + // a comma separated list of hosts can be passed in or a single host may be used + if (!hostString.contains(",")) { + this.hosts = new String[] { hostString }; + } else { + this.hosts = hostString.split(","); + } + } + + public PublisherApiImpl() { + connectTimeout = DEFAULT_CONNECT_TIMEOUT; + readTimeout = DEFAULT_READ_TIMEOUT; + } + + public void init() { + buildAuthorizationString(); + } + + private String buildUrlString(Integer hostIndex, String topic) { + return hosts[hostIndex] + "/events/" + topic; + } + + protected void configureHttpURLConnection(HttpURLConnection httpUrlConnection) { + httpUrlConnection.setRequestProperty("Content-Type", "application/json"); + } + + public Boolean publish(String topic, String body) { + for (int hostIndex = 0; hostIndex < hosts.length; hostIndex++) { + HttpURLConnection httpUrlConnection = null; + URL url = null; + try { + url = new URL(buildUrlString(hostIndex, topic)); + logger.info("Publishing body to topic {} using the url {}", topic, url); + logger.info("Message to publish is\n{}", body); + httpUrlConnection = buildHttpURLConnection(url); + httpUrlConnection.setDoInput(true); + httpUrlConnection.setDoOutput(true); + httpUrlConnection.setUseCaches(false); + httpUrlConnection.setRequestMethod("POST"); + + // Write message + httpUrlConnection.setRequestProperty("Content-Length", Integer.toString(body.length())); + DataOutputStream outStr = new DataOutputStream(httpUrlConnection.getOutputStream()); + outStr.write(body.getBytes()); + outStr.close(); + + int status = httpUrlConnection.getResponseCode(); + logger.info("Publishing body for topic {} using url {} returned status {}.", topic, url, status); + if (status < 300) { + String responseFromDMaaP = readFromStream(httpUrlConnection.getInputStream()); + logger.info("Message router response is\n{}", responseFromDMaaP); + return true; + } else { + if (httpUrlConnection.getErrorStream() != null) { + String responseFromDMaaP = readFromStream(httpUrlConnection.getErrorStream()); + logger.warn("Publishing body for topic {} using url {} failed." + " Error message is\n{}", + topic, url, responseFromDMaaP); + } + return false; + } + + } catch (SocketException socketException) { + logger.error("SocketException was thrown during publishing message to DMaaP on url {}.", url, + socketException); + if (hostIndex < hosts.length) { + logger.info("Message sent to {} failed with a SocketException, but will be tried on {}", + hosts[hostIndex], hosts[hostIndex + 1]); + } + } catch (Exception e) { + logger.warn("Exception was thrown during publishing message to DMaaP on url {}.", url, e); + return false; + } finally { + if (httpUrlConnection != null) { + httpUrlConnection.disconnect(); + } + } + } + return false; + } + + private void buildAuthorizationString() { + String basicAuthString = username + ":" + password; + basicAuthString = Base64.getEncoder().encodeToString(basicAuthString.getBytes()); + this.authorizationString = "Basic " + basicAuthString; + } + + protected HttpURLConnection buildHttpURLConnection(URL url) throws IOException { + HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); + if (authorizationString != null) { + httpUrlConnection.setRequestProperty("Authorization", authorizationString); + } + httpUrlConnection.setRequestProperty("Accept", "application/json"); + httpUrlConnection.setUseCaches(false); + httpUrlConnection.setConnectTimeout(connectTimeout); + httpUrlConnection.setReadTimeout(readTimeout); + configureHttpURLConnection(httpUrlConnection); + return httpUrlConnection; + } + + protected String readFromStream(InputStream stream) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(stream)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + br.close(); + return sb.toString(); + } + +} \ No newline at end of file diff --git a/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100755 index 00000000..da25fd23 --- /dev/null +++ b/message-router/publisher/provider/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/message-router/publisher/sample.client/pom.xml b/message-router/publisher/sample.client/pom.xml new file mode 100755 index 00000000..a264bff1 --- /dev/null +++ b/message-router/publisher/sample.client/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + org.onap.ccsdk.messagerouter + publisher.aggregate + 0.4.1-SNAPSHOT + + + sample.client + bundle + + + + org.onap.ccsdk.messagerouter + publisher.api + ${project.version} + + + org.slf4j + slf4j-api + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.onap.ccsdk.messagerouter.publisher.client.impl + + + + + + diff --git a/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java new file mode 100755 index 00000000..dbf49fe9 --- /dev/null +++ b/message-router/publisher/sample.client/src/main/java/org/onap/ccsdk/messagerouter/publisher/client/impl/ClientImpl.java @@ -0,0 +1,33 @@ +package org.onap.ccsdk.messagerouter.publisher.client.impl; + +import org.onap.ccsdk.messagerouter.publisher.api.PublisherApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientImpl { + private static final Logger logger = LoggerFactory.getLogger(ClientImpl.class); + private String topic; + private PublisherApi publisher; + + public void setPublisher(PublisherApi publisherApi) { + this.publisher = publisherApi; + } + + public void setTopic(String topic) { + this.topic = topic; + } + + public ClientImpl() { + + } + + public void init() { + for (int i = 0; i < 5; i++) { + String body = "{\"hello\":\"world " + String.valueOf(Math.random()) + "\"}"; + logger.error("Loop iteration " + i + " sending body " + body + " to the topic " + topic); + Boolean result = publisher.publish(topic, body); + logger.error("Loop iteration " + i + " returned the boolean value " + result); + } + } + +} \ No newline at end of file diff --git a/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml new file mode 100755 index 00000000..c44a68ad --- /dev/null +++ b/message-router/publisher/sample.client/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file -- cgit 1.2.3-korg