From b9d69f7ff29bf63e2b624f8a7329ac58c6a65eca Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Wed, 15 Jan 2020 11:55:39 -0500 Subject: [PATCH] WIP2 --- beacon/beacon-core/build.gradle | 16 +++++ .../java/edu/internet2/tap/beacon/Beacon.java | 22 +++++++ .../internet2/tap/beacon/BeaconPublisher.java | 10 +++ .../tap/beacon/DefaultBeaconPublisher.java | 65 +++++++++++++++++++ .../beacon/DefaultBeaconPublisherTests.java | 13 ++++ beacon/beacon-spring/build.gradle | 41 ++++++++++++ settings.gradle | 2 +- 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 beacon/beacon-core/build.gradle create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java create mode 100644 beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java create mode 100644 beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java create mode 100644 beacon/beacon-spring/build.gradle diff --git a/beacon/beacon-core/build.gradle b/beacon/beacon-core/build.gradle new file mode 100644 index 000000000..94f9ab994 --- /dev/null +++ b/beacon/beacon-core/build.gradle @@ -0,0 +1,16 @@ +apply plugin: 'java' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + jcenter() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java new file mode 100644 index 000000000..7e7dec5a5 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/Beacon.java @@ -0,0 +1,22 @@ +package edu.internet2.tap.beacon; + +/** + * Exposes expected names of environment variables holding beacon config data. + */ +public final class Beacon { + + private Beacon() { + } + + public static final String LOG_HOST = "LOGHOST"; + + public static final String LOG_PORT = "LOGPORT"; + + public static final String IMAGE = "IMAGE"; + + public static final String VERSION = "VERSION"; + + public static final String TIERVERSION = "TIERVERSION"; + + public static final String MAINTAINER = "MAINTAINER"; +} diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java new file mode 100644 index 000000000..80f4f77e4 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/BeaconPublisher.java @@ -0,0 +1,10 @@ +package edu.internet2.tap.beacon; + +/** + * Simple SPI allowing implementations to publish to beacon service utilizing Runnable API + * so that publishing code could run in separate threads of execution. + * + * @author Dmitriy Kopylenko + */ +public interface BeaconPublisher extends Runnable { +} diff --git a/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java new file mode 100644 index 000000000..d7a4e9837 --- /dev/null +++ b/beacon/beacon-core/src/main/java/edu/internet2/tap/beacon/DefaultBeaconPublisher.java @@ -0,0 +1,65 @@ +package edu.internet2.tap.beacon; + +import java.util.HashMap; +import java.util.Map; + +import static edu.internet2.tap.beacon.Beacon.IMAGE; +import static edu.internet2.tap.beacon.Beacon.LOG_HOST; +import static edu.internet2.tap.beacon.Beacon.LOG_PORT; +import static edu.internet2.tap.beacon.Beacon.MAINTAINER; +import static edu.internet2.tap.beacon.Beacon.TIERVERSION; +import static edu.internet2.tap.beacon.Beacon.VERSION; + +/** + * Default implementation that knows the details about payload structure with its data and beacon endpoint details + * gathered by upstream components and passed to this implementation at object construction site. + * + * @author Dmitriy Kopylenko + */ +public class DefaultBeaconPublisher implements BeaconPublisher { + + private String endpointUri; + + private Map dataToPublish; + + public DefaultBeaconPublisher(Map beaconDetails) { + + + //Do data validation checks here. If any of the necessary beacon data not available here, throw a Runtime exception + if (beaconDetails == null) { + throw new IllegalArgumentException("beaconDetails Map must not be null"); + } + if (beaconDetails.get(LOG_HOST) == null + || beaconDetails.get(LOG_PORT) == null + || beaconDetails.get(IMAGE) == null + || beaconDetails.get(VERSION) == null + || beaconDetails.get(TIERVERSION) == null + || beaconDetails.get(MAINTAINER) == null) { + throw new IllegalArgumentException("Not all the necessary beacon data is available to be able to publish to beacon"); + } + this.endpointUri = String.format("http://%s:%s", beaconDetails.get(LOG_HOST), beaconDetails.get(LOG_PORT)); + + this.dataToPublish = new HashMap<>(); + this.dataToPublish.put("msgType", "TIERBEACON"); + this.dataToPublish.put("msgName", "TIER"); + this.dataToPublish.put("msgVersion", "1.0"); + this.dataToPublish.put("tbProduct", beaconDetails.get(IMAGE)); + this.dataToPublish.put("tbProductVersion", beaconDetails.get(VERSION)); + this.dataToPublish.put("tbTIERRelease", beaconDetails.get(TIERVERSION)); + this.dataToPublish.put("tbMaintainer", beaconDetails.get(MAINTAINER)); + } + + @Override + public void run() { + //log.debug("Posting data {} to beacon endpoint {}", dataToPublish, endpointUri); + } + + //Below are package-private getters used in unit tests + String getEndpointUri() { + return this.endpointUri; + } + + Map getDataToPublish() { + return this.dataToPublish; + } +} diff --git a/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java b/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java new file mode 100644 index 000000000..9cc000625 --- /dev/null +++ b/beacon/beacon-core/src/test/java/edu/internet2/tap/beacon/DefaultBeaconPublisherTests.java @@ -0,0 +1,13 @@ +package edu.internet2.tap.beacon; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class DefaultBeaconPublisherTests { + + @Test + public void checkCorrectInvariantsWithBeaconDataNull() { + Assertions.assertTrue(true); + //new DefaultBeaconPublisher(null); + } +} diff --git a/beacon/beacon-spring/build.gradle b/beacon/beacon-spring/build.gradle new file mode 100644 index 000000000..4b0ba4813 --- /dev/null +++ b/beacon/beacon-spring/build.gradle @@ -0,0 +1,41 @@ +/*plugins { + id 'groovy' + id 'jacoco' + id 'org.springframework.boot' version '2.0.0.RELEASE' apply false + id 'io.spring.dependency-management' version '1.0.6.RELEASE' + id 'io.franzbecker.gradle-lombok' version '1.13' +}*/ + +apply plugin: 'java' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + jcenter() +} + +/*dependencyManagement { + imports { + mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES + } +}*/ + +/*lombok { + version = "1.18.4" + //TODO: get new sha256 + sha256 = "" +}*/ + +/*dependencies { + compileOnly project(':backend') + testCompile project(':backend') + testCompile "org.springframework.boot:spring-boot-starter-test" + testCompile "org.spockframework:spock-core:1.1-groovy-2.4" + testCompile "org.spockframework:spock-spring:1.1-groovy-2.4" + //annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" +}*/ + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2' +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 1b37654ef..f71bfae82 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:core', 'beacon:spring' +include 'backend', 'ui', 'pac4j-module', 'beacon', 'beacon:beacon-core', 'beacon:beacon-spring'