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