blob: 36de21168438383cd84dde075d7f23037514e281 [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 Chand6d581a2015-11-18 16:51:08 -080055 private Set<IpAddress> ips = new HashSet<>();
56 private HostId hostId = HostId.hostId(mac, vlan);
57 private HostDescription hostDescription;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070058 private VlanId innerVlan = VlanId.vlanId((short) 20);
59 private EthType outerTpid = EthType.EtherType.lookup((short) 0x88a8).ethType();
Charles Chand6d581a2015-11-18 16:51:08 -080060
61 @Before
62 public void setUp() {
63 provider.providerService = providerService;
64
65 // Initialize test variables
66 ips.add(IpAddress.valueOf("10.0.0.1"));
67 ips.add(IpAddress.valueOf("192.168.0.1"));
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070068 hostDescription = new DefaultHostDescription(mac, vlan, locations, ips,
69 innerVlan, outerTpid, true);
Charles Chand6d581a2015-11-18 16:51:08 -080070 }
71
72 @Test
73 public void testAddHost() throws Exception {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070074 provider.addHost(mac, vlan, locations, ips, innerVlan, outerTpid);
Charles Chand6d581a2015-11-18 16:51:08 -080075 assertThat(providerService.hostId, is(hostId));
76 assertThat(providerService.hostDescription, is(hostDescription));
77 assertThat(providerService.event, is("hostDetected"));
78 providerService.clear();
79 }
80
81 @Test
82 public void testUpdateHost() throws Exception {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070083 provider.updateHost(mac, vlan, locations, ips, innerVlan, outerTpid);
Charles Chand6d581a2015-11-18 16:51:08 -080084 assertThat(providerService.hostId, is(hostId));
85 assertThat(providerService.hostDescription, is(hostDescription));
86 assertThat(providerService.event, is("hostDetected"));
87 providerService.clear();
88 }
89
90 @Test
91 public void testRemoveHost() throws Exception {
92 provider.removeHost(mac, vlan);
93 assertThat(providerService.hostId, is(hostId));
94 assertNull(providerService.hostDescription);
95 assertThat(providerService.event, is("hostVanished"));
96 providerService.clear();
97 }
98
99 /**
100 * Mock HostProviderService.
101 */
102 private class MockHostProviderService
103 extends AbstractProviderService<HostProvider>
104 implements HostProviderService {
105 private HostId hostId = null;
106 private HostDescription hostDescription = null;
107 private String event = null;
108
109 public MockHostProviderService(HostProvider provider) {
110 super(provider);
111 }
112
113 @Override
114 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
115 this.hostId = hostId;
116 this.hostDescription = hostDescription;
117 this.event = "hostDetected";
118 }
119
120 @Override
121 public void hostVanished(HostId hostId) {
122 this.hostId = hostId;
123 this.event = "hostVanished";
124 }
125
126 @Override
127 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
Charles Chan888e20a2017-05-01 15:44:23 -0700128
129 }
130
131 @Override
132 public void removeLocationFromHost(HostId hostId, HostLocation location) {
133
Charles Chand6d581a2015-11-18 16:51:08 -0800134 }
135
136 public void clear() {
137 this.hostId = null;
138 this.hostDescription = null;
139 this.event = null;
140 }
141 }
142}