2 * Copyright (c) 2015 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.node;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
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.DataTreeChangeListener;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
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.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 public class FlowCapableNodeConnectorListenerTest {
51 private FlowCapableNodeConnectorListener listener;
53 private DataBroker dataProvider;
54 private SwitchManager switchManager;
55 private ListenerRegistration<?> listenerRegistration;
56 private FlowCapableNodeConnector entryValue;
57 private String portName;
58 private NodeId childNodeId;
59 private NodeConnectorId childNodeConnectorId;
60 private InstanceIdentifier<FlowCapableNodeConnector> entryKey;
61 private ReadWriteTransaction rwTx;
62 private CheckedFuture<Optional<Endpoints>, ReadFailedException> checkedFuture;
63 private Optional<Endpoints> potentialEps;
64 private OfOverlayContext ofOverlayEp;
65 private EndpointKey endpointKey;
67 private DataObjectModification<FlowCapableNodeConnector> mockModification;
68 private Collection<DataTreeModification<FlowCapableNodeConnector>> changeEvent;
70 @SuppressWarnings("unchecked")
72 public void initialisation() throws Exception {
73 dataProvider = mock(DataBroker.class);
74 switchManager = mock(SwitchManager.class);
75 listenerRegistration = mock(ListenerRegistration.class);
76 when(dataProvider.registerDataTreeChangeListener(any(DataTreeIdentifier.class),
77 any(DataTreeChangeListener.class))).thenReturn(listenerRegistration);
78 entryValue = mock(FlowCapableNodeConnector.class);
79 portName = "portName";
80 when(entryValue.getName()).thenReturn(portName);
81 childNodeId = mock(NodeId.class);
82 childNodeConnectorId = mock(NodeConnectorId.class);
83 entryKey = InstanceIdentifier.builder(Nodes.class)
84 .child(Node.class, new NodeKey(childNodeId))
85 .child(NodeConnector.class, new NodeConnectorKey(childNodeConnectorId))
86 .augmentation(FlowCapableNodeConnector.class)
88 rwTx = mock(ReadWriteTransaction.class);
89 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
90 checkedFuture = mock(CheckedFuture.class);
91 when(rwTx.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture);
92 potentialEps = mock(Optional.class);
93 when(checkedFuture.get()).thenReturn(potentialEps);
95 when(potentialEps.isPresent()).thenReturn(true);
96 Endpoints endpoints = mock(Endpoints.class);
97 when(potentialEps.get()).thenReturn(endpoints);
98 Endpoint ep = mock(Endpoint.class);
99 List<Endpoint> endpointArray = Arrays.asList(ep);
100 when(endpoints.getEndpoint()).thenReturn(endpointArray);
101 endpointKey = mock(EndpointKey.class);
102 when(ep.getKey()).thenReturn(endpointKey);
104 ofOverlayEp = mock(OfOverlayContext.class);
105 when(ep.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayEp);
106 name = new Name(portName);
107 when(ofOverlayEp.getPortName()).thenReturn(name);
109 listener = new FlowCapableNodeConnectorListener(dataProvider, switchManager);
111 DataTreeModification<FlowCapableNodeConnector> mockDataTreeModification = mock(DataTreeModification.class);
112 mockModification = mock(DataObjectModification.class);
113 doReturn(mockModification).when(mockDataTreeModification).getRootNode();
114 doReturn(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, entryKey)).when(mockDataTreeModification)
116 changeEvent = Collections.singletonList(mockDataTreeModification);
120 public void constructorTest() throws Exception {
122 verify(listenerRegistration).close();
126 public void onDataChangeTestCreatedDataSubmit() {
127 doReturn(DataObjectModification.ModificationType.WRITE).when(mockModification).getModificationType();
128 doReturn(entryValue).when(mockModification).getDataAfter();
130 listener.onDataTreeChanged(changeEvent);
131 verify(rwTx).submit();
135 public void onDataChangeTestUpdatedDataSubmit() {
136 doReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED).when(mockModification).getModificationType();
137 doReturn(entryValue).when(mockModification).getDataBefore();
138 doReturn(entryValue).when(mockModification).getDataAfter();
140 listener.onDataTreeChanged(changeEvent);
141 verify(rwTx).submit();
145 public void onDataChangeTestRemovedPaths() {
146 doReturn(DataObjectModification.ModificationType.DELETE).when(mockModification).getModificationType();
147 doReturn(entryValue).when(mockModification).getDataBefore();
148 when(ofOverlayEp.getNodeConnectorId()).thenReturn(childNodeConnectorId);
150 listener.onDataTreeChanged(changeEvent);
151 verify(rwTx).submit();