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