Unit tests for ofoverlay
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / endpoint / OfOverlayContextListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.groupbasedpolicy.renderer.ofoverlay.endpoint;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.spy;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.util.Set;
20
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46
47 public class OfOverlayContextListenerTest {
48
49     private static final Name OLD_PORT_NAME = new Name("oldPort");
50     private static final Name NEW_PORT_NAME = new Name("newPort");
51     private OfOverlayContextListener listener;
52     private DataObjectModification<OfOverlayContext> rootNode;
53     private Set<DataTreeModification<OfOverlayContext>> changes;
54
55     private DataBroker dataProvider;
56     private SwitchManager switchManager;
57
58     private InstanceIdentifier<OfOverlayContext> rootIdentifier;
59     private OfOverlayContext oldContext;
60     private OfOverlayContext newContext;
61     private OfOverlayContext contextNoPortName;
62
63     @SuppressWarnings("unchecked")
64     @Before
65     public void init() {
66
67         dataProvider = mock(DataBroker.class);
68         switchManager = mock(SwitchManager.class);
69
70         NodeKey nodeKey = new NodeKey(new NodeId("nodeId"));
71         NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(new NodeConnectorId("ncId"));
72         InstanceIdentifier<NodeConnector> ncIid = InstanceIdentifier.builder(Nodes.class)
73             .child(Node.class, nodeKey)
74             .child(NodeConnector.class, nodeConnectorKey)
75             .build();
76         when(switchManager.getNodeConnectorIidForPortName(NEW_PORT_NAME)).thenReturn(ncIid);
77
78         listener = spy(new OfOverlayContextListener(dataProvider, switchManager));
79         EndpointKey epKey = mock(EndpointKey.class);
80
81         rootNode = mock(DataObjectModification.class);
82         rootIdentifier = InstanceIdentifier.builder(Endpoints.class)
83             .child(Endpoint.class, epKey)
84             .augmentation(OfOverlayContext.class)
85             .build();
86         DataTreeIdentifier<OfOverlayContext> rootPath =
87                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, rootIdentifier);
88
89         DataTreeModification<OfOverlayContext> change = mock(DataTreeModification.class);
90
91         when(change.getRootNode()).thenReturn(rootNode);
92         when(change.getRootPath()).thenReturn(rootPath);
93
94         changes = ImmutableSet.of(change);
95
96         oldContext = new OfOverlayContextBuilder().setPortName(OLD_PORT_NAME).build();
97         newContext = new OfOverlayContextBuilder().setPortName(NEW_PORT_NAME).build();
98         contextNoPortName = new OfOverlayContextBuilder().build();
99     }
100
101     @Test
102     public void testOnDataTreeChanged_Write() {
103         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
104         when(rootNode.getDataAfter()).thenReturn(newContext);
105
106         WriteTransaction wt = resetTransaction();
107
108         listener.onDataTreeChanged(changes);
109
110         verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
111                 any(OfOverlayContext.class));
112     }
113
114     @Test
115     public void testOnDataTreeChanged_SubtreeModified() {
116         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
117         when(rootNode.getDataBefore()).thenReturn(oldContext);
118         when(rootNode.getDataAfter()).thenReturn(newContext);
119
120         WriteTransaction wt = resetTransaction();
121
122         listener.onDataTreeChanged(changes);
123
124         verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
125                 any(OfOverlayContext.class));
126     }
127
128     @Test
129     public void testOnDataTreeChanged_SubtreeModified_bothNoPortName() {
130         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
131         when(rootNode.getDataBefore()).thenReturn(contextNoPortName);
132         when(rootNode.getDataAfter()).thenReturn(contextNoPortName);
133
134         WriteTransaction wt = resetTransaction();
135
136         listener.onDataTreeChanged(changes);
137
138         verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
139                 any(OfOverlayContext.class));
140     }
141
142     @Test
143     public void testOnDataTreeChanged_SubtreeModified_oneNoPortName() {
144         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
145         when(rootNode.getDataBefore()).thenReturn(oldContext);
146         when(rootNode.getDataAfter()).thenReturn(contextNoPortName);
147
148         WriteTransaction wt = resetTransaction();
149
150         listener.onDataTreeChanged(changes);
151
152         verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
153                 any(OfOverlayContext.class));
154     }
155
156     @Test
157     public void testOnDataTreeChanged_SubtreeModified_oneNoPortName1() {
158         when(switchManager.getNodeConnectorIidForPortName(NEW_PORT_NAME)).thenReturn(null);
159         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
160         when(rootNode.getDataBefore()).thenReturn(oldContext);
161         when(rootNode.getDataAfter()).thenReturn(newContext);
162
163         WriteTransaction wt = resetTransaction();
164
165         listener.onDataTreeChanged(changes);
166
167         verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
168                 any(OfOverlayContext.class));
169     }
170
171     @Test
172     public void testOnDataTreeChanged_SubtreeModified_samePortName() {
173         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
174         when(rootNode.getDataBefore()).thenReturn(newContext);
175         when(rootNode.getDataAfter()).thenReturn(newContext);
176
177         WriteTransaction wt = resetTransaction();
178
179         listener.onDataTreeChanged(changes);
180
181         verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
182                 any(OfOverlayContext.class));
183     }
184
185     @Test
186     public void testOnDataTreeChanged_Delete() {
187         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
188
189         WriteTransaction wt = resetTransaction();
190
191         listener.onDataTreeChanged(changes);
192
193         // no op
194     }
195
196     private WriteTransaction resetTransaction() {
197         WriteTransaction wt = mock(WriteTransaction.class);
198         CheckedFuture checkedFuture = mock(CheckedFuture.class);
199         when(wt.submit()).thenReturn(checkedFuture);
200         when(dataProvider.newWriteOnlyTransaction()).thenReturn(wt);
201         return wt;
202     }
203
204 }