Refactor NBINotifications and serviceHandlerImpl
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / listeners / ServiceListenerTest.java
1 /*
2  * Copyright © 2021 Orange.  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
9 package org.opendaylight.transportpce.servicehandler.listeners;
10
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;
18
19 import java.util.Collection;
20 import java.util.HashSet;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
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.yang.gen.v1.http.org.openroadm.common.node.types.rev181130.NodeIdType;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEnd;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEndBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceZEnd;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceZEndBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirection;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirectionBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirection;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirectionBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.lgx.LgxBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.port.PortBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.ServicesBuilder;
46 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmService;
47 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.PublishNotificationAlarmServiceBuilder;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49
50 @RunWith(MockitoJUnitRunner.StrictStubs.class)
51 public class ServiceListenerTest {
52
53     @Mock
54     private DataBroker dataBroker;
55     @Mock
56     private NotificationPublishService notificationPublishService;
57
58     @Test
59     public void testOnDataTreeChangedWhenDeleteService() {
60         @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
61                 mock(DataObjectModification.class);
62         final Collection<DataTreeModification<Services>> changes = new HashSet<>();
63         @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
64         changes.add(ch);
65         when(ch.getRootNode()).thenReturn(service);
66
67         when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
68         when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
69         ServiceListener listener = new ServiceListener(dataBroker, notificationPublishService);
70         listener.onDataTreeChanged(changes);
71         verify(ch, times(1)).getRootNode();
72         verify(service, times(1)).getModificationType();
73         verify(service, times(2)).getDataBefore();
74         verify(service, never()).getDataAfter();
75         try {
76             verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
77         } catch (InterruptedException e) {
78             fail("Failed publishing notification");
79         }
80     }
81
82     @Test
83     public void testOnDataTreeChangedWhenAddService() {
84         @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
85                 mock(DataObjectModification.class);
86         final Collection<DataTreeModification<Services>> changes = new HashSet<>();
87         @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
88         changes.add(ch);
89         when(ch.getRootNode()).thenReturn(service);
90
91         Services serviceDown = buildService(State.OutOfService, AdminStates.OutOfService);
92         when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
93         when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
94         when(service.getDataAfter()).thenReturn(serviceDown);
95         ServiceListener listener = new ServiceListener(dataBroker, notificationPublishService);
96         listener.onDataTreeChanged(changes);
97         verify(ch, times(1)).getRootNode();
98         verify(service, times(1)).getModificationType();
99         verify(service, times(3)).getDataBefore();
100         verify(service, times(2)).getDataAfter();
101         PublishNotificationAlarmService publishNotificationAlarmService =
102                 buildNotificationAlarmService(serviceDown, "The service is now outOfService");
103         try {
104             verify(notificationPublishService, times(1))
105                     .putNotification(publishNotificationAlarmService);
106         } catch (InterruptedException e) {
107             fail("Failed publishing notification");
108         }
109     }
110
111     @Test
112     public void testOnDataTreeChangedWhenShouldNeverHappen() {
113         @SuppressWarnings("unchecked") final DataObjectModification<Services> service =
114                 mock(DataObjectModification.class);
115         final Collection<DataTreeModification<Services>> changes = new HashSet<>();
116         @SuppressWarnings("unchecked") final DataTreeModification<Services> ch = mock(DataTreeModification.class);
117         changes.add(ch);
118         when(ch.getRootNode()).thenReturn(service);
119
120         when(service.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
121         when(service.getDataBefore()).thenReturn(buildService(State.InService, AdminStates.InService));
122         ServiceListener listener = new ServiceListener(dataBroker, notificationPublishService);
123         listener.onDataTreeChanged(changes);
124         verify(ch, times(1)).getRootNode();
125         verify(service, times(2)).getModificationType();
126         verify(service, times(2)).getDataBefore();
127         verify(service, never()).getDataAfter();
128         try {
129             verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
130         } catch (InterruptedException e) {
131             fail("Failed publishing notification");
132         }
133     }
134
135     private Services buildService(State state, AdminStates adminStates) {
136         ServiceAEnd serviceAEnd = getServiceAEndBuild().build();
137         ServiceZEnd serviceZEnd = new ServiceZEndBuilder()
138                     .setClli("clli")
139                     .setServiceFormat(ServiceFormat.OC)
140                     .setServiceRate(Uint32.valueOf(1))
141                     .setNodeId(new NodeIdType("XPONDER-3-2"))
142                     .setTxDirection(getTxDirection())
143                     .setRxDirection(getRxDirection())
144                     .build();
145         ServicesBuilder builtInput = new ServicesBuilder()
146                 .setCommonId("commonId")
147                 .setConnectionType(ConnectionType.Service)
148                 .setCustomer("Customer")
149                 .setServiceName("service 1")
150                 .setServiceAEnd(serviceAEnd)
151                 .setServiceZEnd(serviceZEnd)
152                 .setOperationalState(state)
153                 .setAdministrativeState(adminStates);
154
155         return builtInput.build();
156     }
157
158     private ServiceAEndBuilder getServiceAEndBuild() {
159         return new ServiceAEndBuilder()
160                 .setClli("clli")
161                 .setServiceFormat(ServiceFormat.OC)
162                 .setServiceRate(Uint32.valueOf(1))
163                 .setNodeId(new NodeIdType("XPONDER-1-2"))
164                 .setTxDirection(getTxDirection())
165                 .setRxDirection(getRxDirection());
166     }
167
168     private TxDirection getTxDirection() {
169         return new TxDirectionBuilder()
170                 .setPort(new PortBuilder()
171                         .setPortDeviceName("device name")
172                         .setPortName("port name")
173                         .setPortRack("port rack")
174                         .setPortShelf("port shelf")
175                         .setPortSlot("port slot")
176                         .setPortSubSlot("port subslot")
177                         .setPortType("port type")
178                         .build())
179                 .setLgx(new LgxBuilder()
180                         .setLgxDeviceName("lgx device name")
181                         .setLgxPortName("lgx port name")
182                         .setLgxPortRack("lgx port rack")
183                         .setLgxPortShelf("lgx port shelf")
184                         .build())
185                 .build();
186     }
187
188     private RxDirection getRxDirection() {
189         return new RxDirectionBuilder()
190                 .setPort(new PortBuilder()
191                         .setPortDeviceName("device name")
192                         .setPortName("port name")
193                         .setPortRack("port rack")
194                         .setPortShelf("port shelf")
195                         .setPortSlot("port slot")
196                         .setPortSubSlot("port subslot")
197                         .setPortType("port type")
198                         .build())
199                 .setLgx(new LgxBuilder()
200                         .setLgxDeviceName("lgx device name")
201                         .setLgxPortName("lgx port name")
202                         .setLgxPortRack("lgx port rack")
203                         .setLgxPortShelf("lgx port shelf")
204                         .build())
205                 .build();
206     }
207
208     private PublishNotificationAlarmService buildNotificationAlarmService(Services services, String message) {
209         return new PublishNotificationAlarmServiceBuilder()
210                 .setServiceName("service 1")
211                 .setConnectionType(ConnectionType.Service)
212                 .setMessage(message)
213                 .setOperationalState(services.getOperationalState())
214                 .setPublisherName("ServiceListener")
215                 .build();
216     }
217 }