8b5cb0975102e8096cac989c8b3862a761d23b9a
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / RoutingServiceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt.providers.openflow13.services;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import java.net.InetAddress;
20
21 import org.junit.Before;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
34 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
35 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
36 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
37 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 import com.google.common.util.concurrent.CheckedFuture;
42
43 /**
44  * Unit test fort {@link RoutingService}
45  */
46 @Ignore // TODO SB_MIGRATION
47 @RunWith(MockitoJUnitRunner.class)
48 public class RoutingServiceTest {
49
50     @InjectMocks private RoutingService routingService = new RoutingService();
51
52     //@Mock private MdsalConsumer mdsalConsumer;
53     @Mock private PipelineOrchestrator orchestrator;
54
55     @Mock private ReadWriteTransaction readWriteTransaction;
56     @Mock private WriteTransaction writeTransaction;
57     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
58
59     private static final String SEGMENTATION_ID = "2";
60     private static final String HOST_ADDRESS = "127.0.0.1";
61     private static final String MAC_ADDRESS = "87:1D:5E:02:40:B8";
62
63     @Before
64     public void setUp() throws Exception {
65         when(readWriteTransaction.submit()).thenReturn(commitFuture);
66         when(writeTransaction.submit()).thenReturn(commitFuture);
67
68         DataBroker dataBroker = mock(DataBroker.class);
69         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
70         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
71
72         //when(mdsalConsumer.getDataBroker()).thenReturn(dataBroker);
73
74         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
75     }
76
77     /**
78      * Test method {@link RoutingService#programRouterInterface(Long, String, String, String, InetAddress, int, Action)}
79      */
80     @Test
81     public void testProgramRouterInterface() throws Exception {
82         InetAddress address = mock(InetAddress.class);
83         when(address.getHostAddress()).thenReturn(HOST_ADDRESS);
84
85         assertEquals("Error, did not return the expected StatusCode",
86                 new Status(StatusCode.SUCCESS),
87                 routingService.programRouterInterface(Long.valueOf(123),
88                         SEGMENTATION_ID, SEGMENTATION_ID, MAC_ADDRESS, address, 1, Action.ADD));
89         verify(readWriteTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
90         verify(readWriteTransaction, times(1)).submit();
91         verify(commitFuture, times(1)).get();
92
93         assertEquals("Error, did not return the expected StatusCode",
94                 new Status(StatusCode.SUCCESS),
95                 routingService.programRouterInterface(Long.valueOf(123),
96                         SEGMENTATION_ID, SEGMENTATION_ID, MAC_ADDRESS, address, 1, Action.DELETE));
97         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
98         verify(readWriteTransaction, times(1)).submit();
99         verify(commitFuture, times(2)).get(); // 1 + 1 above
100     }
101
102     /**
103      * Test method {@link RoutingService#programDefaultRouteEntry(Long, String, String, InetAddress, Action)}
104      */
105     @Test
106     public void testProgramDefaultRouteEntry() throws Exception {
107         InetAddress address = mock(InetAddress.class);
108         when(address.getHostAddress()).thenReturn(HOST_ADDRESS);
109
110         assertEquals("Error, did not return the expected StatusCode",
111                 new Status(StatusCode.SUCCESS),
112                 routingService.programDefaultRouteEntry(Long.valueOf(123),
113                         SEGMENTATION_ID, MAC_ADDRESS, address, Action.ADD));
114         verify(readWriteTransaction, times(2)).put(any(LogicalDatastoreType.class),
115                 any(InstanceIdentifier.class), any(Node.class), anyBoolean());
116         verify(readWriteTransaction, times(1)).submit();
117         verify(commitFuture, times(1)).get();
118
119         assertEquals("Error, did not return the expected StatusCode",
120                 new Status(StatusCode.SUCCESS),
121                 routingService.programDefaultRouteEntry(Long.valueOf(123),
122                         SEGMENTATION_ID, MAC_ADDRESS, address, Action.DELETE));
123         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
124         verify(readWriteTransaction, times(1)).submit();
125         verify(commitFuture, times(2)).get(); // 1 + 1 above
126     }
127 }