2 * Copyright © 2021 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.dmaap.client.listener;
11 import javax.ws.rs.WebApplicationException;
12 import javax.ws.rs.client.Client;
13 import javax.ws.rs.client.ClientBuilder;
14 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
15 import org.glassfish.jersey.client.proxy.WebResourceFactory;
16 import org.glassfish.jersey.jackson.JacksonFeature;
17 import org.glassfish.jersey.logging.LoggingFeature;
18 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
19 import org.opendaylight.transportpce.dmaap.client.resource.EventsApi;
20 import org.opendaylight.transportpce.dmaap.client.resource.config.JsonConfigurator;
21 import org.opendaylight.transportpce.dmaap.client.resource.model.CreatedEvent;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishTapiNotificationService;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NbiNotificationsHandler {
30 private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsHandler.class);
31 private String topic = "unauthenticated.TPCE";
32 private EventsApi api;
33 private Registration reg;
35 public NbiNotificationsHandler(String baseUrl, String username, String password) {
36 LOG.info("Dmaap server {} for user {}", baseUrl, username);
37 Client client = ClientBuilder.newClient();
38 if (username != null && username.isBlank() && password != null && !password.isBlank()) {
39 HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
40 client.register(authFeature);
41 topic = "authenticated.TPCE";
43 client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
44 .register(JacksonFeature.class).register(JsonConfigurator.class);
45 api = WebResourceFactory.newResource(EventsApi.class, client.target(baseUrl));
49 public CompositeListener getCompositeListener() {
50 return new CompositeListener(Set.of(
51 new CompositeListener.Component<>(
52 PublishNotificationProcessService.class, this::onPublishNotificationProcessService),
53 new CompositeListener.Component<>(
54 PublishNotificationAlarmService.class, this::onPublishNotificationAlarmService),
55 new CompositeListener.Component<>(
56 PublishTapiNotificationService.class, this::onPublishTapiNotificationService)
60 void onPublishNotificationProcessService(PublishNotificationProcessService notification) {
62 CreatedEvent response = api.sendEvent(topic, notification);
63 LOG.info("Response received {}", response);
64 } catch (WebApplicationException e) {
65 LOG.warn("Cannot send event {}", notification, e);
70 private void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
73 private void onPublishTapiNotificationService(PublishTapiNotificationService notification) {