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