blob: 4bba5244e4864c8f924c1b3ae46d2e5a16227f42 [file] [log] [blame]
Charles Chand6d581a2015-11-18 16:51:08 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Charles Chand6d581a2015-11-18 16:51:08 -08003 *
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
17package org.onosproject.provider.netcfghost;
18
Charles Chan61fc0d82017-04-28 14:13:36 -070019import com.google.common.collect.Sets;
Charles Chand6d581a2015-11-18 16:51:08 -080020import org.junit.Before;
21import org.junit.Test;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070022import org.onlab.packet.EthType;
Charles Chand6d581a2015-11-18 16:51:08 -080023import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.host.DefaultHostDescription;
31import org.onosproject.net.host.HostDescription;
32import org.onosproject.net.host.HostProvider;
33import org.onosproject.net.host.HostProviderService;
34import org.onosproject.net.provider.AbstractProviderService;
35
36import java.util.HashSet;
37import java.util.Set;
38
39import static org.hamcrest.Matchers.is;
40import static org.junit.Assert.assertNull;
41import static org.junit.Assert.assertThat;
42
43/**
44 * Set of tests of the host location provider for CORD.
45 */
46public 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 Chan61fc0d82017-04-28 14:13:36 -070054 private Set<HostLocation> locations = Sets.newHashSet(new HostLocation(deviceId, port, 100));
Charles Chaneb5bd492019-12-18 15:49:39 -080055 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 Chand6d581a2015-11-18 16:51:08 -080058 private Set<IpAddress> ips = new HashSet<>();
59 private HostId hostId = HostId.hostId(mac, vlan);
60 private HostDescription hostDescription;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070061 private VlanId innerVlan = VlanId.vlanId((short) 20);
62 private EthType outerTpid = EthType.EtherType.lookup((short) 0x88a8).ethType();
Charles Chand6d581a2015-11-18 16:51:08 -080063
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 Chaneb5bd492019-12-18 15:49:39 -080071 hostDescription = new DefaultHostDescription(mac, vlan, locations, auxLocations, ips,
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070072 innerVlan, outerTpid, true);
Charles Chand6d581a2015-11-18 16:51:08 -080073 }
74
75 @Test
76 public void testAddHost() throws Exception {
Charles Chaneb5bd492019-12-18 15:49:39 -080077 provider.addHost(mac, vlan, locations, auxLocations, ips, innerVlan, outerTpid);
Charles Chand6d581a2015-11-18 16:51:08 -080078 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 Chaneb5bd492019-12-18 15:49:39 -080086 provider.updateHost(mac, vlan, locations, auxLocations, ips, innerVlan, outerTpid);
Charles Chand6d581a2015-11-18 16:51:08 -080087 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 Chan888e20a2017-05-01 15:44:23 -0700131
132 }
133
134 @Override
135 public void removeLocationFromHost(HostId hostId, HostLocation location) {
136
Charles Chand6d581a2015-11-18 16:51:08 -0800137 }
138
139 public void clear() {
140 this.hostId = null;
141 this.hostDescription = null;
142 this.event = null;
143 }
144 }
145}