4a91e6a4cf1d80cb2c9572797af898cc57a3d442
[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.rev210813.NbiNotificationsListener;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmService;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationProcessService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class NbiNotificationsListenerImpl implements NbiNotificationsListener {
27     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsListenerImpl.class);
28     private String topic = "unauthenticated.TPCE";
29     private EventsApi api;
30
31     public NbiNotificationsListenerImpl(String baseUrl, String username, String password) {
32         LOG.info("Dmaap server {} for user {}", baseUrl, username);
33         Client client = ClientBuilder.newClient();
34         if (username != null && username.isBlank() && password != null && !password.isBlank()) {
35             HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password);
36             client.register(authFeature);
37             topic = "authenticated.TPCE";
38         }
39         client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName())))
40         .register(JacksonFeature.class).register(JsonConfigurator.class);
41         api = WebResourceFactory.newResource(EventsApi.class, client.target(baseUrl));
42
43     }
44
45     @Override
46     public void onPublishNotificationProcessService(PublishNotificationProcessService notification) {
47         try {
48             CreatedEvent response = api.sendEvent(topic, notification);
49             LOG.info("Response received {}", response);
50         } catch (WebApplicationException e) {
51             LOG.warn("Cannot send event {}", notification, e);
52         }
53
54     }
55
56     @Override
57     public void onPublishNotificationAlarmService(PublishNotificationAlarmService notification) {
58
59     }
60
61 }