Dmaap client 82/94982/19
authormanuedelf <emmanuelle.delfour@orange.com>
Tue, 2 Feb 2021 22:08:17 +0000 (23:08 +0100)
committerguillaume.lambert <guillaume.lambert@orange.com>
Thu, 11 Mar 2021 15:51:27 +0000 (16:51 +0100)
- REST client in order to send TPCE notification
on ONAP Dmaap Message router.
- Add a new maven module dmaap-client.
- Use customized jackson serializer as a temporary workaround
while waiting jackson support in yang tools
https://git.opendaylight.org/gerrit/c/yangtools/+/94852

JIRA: TRNSPRTPCE-394
Signed-off-by: manuedelf <emmanuelle.delfour@orange.com>
Change-Id: If31ee8befccba0b7e0e34b7cdadb27cc87858fed

19 files changed:
dmaap-client/pom.xml [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProvider.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImpl.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApi.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/JsonConfigurator.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/LgxSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PortSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceModule.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/RxDirectionSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceAEndSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceZEndSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/TxDirectionSerializer.java [new file with mode: 0644]
dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/model/CreatedEvent.java [new file with mode: 0644]
dmaap-client/src/main/resources/OSGI-INF/blueprint/dmaap-blueprint.xml [new file with mode: 0644]
dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProviderTest.java [new file with mode: 0644]
dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImplTest.java [new file with mode: 0644]
dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApiStub.java [new file with mode: 0644]
pom.xml

diff --git a/dmaap-client/pom.xml b/dmaap-client/pom.xml
new file mode 100644 (file)
index 0000000..0560fbc
--- /dev/null
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright © 2021 Orange and others. All rights reserved. This program and the accompanying materials
+    are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
+    and is available at http://www.eclipse.org/legal/epl-v10.html -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.opendaylight.mdsal</groupId>
+        <artifactId>binding-parent</artifactId>
+        <version>7.0.5</version>
+        <relativePath />
+    </parent>
+    <groupId>org.opendaylight.transportpce</groupId>
+    <artifactId>transportpce-dmaap-client</artifactId>
+    <version>3.0.0-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+    <description>client to send message to Dmaap message router</description>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>transportpce-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.mdsal</groupId>
+            <artifactId>mdsal-binding-api</artifactId>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.ext</groupId>
+            <artifactId>jersey-proxy-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.inject</groupId>
+            <artifactId>jersey-hk2</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.media</groupId>
+            <artifactId>jersey-media-json-jackson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.datatype</groupId>
+            <artifactId>jackson-datatype-jsr310</artifactId>
+        </dependency>
+        <!-- Testing dependencies -->
+        <dependency>
+            <groupId>org.glassfish.jersey.test-framework</groupId>
+            <artifactId>jersey-test-framework-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
+            <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProvider.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProvider.java
new file mode 100644 (file)
index 0000000..adc4d06
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.impl;
+
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.transportpce.dmaap.client.listener.NbiNotificationsListenerImpl;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NbiNotificationsListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DmaapClientProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(DmaapClientProvider.class);
+    private ListenerRegistration<NbiNotificationsListener> listenerRegistration;
+    private NotificationService notificationService;
+    private final String baseUrl;
+    private final String username;
+    private final String password;
+
+    public DmaapClientProvider(NotificationService notificationService, String baseUrl,
+            String username, String password) {
+        this.notificationService = notificationService;
+        this.baseUrl = baseUrl;
+        this.username = username;
+        this.password = password;
+    }
+
+    /**
+     * Method called when the blueprint container is created.
+     */
+    public void init() {
+        LOG.info("DmaapClientProvider Session Initiated");
+        listenerRegistration = notificationService.registerNotificationListener(
+                new NbiNotificationsListenerImpl(baseUrl, username, password));
+    }
+
+    /**
+     * Method called when the blueprint container is destroyed.
+     */
+    public void close() {
+        listenerRegistration.close();
+        LOG.info("DmaapClientProvider Closed");
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImpl.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImpl.java
new file mode 100644 (file)
index 0000000..ed84dae
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.listener;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
+import org.glassfish.jersey.client.proxy.WebResourceFactory;
+import org.glassfish.jersey.jackson.JacksonFeature;
+import org.glassfish.jersey.logging.LoggingFeature;
+import org.opendaylight.transportpce.dmaap.client.resource.EventsApi;
+import org.opendaylight.transportpce.dmaap.client.resource.config.JsonConfigurator;
+import org.opendaylight.transportpce.dmaap.client.resource.model.CreatedEvent;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NbiNotificationsListener;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NbiNotificationsListenerImpl implements NbiNotificationsListener {
+    private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsListenerImpl.class);
+    private String topic = "unauthenticated.TPCE";
+    private EventsApi api;
+
+    public NbiNotificationsListenerImpl(String baseUrl, String username, String password) {
+        LOG.info("Dmaap server {} for user {}", baseUrl, username);
+        Client client = ClientBuilder.newClient();
+        if (username != null && username.isBlank() && password != null && !password.isBlank()) {
+            HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
+            client.register(authFeature);
+            topic = "authenticated.TPCE";
+        }
+        client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
+        .register(JacksonFeature.class).register(JsonConfigurator.class);
+        api = WebResourceFactory.newResource(EventsApi.class, client.target(baseUrl));
+
+    }
+
+    @Override
+    public void onPublishNotificationService(PublishNotificationService notification) {
+        try {
+            CreatedEvent response = api.sendEvent(topic, notification);
+            LOG.info("Response received {}", response);
+        } catch (WebApplicationException e) {
+            LOG.warn("Cannot send event {}", notification, e);
+        }
+
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApi.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApi.java
new file mode 100644 (file)
index 0000000..e9d936c
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import org.opendaylight.transportpce.dmaap.client.resource.model.CreatedEvent;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
+
+@Path("/events")
+public interface EventsApi {
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("/{topic}")
+    CreatedEvent sendEvent(@PathParam("topic") String topic, PublishNotificationService event);
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/JsonConfigurator.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/JsonConfigurator.java
new file mode 100644 (file)
index 0000000..62676c0
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import javax.ws.rs.ext.ContextResolver;
+
+public class JsonConfigurator implements ContextResolver<ObjectMapper> {
+
+    private final ObjectMapper mapper;
+
+    public JsonConfigurator() {
+        mapper = new ObjectMapper();
+        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        mapper.registerModule(new JavaTimeModule());
+        mapper.registerModule(new PublishNotificationServiceModule());
+    }
+
+    @Override
+    public ObjectMapper getContext(Class<?> type) {
+        return mapper;
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/LgxSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/LgxSerializer.java
new file mode 100644 (file)
index 0000000..8f847a9
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.lgx.Lgx;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class LgxSerializer extends StdSerializer<Lgx> {
+    private static final long serialVersionUID = 1L;
+
+    public LgxSerializer() {
+        super(Lgx.class);
+    }
+
+    @Override
+    public void serialize(Lgx value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            gen.writeStringField("lgx-port-rack", value.getLgxPortRack());
+            gen.writeStringField("lgx-port-shelf", value.getLgxPortShelf());
+            gen.writeStringField("lgx-device-name", value.getLgxDeviceName());
+            gen.writeStringField("lgx-port-name", value.getLgxPortName());
+            gen.writeEndObject();
+        }
+    }
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PortSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PortSerializer.java
new file mode 100644 (file)
index 0000000..bdce081
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.port.Port;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class PortSerializer extends StdSerializer<Port> {
+    private static final long serialVersionUID = 1L;
+
+    public PortSerializer() {
+        super(Port.class);
+    }
+
+    @Override
+    public void serialize(Port value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            gen.writeStringField("port-rack", value.getPortRack());
+            gen.writeStringField("port-shelf", value.getPortShelf());
+            gen.writeStringField("port-device-name", value.getPortDeviceName());
+            gen.writeStringField("port-name", value.getPortName());
+            gen.writeStringField("port-type", value.getPortType());
+            gen.writeEndObject();
+        }
+    }
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceModule.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceModule.java
new file mode 100644 (file)
index 0000000..6b61557
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirection;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirection;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.lgx.Lgx;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.port.Port;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEnd;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceZEnd;
+
+//This class is a temporary workaround while waiting jackson
+//support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class PublishNotificationServiceModule extends SimpleModule {
+
+    private static final long serialVersionUID = 1L;
+
+    public PublishNotificationServiceModule() {
+        super(PackageVersion.VERSION);
+        addSerializer(PublishNotificationService.class, new PublishNotificationServiceSerializer());
+        addSerializer(Lgx.class, new LgxSerializer());
+        addSerializer(Port.class, new PortSerializer());
+        addSerializer(RxDirection.class, new RxDirectionSerializer());
+        addSerializer(TxDirection.class, new TxDirectionSerializer());
+        addSerializer(ServiceAEnd.class, new ServiceAEndSerializer());
+        addSerializer(ServiceZEnd.class, new ServiceZEndSerializer());
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/PublishNotificationServiceSerializer.java
new file mode 100644 (file)
index 0000000..0ed9b7b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class PublishNotificationServiceSerializer extends StdSerializer<PublishNotificationService> {
+    private static final long serialVersionUID = 1L;
+
+    public PublishNotificationServiceSerializer() {
+        super(PublishNotificationService.class);
+    }
+
+    @Override
+    public void serialize(PublishNotificationService value, JsonGenerator gen, SerializerProvider provider)
+            throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            gen.writeStringField("common-id", value.getCommonId());
+            gen.writeStringField("message", value.getMessage());
+            gen.writeStringField("response-failed", value.getResponseFailed());
+            gen.writeStringField("service-name", value.getServiceName());
+            gen.writeStringField("topic", value.getTopic());
+            if (value.getOperationalState() != null) {
+                gen.writeStringField("operational-state", value.getOperationalState().getName());
+            }
+            gen.writeObjectField("service-a-end", value.getServiceAEnd());
+            gen.writeObjectField("service-z-end", value.getServiceZEnd());
+            gen.writeEndObject();
+        }
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/RxDirectionSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/RxDirectionSerializer.java
new file mode 100644 (file)
index 0000000..376b6be
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirection;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class RxDirectionSerializer extends StdSerializer<RxDirection> {
+    private static final long serialVersionUID = 1L;
+
+    public RxDirectionSerializer() {
+        super(RxDirection.class);
+    }
+
+    @Override
+    public void serialize(RxDirection value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            if (value.getPort() != null) {
+                gen.writeObjectField("port", value.getPort());
+            }
+            if (value.getLgx() != null) {
+                gen.writeObjectField("lgx", value.getLgx());
+            }
+            gen.writeEndObject();
+        }
+    }
+
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceAEndSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceAEndSerializer.java
new file mode 100644 (file)
index 0000000..55bfaea
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEnd;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class ServiceAEndSerializer extends StdSerializer<ServiceAEnd> {
+    private static final long serialVersionUID = 1L;
+
+    public ServiceAEndSerializer() {
+        super(ServiceAEnd.class);
+    }
+
+    @Override
+    public void serialize(ServiceAEnd value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            gen.writeStringField("clli", value.getClli());
+            if (value.getServiceFormat() != null) {
+                gen.writeStringField("service-format", value.getServiceFormat().getName());
+            }
+            if (value.getNodeId() != null) {
+                gen.writeStringField("node-id", value.getNodeId().getValue());
+            }
+            if (value.getServiceRate() != null) {
+                gen.writeNumberField("service-rate", value.getServiceRate().intValue());
+            }
+            if (value.getOpticType() != null) {
+                gen.writeStringField("optic-type", value.getOpticType().getName());
+            }
+            if (value.getTxDirection() != null) {
+                gen.writeObjectField("tx-direction", value.getTxDirection());
+            }
+            if (value.getRxDirection() != null) {
+                gen.writeObjectField("rx-direction", value.getRxDirection());
+            }
+            gen.writeEndObject();
+        }
+    }
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceZEndSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/ServiceZEndSerializer.java
new file mode 100644 (file)
index 0000000..1d65b11
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceZEnd;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class ServiceZEndSerializer extends StdSerializer<ServiceZEnd> {
+    private static final long serialVersionUID = 1L;
+
+    public ServiceZEndSerializer() {
+        super(ServiceZEnd.class);
+    }
+
+    @Override
+    public void serialize(ServiceZEnd value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            gen.writeStringField("clli", value.getClli());
+            if (value.getServiceFormat() != null) {
+                gen.writeStringField("service-format", value.getServiceFormat().getName());
+            }
+            if (value.getNodeId() != null) {
+                gen.writeStringField("node-id", value.getNodeId().getValue());
+            }
+            if (value.getServiceRate() != null) {
+                gen.writeNumberField("service-rate", value.getServiceRate().intValue());
+            }
+            if (value.getOpticType() != null) {
+                gen.writeStringField("optic-type", value.getOpticType().getName());
+            }
+            if (value.getTxDirection() != null) {
+                gen.writeObjectField("tx-direction", value.getTxDirection());
+            }
+            if (value.getRxDirection() != null) {
+                gen.writeObjectField("rx-direction", value.getRxDirection());
+            }
+            gen.writeEndObject();
+        }
+    }
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/TxDirectionSerializer.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/config/TxDirectionSerializer.java
new file mode 100644 (file)
index 0000000..90942a8
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.config;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+import java.io.IOException;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirection;
+
+// This class is a temporary workaround while waiting jackson
+// support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
+public class TxDirectionSerializer extends StdSerializer<TxDirection> {
+    private static final long serialVersionUID = 1L;
+
+    public TxDirectionSerializer() {
+        super(TxDirection.class);
+    }
+
+    @Override
+    public void serialize(TxDirection value, JsonGenerator gen, SerializerProvider provider) throws IOException {
+        if (value != null) {
+            gen.writeStartObject();
+            if (value.getPort() != null) {
+                gen.writeObjectField("port", value.getPort());
+            }
+            if (value.getLgx() != null) {
+                gen.writeObjectField("lgx", value.getLgx());
+            }
+            gen.writeEndObject();
+        }
+    }
+}
diff --git a/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/model/CreatedEvent.java b/dmaap-client/src/main/java/org/opendaylight/transportpce/dmaap/client/resource/model/CreatedEvent.java
new file mode 100644 (file)
index 0000000..aa39efc
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource.model;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class CreatedEvent {
+
+    @JsonProperty("serverTimeMs")
+    private Integer serverTimeMs;
+
+    @JsonProperty("count")
+    private Integer count;
+
+    public Integer getServerTimeMs() {
+        return serverTimeMs;
+    }
+
+    public void setServerTimeMs(Integer serverTimeMs) {
+        this.serverTimeMs = serverTimeMs;
+    }
+
+    public Integer getCount() {
+        return count;
+    }
+
+    public void setCount(Integer count) {
+        this.count = count;
+    }
+
+    @Override
+    public String toString() {
+        return new StringBuilder()
+                .append("CreatedEvent [serverTimeMs=")
+                .append(serverTimeMs).append(", count=")
+                .append(count).append("]")
+                .toString();
+    }
+}
diff --git a/dmaap-client/src/main/resources/OSGI-INF/blueprint/dmaap-blueprint.xml b/dmaap-client/src/main/resources/OSGI-INF/blueprint/dmaap-blueprint.xml
new file mode 100644 (file)
index 0000000..459848e
--- /dev/null
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vi: set et smarttab sw=4 tabstop=4: -->
+<!-- Copyright © 2021 Orange and others. All rights reserved. This program and the accompanying materials
+    are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
+    and is available at http://www.eclipse.org/legal/epl-v10.html -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
+ xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
+    odl:use-default-for-reference-types="true">
+    <cm:property-placeholder persistent-id="org.opendaylight.transportpce.dmaap" update-strategy="reload">
+        <cm:default-properties>
+            <cm:property name="dmaap.baseUrl" value="http://localhost:8080" />
+            <cm:property name="dmaap.username" value="" />
+             <cm:property name="dmaap.password" value="" />
+        </cm:default-properties>
+    </cm:property-placeholder>
+    <reference id="notificationService" interface="org.opendaylight.mdsal.binding.api.NotificationService"/>
+
+    <bean id="provider"
+        class="org.opendaylight.transportpce.dmaap.client.impl.DmaapClientProvider"
+        init-method="init" destroy-method="close">
+        <argument ref="notificationService" />
+        <argument value="${dmaap.baseUrl}"></argument>
+        <argument value="${dmaap.username}"></argument>
+        <argument value="${dmaap.password}"></argument>
+    </bean>
+</blueprint>
diff --git a/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProviderTest.java b/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/impl/DmaapClientProviderTest.java
new file mode 100644 (file)
index 0000000..1ebfade
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.impl;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.transportpce.dmaap.client.listener.NbiNotificationsListenerImpl;
+
+
+public class DmaapClientProviderTest {
+
+    @Mock
+    private NotificationService notificationService;
+
+    @Before
+    public void init() {
+        MockitoAnnotations.openMocks(this);
+
+    }
+
+    @Test
+    public void testInitRegisterNbiNotificationsToRpcRegistry() {
+        DmaapClientProvider provider =  new DmaapClientProvider(notificationService, "http://localhost", "username", "password");
+        provider.init();
+        (verify(notificationService, times(1)))
+                .registerNotificationListener(Mockito.any(NbiNotificationsListenerImpl.class));
+    }
+
+}
diff --git a/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImplTest.java b/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/listener/NbiNotificationsListenerImplTest.java
new file mode 100644 (file)
index 0000000..710bf1b
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.listener;
+
+import static org.junit.Assert.assertEquals;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+import java.util.List;
+import javax.ws.rs.core.Application;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.test.TestProperties;
+import org.junit.Test;
+import org.opendaylight.transportpce.dmaap.client.resource.EventsApiStub;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirectionBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirectionBuilder;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NbiNotificationsListener;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationServiceBuilder;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEndBuilder;
+import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceZEndBuilder;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.slf4j.LoggerFactory;
+
+public class NbiNotificationsListenerImplTest extends JerseyTest {
+    @Override
+    protected Application configure() {
+        enable(TestProperties.LOG_TRAFFIC);
+        enable(TestProperties.DUMP_ENTITY);
+        return new ResourceConfig(EventsApiStub.class);
+    }
+
+    @Test
+    public void onPublishNotificationServiceTest() {
+        Logger logger = (Logger) LoggerFactory.getLogger(NbiNotificationsListenerImpl.class);
+        ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
+        listAppender.start();
+        logger.addAppender(listAppender);
+        NbiNotificationsListener listener = new NbiNotificationsListenerImpl("http://localhost:9998", null, null);
+        PublishNotificationService notification = new PublishNotificationServiceBuilder().setCommonId("CommonId")
+                .setMessage("Service implemented")
+                .setOperationalState(State.InService)
+                .setTopic("topic")
+                .setConnectionType(ConnectionType.Service)
+                .setServiceAEnd(new ServiceAEndBuilder()
+                        .setClli("clli")
+                        .setNodeId(new org.opendaylight.yang.gen.v1.http
+                                .org.openroadm.common.node.types.rev181130.NodeIdType("nodeidtype"))
+                        .setServiceFormat(ServiceFormat.Ethernet)
+                        .setServiceRate(Uint32.valueOf(100))
+                        .setRxDirection(new RxDirectionBuilder().build())
+                        .setTxDirection(new TxDirectionBuilder().build())
+                        .build())
+                .setServiceZEnd(new ServiceZEndBuilder()
+                        .setClli("clli")
+                        .setNodeId(new org.opendaylight.yang.gen.v1.http
+                                .org.openroadm.common.node.types.rev181130.NodeIdType("nodeidtype"))
+                        .setServiceFormat(ServiceFormat.Ethernet)
+                        .setServiceRate(Uint32.valueOf(100))
+                        .setRxDirection(new RxDirectionBuilder().build())
+                        .setTxDirection(new TxDirectionBuilder().build())
+                        .build())
+                .build();
+        listener.onPublishNotificationService(notification);
+        // as onPublishNotificationService is a void method, we check log message to be sur everything went well
+        List<ILoggingEvent> logsList = listAppender.list;
+        assertEquals("Response received CreatedEvent [serverTimeMs=1, count=1]", logsList.get(1).getFormattedMessage());
+
+    }
+}
diff --git a/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApiStub.java b/dmaap-client/src/test/java/org/opendaylight/transportpce/dmaap/client/resource/EventsApiStub.java
new file mode 100644 (file)
index 0000000..c71e0a9
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.transportpce.dmaap.client.resource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import org.opendaylight.transportpce.dmaap.client.resource.model.CreatedEvent;
+
+@Path("/events")
+public class EventsApiStub {
+
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("/{topic}")
+    public CreatedEvent sendEvent(@PathParam("topic") String topic, String event) {
+        CreatedEvent response = new CreatedEvent();
+        response.setCount(1);
+        response.setServerTimeMs(1);
+        return response;
+    }
+
+}
diff --git a/pom.xml b/pom.xml
index f8f8eea917ddbfa2cd50bf5e08be10e6a273ab4c..b3395f990a2a5cd0fa3e23eb2fee0cb2ca3d6b20 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -43,6 +43,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
         <module>servicehandler</module>
         <module>tapi</module>
         <module>nbinotifications</module>
+        <module>dmaap-client</module>
         <module>features</module>
         <module>karaf</module>
     </modules>