861f9ca2dd3ff386de06694d61bf15d8b0e63a84
[transportpce.git] / dmaap-client / src / main / java / org / opendaylight / transportpce / dmaap / client / listener / NbiNotificationsHandler.java
1 /*
2  * Copyright © 2021 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.dmaap.client.listener;
9
10 import java.util.Set;
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.rev230726.PublishNotificationAlarmService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.PublishNotificationProcessService;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.PublishTapiNotificationService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NbiNotificationsHandler {
29     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsHandler.class);
30     private String topic = "unauthenticated.TPCE";
31     private EventsApi api;
32
33     public NbiNotificationsHandler(String baseUrl, String username, String password) {
34         LOG.info("Dmaap server {} for user {}", baseUrl, username);
35         Client client = ClientBuilder.newClient();
36         if (username != null && username.isBlank() && password != null && !password.isBlank()) {
37             HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
38             client.register(authFeature);
39             topic = "authenticated.TPCE";
40         }
41         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
42         .register(JacksonFeature.class).register(JsonConfigurator.class);
43         api = WebResourceFactory.newResource(EventsApi.class, client.target(baseUrl));
44
45     }
46
47     public CompositeListener getCompositeListener() {
48         return new CompositeListener(Set.of(
49             new CompositeListener.Component<>(
50                 PublishNotificationProcessService.class, this::onPublishNotificationProcessService),
51             new CompositeListener.Component<>(
52                 PublishNotificationAlarmService.class, this::onPublishNotificationAlarmService),
53             new CompositeListener.Component<>(
54                 PublishTapiNotificationService.class, this::onPublishTapiNotificationService)
55         ));
56     }
57
58     void onPublishNotificationProcessService(PublishNotificationProcessService notification) {
59         try {
60             CreatedEvent response = api.sendEvent(topic, notification);
61             LOG.info("Response received {}", response);
62         } catch (WebApplicationException e) {
63             LOG.warn("Cannot send event {}", notification, e);
64         }
65
66     }
67
68     private void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
69     }
70
71     private void onPublishTapiNotificationService(PublishTapiNotificationService notification) {
72     }
73 }