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.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;
47 public class OfOverlayContextListenerTest {
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;
55 private DataBroker dataProvider;
56 private SwitchManager switchManager;
58 private InstanceIdentifier<OfOverlayContext> rootIdentifier;
59 private OfOverlayContext oldContext;
60 private OfOverlayContext newContext;
61 private OfOverlayContext contextNoPortName;
63 @SuppressWarnings("unchecked")
67 dataProvider = mock(DataBroker.class);
68 switchManager = mock(SwitchManager.class);
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)
76 when(switchManager.getNodeConnectorIidForPortName(NEW_PORT_NAME)).thenReturn(ncIid);
78 listener = spy(new OfOverlayContextListener(dataProvider, switchManager));
79 EndpointKey epKey = mock(EndpointKey.class);
81 rootNode = mock(DataObjectModification.class);
82 rootIdentifier = InstanceIdentifier.builder(Endpoints.class)
83 .child(Endpoint.class, epKey)
84 .augmentation(OfOverlayContext.class)
86 DataTreeIdentifier<OfOverlayContext> rootPath =
87 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, rootIdentifier);
89 DataTreeModification<OfOverlayContext> change = mock(DataTreeModification.class);
91 when(change.getRootNode()).thenReturn(rootNode);
92 when(change.getRootPath()).thenReturn(rootPath);
94 changes = ImmutableSet.of(change);
96 oldContext = new OfOverlayContextBuilder().setPortName(OLD_PORT_NAME).build();
97 newContext = new OfOverlayContextBuilder().setPortName(NEW_PORT_NAME).build();
98 contextNoPortName = new OfOverlayContextBuilder().build();
102 public void testOnDataTreeChanged_Write() {
103 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
104 when(rootNode.getDataAfter()).thenReturn(newContext);
106 WriteTransaction wt = resetTransaction();
108 listener.onDataTreeChanged(changes);
110 verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
111 any(OfOverlayContext.class));
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);
120 WriteTransaction wt = resetTransaction();
122 listener.onDataTreeChanged(changes);
124 verify(wt).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
125 any(OfOverlayContext.class));
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);
134 WriteTransaction wt = resetTransaction();
136 listener.onDataTreeChanged(changes);
138 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
139 any(OfOverlayContext.class));
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);
148 WriteTransaction wt = resetTransaction();
150 listener.onDataTreeChanged(changes);
152 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
153 any(OfOverlayContext.class));
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);
163 WriteTransaction wt = resetTransaction();
165 listener.onDataTreeChanged(changes);
167 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
168 any(OfOverlayContext.class));
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);
177 WriteTransaction wt = resetTransaction();
179 listener.onDataTreeChanged(changes);
181 verify(wt, never()).merge(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
182 any(OfOverlayContext.class));
186 public void testOnDataTreeChanged_Delete() {
187 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
189 WriteTransaction wt = resetTransaction();
191 listener.onDataTreeChanged(changes);
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);