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;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
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.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.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.nbinotifications.impl.NbiNotificationsImpl;
29 import org.opendaylight.transportpce.nbinotifications.impl.rpc.CreateNotificationSubscriptionServiceImpl;
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.rev230728.NotificationAlarmService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationProcessService;
39 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationTapiService;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.CreateNotificationSubscriptionServiceInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.create.notification.subscription.service.input.SubscriptionFilter;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.notification.rev221121.create.notification.subscription.service.input.SubscriptionFilterBuilder;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
47 public class PublisherTest extends AbstractTest {
48 private static NetworkTransactionService networkTransactionService;
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;
64 void setUp() throws ExecutionException, InterruptedException {
65 topicManager = TopicManager.getInstance();
66 converterService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
67 converterAlarm = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
68 converterTapiService = new JsonStringConverter<>(getDataStoreContextUtil().getBindingDOMCodecServices());
69 NotificationServiceSerializer serializerService = new NotificationServiceSerializer();
70 NotificationAlarmServiceSerializer serializerAlarm = new NotificationAlarmServiceSerializer();
71 TapiNotificationSerializer serializerTapi = new TapiNotificationSerializer();
72 Map<String, Object> properties = Map.of(ConfigConstants.CONVERTER, converterService);
73 Map<String, Object> propertiesAlarm = Map.of(ConfigConstants.CONVERTER, converterAlarm);
74 Map<String, Object> propertiesTapi = Map.of(ConfigConstants.CONVERTER, converterTapiService);
75 serializerService.configure(properties, false);
76 serializerAlarm.configure(propertiesAlarm, false);
77 serializerTapi.configure(propertiesTapi, false);
78 mockProducer = new MockProducer<>(true, new StringSerializer(), serializerService);
79 mockAlarmProducer = new MockProducer<>(true, new StringSerializer(), serializerAlarm);
80 mockTapiProducer = new MockProducer<>(true, new StringSerializer(), serializerTapi);
81 publisherService = new Publisher<>("test", mockProducer);
82 publisherAlarm = new Publisher<>("test", mockAlarmProducer);
83 publisherTapiService = new Publisher<>("test", mockTapiProducer);
84 MockitoAnnotations.openMocks(this);
85 networkTransactionService = new NetworkTransactionImpl(getDataBroker());
86 topicManager.setTapiConverter(converterTapiService);
87 NotificationServiceDataUtils.createTapiContext(networkTransactionService);
88 nbiNotificationsImpl = new NbiNotificationsImpl(converterService, converterAlarm, converterTapiService,
89 "localhost:8080", networkTransactionService, topicManager);
93 void sendEventServiceShouldBeSuccessful() throws IOException {
94 String json = Files.readString(Paths.get("src/test/resources/event.json"));
95 NotificationProcessService notificationProcessService = converterService
96 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationProcessService.QNAME),
97 json, JSONCodecFactorySupplier.RFC7951);
98 publisherService.sendEvent(notificationProcessService, notificationProcessService.getConnectionType().name());
99 assertEquals(1, mockProducer.history().size(), "We should have one message");
100 assertEquals("test", mockProducer.history().get(0).key(), "Key should be test");
104 void sendEventAlarmShouldBeSuccessful() throws IOException {
105 String json = Files.readString(Paths.get("src/test/resources/event_alarm_service.json"));
106 NotificationAlarmService notificationAlarmService = converterAlarm
107 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationAlarmService.QNAME),
108 json, JSONCodecFactorySupplier.RFC7951);
109 publisherAlarm.sendEvent(notificationAlarmService, "alarm"
110 + notificationAlarmService.getConnectionType().getName());
111 assertEquals(1, mockAlarmProducer.history().size(), "We should have one message");
112 assertEquals("test", mockAlarmProducer.history().get(0).key(), "Key should be test");
116 void sendTapiEventShouldBeSuccessful() throws IOException {
117 CreateNotificationSubscriptionServiceInputBuilder builder
118 = NotificationServiceDataUtils.buildNotificationSubscriptionServiceInputBuilder();
119 SubscriptionFilter subscriptionFilter = new SubscriptionFilterBuilder(builder.getSubscriptionFilter())
120 .setRequestedObjectIdentifier(new HashSet<>(List.of(new Uuid("76d8f07b-ead5-4132-8eb8-cf3fdef7e079"))))
122 builder.setSubscriptionFilter(subscriptionFilter);
124 new CreateNotificationSubscriptionServiceImpl(nbiNotificationsImpl, topicManager).invoke(builder.build());
125 String json = Files.readString(Paths.get("src/test/resources/tapi_event.json"));
126 NotificationTapiService notificationTapiService = converterTapiService
127 .createDataObjectFromJsonString(YangInstanceIdentifier.of(NotificationTapiService.QNAME),
128 json, JSONCodecFactorySupplier.RFC7951);
129 publisherTapiService.sendEvent(notificationTapiService, "");
130 assertEquals(1, mockTapiProducer.history().size(), "We should have one message");
131 assertEquals("test", mockTapiProducer.history().get(0).key(), "Key should be test");