Update docs conf.yaml version to Sulfur
[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 java.util.Map;
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;
54
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class ServiceListenerTest {
57
58     @Mock
59     private ServicehandlerImpl servicehandler;
60     @Mock
61     private ServiceDataStoreOperations serviceDataStoreOperations;
62     @Mock
63     private NotificationPublishService notificationPublishService;
64
65     @Test
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);
71         changes.add(ch);
72         when(ch.getRootNode()).thenReturn(service);
73
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();
83         try {
84             verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
85         } catch (InterruptedException e) {
86             fail("Failed publishing notification");
87         }
88     }
89
90     @Test
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);
96         changes.add(ch);
97         when(ch.getRootNode()).thenReturn(service);
98
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");
112         try {
113             verify(notificationPublishService, times(1))
114                     .putNotification(publishNotificationAlarmService);
115         } catch (InterruptedException e) {
116             fail("Failed publishing notification");
117         }
118     }
119
120     @Test
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);
126         changes.add(ch);
127         when(ch.getRootNode()).thenReturn(service);
128
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();
138         try {
139             verify(notificationPublishService, never()).putNotification(any(PublishNotificationAlarmService.class));
140         } catch (InterruptedException e) {
141             fail("Failed publishing notification");
142         }
143     }
144
145     private Services buildService(State state, AdminStates adminStates) {
146         ServiceAEnd serviceAEnd = getServiceAEndBuild().build();
147         ServiceZEnd serviceZEnd = new ServiceZEndBuilder()
148                 .setClli("clli")
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()))
154                 .build();
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);
164
165         return builtInput.build();
166     }
167
168     private ServiceAEndBuilder getServiceAEndBuild() {
169         return new ServiceAEndBuilder()
170                 .setClli("clli")
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()));
176     }
177
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")
188                         .build())
189                 .setLgx(new LgxBuilder()
190                         .setLgxDeviceName("lgx device name")
191                         .setLgxPortName("lgx port name")
192                         .setLgxPortRack("lgx port rack")
193                         .setLgxPortShelf("lgx port shelf")
194                         .build())
195                 .setIndex(Uint8.ZERO)
196                 .build();
197     }
198
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")
209                         .build())
210                 .setLgx(new LgxBuilder()
211                         .setLgxDeviceName("lgx device name")
212                         .setLgxPortName("lgx port name")
213                         .setLgxPortRack("lgx port rack")
214                         .setLgxPortShelf("lgx port shelf")
215                         .build())
216                 .setIndex(Uint8.ZERO)
217                 .build();
218     }
219
220     private PublishNotificationAlarmService buildNotificationAlarmService(Services services, String message) {
221         return new PublishNotificationAlarmServiceBuilder()
222                 .setServiceName("service 1")
223                 .setConnectionType(ConnectionType.Service)
224                 .setMessage(message)
225                 .setOperationalState(services.getOperationalState())
226                 .setPublisherName("ServiceListener")
227                 .build();
228     }
229 }