2 * Copyright © 2020 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.nbinotifications.producer;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.io.IOException;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.HashSet;
17 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import org.apache.kafka.clients.producer.MockProducer;
21 import org.apache.kafka.common.serialization.StringSerializer;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
26 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
27 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
28 import org.opendaylight.transportpce.common.network.RequestProcessor;
29 import org.opendaylight.transportpce.nbinotifications.impl.NbiNotificationsImpl;
30 import org.opendaylight.transportpce.nbinotifications.serialization.ConfigConstants;
31 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationAlarmServiceSerializer;
32 import org.opendaylight.transportpce.nbinotifications.serialization.NotificationServiceSerializer;
33 import org.opendaylight.transportpce.nbinotifications.serialization.TapiNotificationSerializer;
34 import org.opendaylight.transportpce.nbinotifications.utils.NotificationServiceDataUtils;
35 import org.opendaylight.transportpce.nbinotifications.utils.TopicManager;
36 import org.opendaylight.transportpce.test.AbstractTest;
37 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationAlarmService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationProcessService;
39 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.NotificationTapiService;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.CreateNotificationSubscriptionServiceInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.CreateNotificationSubscriptionServiceOutput;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.create.notification.subscription.service.input.SubscriptionFilter;
44 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev181210.create.notification.subscription.service.input.SubscriptionFilterBuilder;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
49 public class PublisherTest extends AbstractTest {
50 private JsonStringConverter<NotificationProcessService> converterService;
51 private JsonStringConverter<NotificationAlarmService> converterAlarm;
52 private JsonStringConverter<NotificationTapiService> converterTapiService;
53 private Publisher<NotificationProcessService> publisherService;
54 private Publisher<NotificationAlarmService> publisherAlarm;
55 private Publisher<NotificationTapiService> publisherTapiService;
56 private MockProducer<String, NotificationProcessService> mockProducer;
57 private MockProducer<String, NotificationAlarmService> mockAlarmProducer;
58 private MockProducer<String, NotificationTapiService> mockTapiProducer;
59 private NbiNotificationsImpl nbiNotificationsImpl;
60 private TopicManager topicManager;
62 public static NetworkTransactionService networkTransactionService;
65 public void setUp() throws ExecutionException, InterruptedException {
66 topicManager = TopicManager.getInstance();
67 converterService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
68 converterAlarm = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
69 converterTapiService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
70 NotificationServiceSerializer serializerService = new NotificationServiceSerializer();
71 NotificationAlarmServiceSerializer serializerAlarm = new NotificationAlarmServiceSerializer();
72 TapiNotificationSerializer serializerTapi = new TapiNotificationSerializer();
73 Map<String, Object> properties = Map.of(ConfigConstants.CONVERTER, converterService);
74 Map<String, Object> propertiesAlarm = Map.of(ConfigConstants.CONVERTER, converterAlarm);
75 Map<String, Object> propertiesTapi = Map.of(ConfigConstants.CONVERTER, converterTapiService);
76 serializerService.configure(properties, false);
77 serializerAlarm.configure(propertiesAlarm, false);
78 serializerTapi.configure(propertiesTapi, false);
79 mockProducer = new MockProducer<>(true, new StringSerializer(), serializerService);
80 mockAlarmProducer = new MockProducer<>(true, new StringSerializer(), serializerAlarm);
81 mockTapiProducer = new MockProducer<>(true, new StringSerializer(), serializerTapi);
82 publisherService = new Publisher<>("test", mockProducer);
83 publisherAlarm = new Publisher<>("test", mockAlarmProducer);
84 publisherTapiService = new Publisher<>("test", mockTapiProducer);
85 MockitoAnnotations.openMocks(this);
86 networkTransactionService = new NetworkTransactionImpl(
87 new RequestProcessor(getDataStoreContextUtil().getDataBroker()));
88 topicManager.setTapiConverter(converterTapiService);
89 NotificationServiceDataUtils.createTapiContext(networkTransactionService);
90 nbiNotificationsImpl = new NbiNotificationsImpl(converterService, converterAlarm, converterTapiService,
91 "localhost:8080", networkTransactionService, topicManager);
95 public void sendEventServiceShouldBeSuccessful() throws IOException {
96 String json = Files.readString(Paths.get("src/test/resources/event.json"));
97 NotificationProcessService notificationProcessService = converterService
98 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationProcessService.QNAME),
99 json, JSONCodecFactorySupplier.RFC7951);
100 publisherService.sendEvent(notificationProcessService, notificationProcessService.getConnectionType().name());
101 assertEquals("We should have one message", 1, mockProducer.history().size());
102 assertEquals("Key should be test", "test", mockProducer.history().get(0).key());
106 public void sendEventAlarmShouldBeSuccessful() throws IOException {
107 String json = Files.readString(Paths.get("src/test/resources/event_alarm_service.json"));
108 NotificationAlarmService notificationAlarmService = converterAlarm
109 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationAlarmService.QNAME),
110 json, JSONCodecFactorySupplier.RFC7951);
111 publisherAlarm.sendEvent(notificationAlarmService, "alarm"
112 + notificationAlarmService.getConnectionType().getName());
113 assertEquals("We should have one message", 1, mockAlarmProducer.history().size());
114 assertEquals("Key should be test", "test", mockAlarmProducer.history().get(0).key());
118 public void sendTapiEventShouldBeSuccessful() throws IOException {
119 CreateNotificationSubscriptionServiceInputBuilder builder
120 = NotificationServiceDataUtils.buildNotificationSubscriptionServiceInputBuilder();
121 SubscriptionFilter subscriptionFilter = new SubscriptionFilterBuilder(builder.getSubscriptionFilter())
122 .setRequestedObjectIdentifier(new HashSet<>(List.of(new Uuid("76d8f07b-ead5-4132-8eb8-cf3fdef7e079"))))
124 builder.setSubscriptionFilter(subscriptionFilter);
125 ListenableFuture<RpcResult<CreateNotificationSubscriptionServiceOutput>> result =
126 nbiNotificationsImpl.createNotificationSubscriptionService(builder.build());
127 String json = Files.readString(Paths.get("src/test/resources/tapi_event.json"));
128 NotificationTapiService notificationTapiService = converterTapiService
129 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationTapiService.QNAME),
130 json, JSONCodecFactorySupplier.RFC7951);
131 publisherTapiService.sendEvent(notificationTapiService, "");
132 assertEquals("We should have one message", 1, mockTapiProducer.history().size());
133 assertEquals("Key should be test", "test", mockTapiProducer.history().get(0).key());