blob: 50071db0c0ff06de0eb09f271fe2e008e2b85efa [file] [log] [blame]
Charles Chand6d581a2015-11-18 16:51:08 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.HostId;
27import org.onosproject.net.HostLocation;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.host.DefaultHostDescription;
30import org.onosproject.net.host.HostDescription;
31import org.onosproject.net.host.HostProvider;
32import org.onosproject.net.host.HostProviderService;
33import org.onosproject.net.provider.AbstractProviderService;
34
35import java.util.HashSet;
36import java.util.Set;
37
38import static org.hamcrest.Matchers.is;
39import static org.junit.Assert.assertNull;
40import static org.junit.Assert.assertThat;
41
42/**
43 * Set of tests of the host location provider for CORD.
44 */
45public class NetworkConfigHostProviderTest {
46 private NetworkConfigHostProvider provider = new NetworkConfigHostProvider();
47 private MockHostProviderService providerService = new MockHostProviderService(provider);
48
49 private MacAddress mac = MacAddress.valueOf("c0:ff:ee:c0:ff:ee");
50 private VlanId vlan = VlanId.vlanId(VlanId.UNTAGGED);
51 private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001");
52 private PortNumber port = PortNumber.portNumber(5);
Charles Chan61fc0d82017-04-28 14:13:36 -070053 private Set<HostLocation> locations = Sets.newHashSet(new HostLocation(deviceId, port, 100));
Charles Chand6d581a2015-11-18 16:51:08 -080054 private Set<IpAddress> ips = new HashSet<>();
55 private HostId hostId = HostId.hostId(mac, vlan);
56 private HostDescription hostDescription;
57
58 @Before
59 public void setUp() {
60 provider.providerService = providerService;
61
62 // Initialize test variables
63 ips.add(IpAddress.valueOf("10.0.0.1"));
64 ips.add(IpAddress.valueOf("192.168.0.1"));
Charles Chan61fc0d82017-04-28 14:13:36 -070065 hostDescription = new DefaultHostDescription(mac, vlan, locations, ips, true);
Charles Chand6d581a2015-11-18 16:51:08 -080066 }
67
68 @Test
69 public void testAddHost() throws Exception {
Charles Chan61fc0d82017-04-28 14:13:36 -070070 provider.addHost(mac, vlan, locations, ips);
Charles Chand6d581a2015-11-18 16:51:08 -080071 assertThat(providerService.hostId, is(hostId));
72 assertThat(providerService.hostDescription, is(hostDescription));
73 assertThat(providerService.event, is("hostDetected"));
74 providerService.clear();
75 }
76
77 @Test
78 public void testUpdateHost() throws Exception {
Charles Chan61fc0d82017-04-28 14:13:36 -070079 provider.updateHost(mac, vlan, locations, ips);
Charles Chand6d581a2015-11-18 16:51:08 -080080 assertThat(providerService.hostId, is(hostId));
81 assertThat(providerService.hostDescription, is(hostDescription));
82 assertThat(providerService.event, is("hostDetected"));
83 providerService.clear();
84 }
85
86 @Test
87 public void testRemoveHost() throws Exception {
88 provider.removeHost(mac, vlan);
89 assertThat(providerService.hostId, is(hostId));
90 assertNull(providerService.hostDescription);
91 assertThat(providerService.event, is("hostVanished"));
92 providerService.clear();
93 }
94
95 /**
96 * Mock HostProviderService.
97 */
98 private class MockHostProviderService
99 extends AbstractProviderService<HostProvider>
100 implements HostProviderService {
101 private HostId hostId = null;
102 private HostDescription hostDescription = null;
103 private String event = null;
104
105 public MockHostProviderService(HostProvider provider) {
106 super(provider);
107 }
108
109 @Override
110 public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
111 this.hostId = hostId;
112 this.hostDescription = hostDescription;
113 this.event = "hostDetected";
114 }
115
116 @Override
117 public void hostVanished(HostId hostId) {
118 this.hostId = hostId;
119 this.event = "hostVanished";
120 }
121
122 @Override
123 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
Charles Chan888e20a2017-05-01 15:44:23 -0700124
125 }
126
127 @Override
128 public void removeLocationFromHost(HostId hostId, HostLocation location) {
129
Charles Chand6d581a2015-11-18 16:51:08 -0800130 }
131
132 public void clear() {
133 this.hostId = null;
134 this.hostDescription = null;
135 this.event = null;
136 }
137 }
138}