2 * Copyright © 2021 Orange. 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
9 package org.opendaylight.transportpce.servicehandler.listeners;
11 import static org.junit.Assert.fail;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.util.Collection;
20 import java.util.HashSet;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.binding.api.DataObjectModification;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
29 import org.opendaylight.transportpce.servicehandler.impl.ServicehandlerImpl;
30 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.ConnectionType;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.ServiceAEnd;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.ServiceAEndBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.ServiceZEnd;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.ServiceZEndBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.RxDirection;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.RxDirectionBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.RxDirectionKey;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.TxDirection;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.TxDirectionBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.endpoint.TxDirectionKey;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.lgx.LgxBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.service.port.PortBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev191129.ServiceFormat;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.ServicesBuilder;
50 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmService;
51 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmServiceBuilder;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.opendaylight.yangtools.yang.common.Uint8;
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class ServiceListenerTest {
59 private ServicehandlerImpl servicehandler;
61 private ServiceDataStoreOperations serviceDataStoreOperations;
63 private NotificationPublishService notificationPublishService;
66 public void testOnDataTreeChangedWhenDeleteService() {
67 @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
68 mock(DataObjectModification.class);
69 final Collection<DataTreeModification<Services>> changes = new HashSet<>();
70 @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
72 when(ch.getRootNode()).thenReturn(service);
74 when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
75 when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
76 ServiceListener listener = new ServiceListener(servicehandler, serviceDataStoreOperations,
77 notificationPublishService);
78 listener.onDataTreeChanged(changes);
79 verify(ch, times(1)).getRootNode();
80 verify(service, times(1)).getModificationType();
81 verify(service, times(2)).getDataBefore();
82 verify(service, never()).getDataAfter();
84 verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
85 } catch (InterruptedException e) {
86 fail("Failed publishing notification");
91 public void testOnDataTreeChangedWhenAddService() {
92 @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
93 mock(DataObjectModification.class);
94 final Collection<DataTreeModification<Services>> changes = new HashSet<>();
95 @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
97 when(ch.getRootNode()).thenReturn(service);
99 Services serviceDown = buildService(State.OutOfService, AdminStates.OutOfService);
100 when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
101 when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
102 when(service.getDataAfter()).thenReturn(serviceDown);
103 ServiceListener listener = new ServiceListener(servicehandler, serviceDataStoreOperations,
104 notificationPublishService);
105 listener.onDataTreeChanged(changes);
106 verify(ch, times(1)).getRootNode();
107 verify(service, times(1)).getModificationType();
108 verify(service, times(3)).getDataBefore();
109 verify(service, times(1)).getDataAfter();
110 PublishNotificationAlarmService publishNotificationAlarmService =
111 buildNotificationAlarmService(serviceDown, "The service is now outOfService");
113 verify(notificationPublishService, times(1))
114 .putNotification(publishNotificationAlarmService);
115 } catch (InterruptedException e) {
116 fail("Failed publishing notification");
121 public void testOnDataTreeChangedWhenShouldNeverHappen() {
122 @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
123 mock(DataObjectModification.class);
124 final Collection<DataTreeModification<Services>> changes = new HashSet<>();
125 @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
127 when(ch.getRootNode()).thenReturn(service);
129 when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
130 when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
131 ServiceListener listener = new ServiceListener(servicehandler, serviceDataStoreOperations,
132 notificationPublishService);
133 listener.onDataTreeChanged(changes);
134 verify(ch, times(1)).getRootNode();
135 verify(service, times(2)).getModificationType();
136 verify(service, times(2)).getDataBefore();
137 verify(service, never()).getDataAfter();
139 verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
140 } catch (InterruptedException e) {
141 fail("Failed publishing notification");
145 private Services buildService(State state, AdminStates adminStates) {
146 ServiceAEnd serviceAEnd = getServiceAEndBuild().build();
147 ServiceZEnd serviceZEnd = new ServiceZEndBuilder()
149 .setServiceFormat(ServiceFormat.OC)
150 .setServiceRate(Uint32.valueOf(1))
151 .setNodeId(new NodeIdType("XPONDER-3-2"))
152 .setTxDirection(Map.of(new TxDirectionKey(getTxDirection().key()), getTxDirection()))
153 .setRxDirection(Map.of(new RxDirectionKey(getRxDirection().key()), getRxDirection()))
155 ServicesBuilder builtInput = new ServicesBuilder()
156 .setCommonId("commonId")
157 .setConnectionType(ConnectionType.Service)
158 .setCustomer("Customer")
159 .setServiceName("service 1")
160 .setServiceAEnd(serviceAEnd)
161 .setServiceZEnd(serviceZEnd)
162 .setOperationalState(state)
163 .setAdministrativeState(adminStates);
165 return builtInput.build();
168 private ServiceAEndBuilder getServiceAEndBuild() {
169 return new ServiceAEndBuilder()
171 .setServiceFormat(ServiceFormat.OC)
172 .setServiceRate(Uint32.valueOf(1))
173 .setNodeId(new NodeIdType("XPONDER-1-2"))
174 .setTxDirection(Map.of(new TxDirectionKey(getTxDirection().key()), getTxDirection()))
175 .setRxDirection(Map.of(new RxDirectionKey(getRxDirection().key()), getRxDirection()));
178 private TxDirection getTxDirection() {
179 return new TxDirectionBuilder()
180 .setPort(new PortBuilder()
181 .setPortDeviceName("device name")
182 .setPortName("port name")
183 .setPortRack("port rack")
184 .setPortShelf("port shelf")
185 .setPortSlot("port slot")
186 .setPortSubSlot("port subslot")
187 .setPortType("port type")
189 .setLgx(new LgxBuilder()
190 .setLgxDeviceName("lgx device name")
191 .setLgxPortName("lgx port name")
192 .setLgxPortRack("lgx port rack")
193 .setLgxPortShelf("lgx port shelf")
195 .setIndex(Uint8.ZERO)
199 private RxDirection getRxDirection() {
200 return new RxDirectionBuilder()
201 .setPort(new PortBuilder()
202 .setPortDeviceName("device name")
203 .setPortName("port name")
204 .setPortRack("port rack")
205 .setPortShelf("port shelf")
206 .setPortSlot("port slot")
207 .setPortSubSlot("port subslot")
208 .setPortType("port type")
210 .setLgx(new LgxBuilder()
211 .setLgxDeviceName("lgx device name")
212 .setLgxPortName("lgx port name")
213 .setLgxPortRack("lgx port rack")
214 .setLgxPortShelf("lgx port shelf")
216 .setIndex(Uint8.ZERO)
220 private PublishNotificationAlarmService buildNotificationAlarmService(Services services, String message) {
221 return new PublishNotificationAlarmServiceBuilder()
222 .setServiceName("service 1")
223 .setConnectionType(ConnectionType.Service)
225 .setOperationalState(services.getOperationalState())
226 .setPublisherName("ServiceListener")