Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.provider.netcfghost; |
| 18 | |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 22 | import org.onlab.packet.EthType; |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.HostId; |
| 28 | import org.onosproject.net.HostLocation; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.host.DefaultHostDescription; |
| 31 | import org.onosproject.net.host.HostDescription; |
| 32 | import org.onosproject.net.host.HostProvider; |
| 33 | import org.onosproject.net.host.HostProviderService; |
| 34 | import org.onosproject.net.provider.AbstractProviderService; |
| 35 | |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.junit.Assert.assertNull; |
| 41 | import static org.junit.Assert.assertThat; |
| 42 | |
| 43 | /** |
| 44 | * Set of tests of the host location provider for CORD. |
| 45 | */ |
| 46 | public class NetworkConfigHostProviderTest { |
| 47 | private NetworkConfigHostProvider provider = new NetworkConfigHostProvider(); |
| 48 | private MockHostProviderService providerService = new MockHostProviderService(provider); |
| 49 | |
| 50 | private MacAddress mac = MacAddress.valueOf("c0:ff:ee:c0:ff:ee"); |
| 51 | private VlanId vlan = VlanId.vlanId(VlanId.UNTAGGED); |
| 52 | private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001"); |
| 53 | private PortNumber port = PortNumber.portNumber(5); |
Charles Chan | 61fc0d8 | 2017-04-28 14:13:36 -0700 | [diff] [blame] | 54 | private Set<HostLocation> locations = Sets.newHashSet(new HostLocation(deviceId, port, 100)); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame^] | 55 | private DeviceId auxDeviceId = DeviceId.deviceId("of:0000000000000002"); |
| 56 | private PortNumber auxPort = PortNumber.portNumber(7); |
| 57 | private Set<HostLocation> auxLocations = Sets.newHashSet(new HostLocation(auxDeviceId, auxPort, 100)); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 58 | private Set<IpAddress> ips = new HashSet<>(); |
| 59 | private HostId hostId = HostId.hostId(mac, vlan); |
| 60 | private HostDescription hostDescription; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 61 | private VlanId innerVlan = VlanId.vlanId((short) 20); |
| 62 | private EthType outerTpid = EthType.EtherType.lookup((short) 0x88a8).ethType(); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 63 | |
| 64 | @Before |
| 65 | public void setUp() { |
| 66 | provider.providerService = providerService; |
| 67 | |
| 68 | // Initialize test variables |
| 69 | ips.add(IpAddress.valueOf("10.0.0.1")); |
| 70 | ips.add(IpAddress.valueOf("192.168.0.1")); |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame^] | 71 | hostDescription = new DefaultHostDescription(mac, vlan, locations, auxLocations, ips, |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 72 | innerVlan, outerTpid, true); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testAddHost() throws Exception { |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame^] | 77 | provider.addHost(mac, vlan, locations, auxLocations, ips, innerVlan, outerTpid); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 78 | assertThat(providerService.hostId, is(hostId)); |
| 79 | assertThat(providerService.hostDescription, is(hostDescription)); |
| 80 | assertThat(providerService.event, is("hostDetected")); |
| 81 | providerService.clear(); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testUpdateHost() throws Exception { |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame^] | 86 | provider.updateHost(mac, vlan, locations, auxLocations, ips, innerVlan, outerTpid); |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 87 | assertThat(providerService.hostId, is(hostId)); |
| 88 | assertThat(providerService.hostDescription, is(hostDescription)); |
| 89 | assertThat(providerService.event, is("hostDetected")); |
| 90 | providerService.clear(); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testRemoveHost() throws Exception { |
| 95 | provider.removeHost(mac, vlan); |
| 96 | assertThat(providerService.hostId, is(hostId)); |
| 97 | assertNull(providerService.hostDescription); |
| 98 | assertThat(providerService.event, is("hostVanished")); |
| 99 | providerService.clear(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Mock HostProviderService. |
| 104 | */ |
| 105 | private class MockHostProviderService |
| 106 | extends AbstractProviderService<HostProvider> |
| 107 | implements HostProviderService { |
| 108 | private HostId hostId = null; |
| 109 | private HostDescription hostDescription = null; |
| 110 | private String event = null; |
| 111 | |
| 112 | public MockHostProviderService(HostProvider provider) { |
| 113 | super(provider); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) { |
| 118 | this.hostId = hostId; |
| 119 | this.hostDescription = hostDescription; |
| 120 | this.event = "hostDetected"; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void hostVanished(HostId hostId) { |
| 125 | this.hostId = hostId; |
| 126 | this.event = "hostVanished"; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
Charles Chan | 888e20a | 2017-05-01 15:44:23 -0700 | [diff] [blame] | 131 | |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 136 | |
Charles Chan | d6d581a | 2015-11-18 16:51:08 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public void clear() { |
| 140 | this.hostId = null; |
| 141 | this.hostDescription = null; |
| 142 | this.event = null; |
| 143 | } |
| 144 | } |
| 145 | } |