71edc7fb50bb0e059743f584bdae8087a6ae299c
[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.opendaylight.yangtools.concepts.Registration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
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;
34
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";
42         }
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));
46
47     }
48
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)
57         ));
58     }
59
60     void onPublishNotificationProcessService(PublishNotificationProcessService notification) {
61         try {
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);
66         }
67
68     }
69
70     private void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
71     }
72
73     private void onPublishTapiNotificationService(PublishTapiNotificationService notification) {
74     }
75 }