blob: a4f057cfccb5b3679785d3de580680d24cb99a33 [file] [log] [blame]
Charles Chand6d581a2015-11-18 16:51:08 -08001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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
17package org.onosproject.provider.netcfghost;
18
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.HostId;
26import org.onosproject.net.HostLocation;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.host.DefaultHostDescription;
29import org.onosproject.net.host.HostDescription;
30import org.onosproject.net.host.HostProvider;
31import org.onosproject.net.host.HostProviderService;
32import org.onosproject.net.provider.AbstractProviderService;
33
34import java.util.HashSet;
35import java.util.Set;
36
37import static org.hamcrest.Matchers.is;
38import static org.junit.Assert.assertNull;
39import static org.junit.Assert.assertThat;
40
41/**
42 * Set of tests of the host location provider for CORD.
43 */
44public class NetworkConfigHostProviderTest {
45 private NetworkConfigHostProvider provider = new NetworkConfigHostProvider();
46 private MockHostProviderService providerService = new MockHostProviderService(provider);
47
48 private MacAddress mac = MacAddress.valueOf("c0:ff:ee:c0:ff:ee");
49 private VlanId vlan = VlanId.vlanId(VlanId.UNTAGGED);
50 private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001");
51 private PortNumber port = PortNumber.portNumber(5);
52 private HostLocation hloc = new HostLocation(deviceId, port, 100);
53 private Set<IpAddress> ips = new HashSet<>();
54 private HostId hostId = HostId.hostId(mac, vlan);
55 private HostDescription hostDescription;
56
57 @Before
58 public void setUp() {
59 provider.providerService = providerService;
60
61 // Initialize test variables
62 ips.add(IpAddress.valueOf("10.0.0.1"));
63 ips.add(IpAddress.valueOf("192.168.0.1"));
64 hostDescription = new DefaultHostDescription(mac, vlan, hloc, ips);
65 }
66
67 @Test
68 public void testAddHost() throws Exception {
69 provider.addHost(mac, vlan, hloc, ips);
70 assertThat(providerService.hostId, is(hostId));
71 assertThat(providerService.hostDescription, is(hostDescription));
72 assertThat(providerService.event, is("hostDetected"));
73 providerService.clear();
74 }
75
76 @Test
77 public void testUpdateHost() throws Exception {
78 provider.updateHost(mac, vlan, hloc, ips);
79 assertThat(providerService.hostId, is(hostId));
80 assertThat(providerService.hostDescription, is(hostDescription));
81 assertThat(providerService.event, is("hostDetected"));
82 providerService.clear();
83 }
84
85 @Test
86 public void testRemoveHost() throws Exception {
87 provider.removeHost(mac, vlan);
88 assertThat(providerService.hostId, is(hostId));
89 assertNull(providerService.hostDescription);
90 assertThat(providerService.event, is("hostVanished"));
91 providerService.clear();
92 }
93
94 /**
95 * Mock HostProviderService.
96 */
97 private class MockHostProviderService
98 extends AbstractProviderService<HostProvider>
99 implements HostProviderService {
100 private HostId hostId = null;
101 private HostDescription hostDescription = null;
102 private String event = null;
103
104 public MockHostProviderService(HostProvider provider) {
105 super(provider);
106 }
107
108 @Override
109 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
110 this.hostId = hostId;
111 this.hostDescription = hostDescription;
112 this.event = "hostDetected";
113 }
114
115 @Override
116 public void hostVanished(HostId hostId) {
117 this.hostId = hostId;
118 this.event = "hostVanished";
119 }
120
121 @Override
122 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
123 // Note: This method is never used.
124 }
125
126 public void clear() {
127 this.hostId = null;
128 this.hostDescription = null;
129 this.event = null;
130 }
131 }
132}