2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint;
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;
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;
48 public class OfOverlayL3ContextListenerTest {
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;
56 private DataBroker dataProvider;
57 private SwitchManager switchManager;
59 private InstanceIdentifier<OfOverlayL3Context> rootIdentifier;
60 private OfOverlayL3Context oldContext;
61 private OfOverlayL3Context newContext;
62 private OfOverlayL3Context contextNoPortName;
64 @SuppressWarnings("unchecked")
68 dataProvider = mock(DataBroker.class);
69 switchManager = mock(SwitchManager.class);
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)
77 when(switchManager.getNodeConnectorIidForPortName(NEW_PORT_NAME)).thenReturn(ncIid);
79 listener = spy(new OfOverlayL3ContextListener(dataProvider, switchManager));
80 EndpointL3Key epL3Key = mock(EndpointL3Key.class);
82 rootNode = mock(DataObjectModification.class);
83 rootIdentifier = InstanceIdentifier.builder(Endpoints.class)
84 .child(EndpointL3.class, epL3Key)
85 .augmentation(OfOverlayL3Context.class)
87 DataTreeIdentifier<OfOverlayL3Context> rootPath =
88 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, rootIdentifier);
90 DataTreeModification<OfOverlayL3Context> change = mock(DataTreeModification.class);
92 when(change.getRootNode()).thenReturn(rootNode);
93 when(change.getRootPath()).thenReturn(rootPath);
95 changes = ImmutableSet.of(change);
97 oldContext = new OfOverlayL3ContextBuilder().setPortName(OLD_PORT_NAME).build();
98 newContext = new OfOverlayL3ContextBuilder().setPortName(NEW_PORT_NAME).build();
99 contextNoPortName = new OfOverlayL3ContextBuilder().build();
103 public void testOnDataTreeChanged_Write() {
104 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
105 when(rootNode.getDataAfter()).thenReturn(newContext);
107 WriteTransaction wt = resetTransaction();
109 listener.onDataTreeChanged(changes);
111 verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
112 any(OfOverlayContext.class));
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);
121 WriteTransaction wt = resetTransaction();
123 listener.onDataTreeChanged(changes);
125 verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
126 any(OfOverlayContext.class));
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);
135 WriteTransaction wt = resetTransaction();
137 listener.onDataTreeChanged(changes);
139 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
140 any(OfOverlayContext.class));
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);
149 WriteTransaction wt = resetTransaction();
151 listener.onDataTreeChanged(changes);
153 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
154 any(OfOverlayContext.class));
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);
164 WriteTransaction wt = resetTransaction();
166 listener.onDataTreeChanged(changes);
168 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
169 any(OfOverlayContext.class));
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);
178 WriteTransaction wt = resetTransaction();
180 listener.onDataTreeChanged(changes);
182 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
183 any(OfOverlayContext.class));
187 public void testOnDataTreeChanged_Delete() {
188 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
190 WriteTransaction wt = resetTransaction();
192 listener.onDataTreeChanged(changes);
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);