fcf0b000dd8c80e88ab4c782e025cc2b4af76c93
[transportpce.git] / dmaap-client / src / main / java / org / opendaylight / transportpce / dmaap / client / listener / NbiNotificationsListenerImpl.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 javax.ws.rs.WebApplicationException;
11 import javax.ws.rs.client.Client;
12 import javax.ws.rs.client.ClientBuilder;
13 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
14 import org.glassfish.jersey.client.proxy.WebResourceFactory;
15 import org.glassfish.jersey.jackson.JacksonFeature;
16 import org.glassfish.jersey.logging.LoggingFeature;
17 import org.opendaylight.transportpce.dmaap.client.resource.EventsApi;
18 import org.opendaylight.transportpce.dmaap.client.resource.config.JsonConfigurator;
19 import org.opendaylight.transportpce.dmaap.client.resource.model.CreatedEvent;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NbiNotificationsListener;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationAlarmService;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishTapiNotificationService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NbiNotificationsListenerImpl implements NbiNotificationsListener {
28     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsListenerImpl.class);
29     private String topic = "unauthenticated.TPCE";
30     private EventsApi api;
31
32     public NbiNotificationsListenerImpl(String baseUrl, String username, String password) {
33         LOG.info("Dmaap server {} for user {}", baseUrl, username);
34         Client client = ClientBuilder.newClient();
35         if (username != null && username.isBlank() && password != null && !password.isBlank()) {
36             HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
37             client.register(authFeature);
38             topic = "authenticated.TPCE";
39         }
40         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
41         .register(JacksonFeature.class).register(JsonConfigurator.class);
42         api = WebResourceFactory.newResource(EventsApi.class, client.target(baseUrl));
43
44     }
45
46     @Override
47     public void onPublishNotificationProcessService(PublishNotificationProcessService notification) {
48         try {
49             CreatedEvent response = api.sendEvent(topic, notification);
50             LOG.info("Response received {}", response);
51         } catch (WebApplicationException e) {
52             LOG.warn("Cannot send event {}", notification, e);
53         }
54
55     }
56
57     @Override
58     public void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
59     }
60
61     @Override
62     public void onPublishTapiNotificationService(PublishTapiNotificationService notification) {
63     }
64 }