OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / DeviceInitializationUtilTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.util.concurrent.Futures;
19 import java.net.InetSocketAddress;
20 import java.util.Collections;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
30 import org.opendaylight.openflowplugin.api.OFConstants;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
33 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class DeviceInitializationUtilTest {
50     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II = DeviceStateUtil
51                 .createNodeInstanceIdentifier(new NodeId("openflow:1"));
52     private static final int PORT = 2017;
53     private static final InetSocketAddress INET_SOCKET_ADDRESS = new InetSocketAddress("192.168.0.1", PORT);
54     private static final short TABLES = 25;
55
56     @Mock
57     private DataBroker dataBroker;
58     @Mock
59     private WriteTransaction writeTransaction;
60     @Mock
61     private TxFacade txFacade;
62     @Mock
63     private DeviceInfo deviceInfo;
64     @Mock
65     private ConnectionAdapter connectionAdapter;
66     @Mock
67     private ConnectionContext connectionContext;
68     @Mock
69     private FeaturesReply featuresReply;
70
71     @Before
72     public void setUp() throws Exception {
73         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
74         when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
75         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
76         when(connectionAdapter.getRemoteAddress()).thenReturn(INET_SOCKET_ADDRESS);
77         when(featuresReply.getTables()).thenReturn(TABLES);
78         when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
79         when(featuresReply.getCapabilities()).thenReturn(new Capabilities(false, false,
80                 false, false, false, false, false));
81         when(connectionContext.getFeatures()).thenReturn(featuresReply);
82         when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
83     }
84
85     @Test
86     public void makeEmptyNodes() throws Exception {
87         DeviceInitializationUtil.makeEmptyNodes(dataBroker);
88         verify(dataBroker).newWriteOnlyTransaction();
89         verify(writeTransaction).merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier
90                 .create(Nodes.class), new NodesBuilder()
91                 .setNode(Collections.emptyList())
92                 .build());
93         verify(writeTransaction).submit();
94     }
95
96     @Test
97     public void makeEmptyTables() throws Exception {
98         DeviceInitializationUtil.makeEmptyTables(txFacade, deviceInfo, (short) 10);
99         verify(txFacade, times(10)).writeToTransaction(
100                 eq(LogicalDatastoreType.OPERATIONAL), any(), any());
101     }
102
103     @Test
104     public void getIpAddress() throws Exception {
105         final IpAddress ipAddress = DeviceInitializationUtil.getIpAddress(connectionContext, NODE_II);
106         assertEquals(ipAddress, IetfInetUtil.INSTANCE.ipAddressFor(INET_SOCKET_ADDRESS.getAddress()));
107     }
108
109     @Test
110     public void getPortNumber() throws Exception {
111         final PortNumber portNumber = DeviceInitializationUtil.getPortNumber(connectionContext, NODE_II);
112         assertEquals(portNumber, new PortNumber(PORT));
113     }
114
115     @Test
116     public void getSwitchFeatures() throws Exception {
117         final SwitchFeatures switchFeatures = DeviceInitializationUtil.getSwitchFeatures(connectionContext);
118         assertEquals(TABLES, switchFeatures.getMaxTables().shortValue());
119     }
120
121 }