blob: 08798854c41c2f089c3f4426efce488e0d8d7b20 [file] [log] [blame]
samanwita pale7c08de2015-09-24 21:59:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
samanwita pale7c08de2015-09-24 21:59:49 -07003 *
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 */
16package org.onosproject.store.host.impl;
17
samanwita pale7c08de2015-09-24 21:59:49 -070018import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onosproject.net.Host;
24import org.onosproject.net.HostId;
25import org.onosproject.net.HostLocation;
26import org.onosproject.net.host.DefaultHostDescription;
27import org.onosproject.net.host.HostDescription;
28import org.onosproject.net.provider.ProviderId;
samanwita pale7c08de2015-09-24 21:59:49 -070029import org.onosproject.store.service.TestStorageService;
30
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053031import com.google.common.collect.Sets;
32
samanwita pale7c08de2015-09-24 21:59:49 -070033import java.util.HashSet;
34import java.util.Set;
35
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070036import static junit.framework.TestCase.assertTrue;
Charles Chanb1e99242017-07-07 14:11:09 -070037import static org.junit.Assert.assertEquals;
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070038import static org.junit.Assert.assertFalse;
39
samanwita pale7c08de2015-09-24 21:59:49 -070040/**
41 * Tests for the ECHostStore.
42 */
Sho SHIMIZU53bcc242016-08-15 11:19:24 -070043public class DistributedHostStoreTest {
samanwita pale7c08de2015-09-24 21:59:49 -070044
alshabib8a4a6002015-11-25 14:31:16 -080045 private DistributedHostStore ecXHostStore;
samanwita pale7c08de2015-09-24 21:59:49 -070046
47 private static final HostId HOSTID = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1a"));
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053048 private static final HostId HOSTID1 = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1b"));
samanwita pale7c08de2015-09-24 21:59:49 -070049
50 private static final IpAddress IP1 = IpAddress.valueOf("10.2.0.2");
51 private static final IpAddress IP2 = IpAddress.valueOf("10.2.0.3");
52
53 private static final ProviderId PID = new ProviderId("of", "foo");
Charles Chanb1e99242017-07-07 14:11:09 -070054 private static final ProviderId PID2 = new ProviderId("of", "foo2");
55
56 private static final HostDescription HOST_LEARNT =
57 createHostDesc(HOSTID, Sets.newHashSet(IP1), false);
58 private static final HostDescription HOST_CONFIGURED =
59 createHostDesc(HOSTID, Sets.newHashSet(IP1), true);
samanwita pale7c08de2015-09-24 21:59:49 -070060
61 @Before
62 public void setUp() {
alshabib8a4a6002015-11-25 14:31:16 -080063 ecXHostStore = new DistributedHostStore();
samanwita pale7c08de2015-09-24 21:59:49 -070064
65 ecXHostStore.storageService = new TestStorageService();
samanwita pale7c08de2015-09-24 21:59:49 -070066 ecXHostStore.activate();
67 }
68
69 @After
70 public void tearDown() {
71 ecXHostStore.deactivate();
72 }
73
74 /**
75 * Tests the removeIp method call.
76 */
77 @Test
78 public void testRemoveIp() {
79 Set<IpAddress> ips = new HashSet<>();
80 ips.add(IP1);
81 ips.add(IP2);
82
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053083 HostDescription description = createHostDesc(HOSTID, ips);
samanwita pale7c08de2015-09-24 21:59:49 -070084 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
85 ecXHostStore.removeIp(HOSTID, IP1);
86 Host host = ecXHostStore.getHost(HOSTID);
87
88 assertFalse(host.ipAddresses().contains(IP1));
89 assertTrue(host.ipAddresses().contains(IP2));
90 }
91
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +053092 @Test
93 public void testAddHostByIp() {
94 Set<IpAddress> ips = new HashSet<>();
95 ips.add(IP1);
96 ips.add(IP2);
97
98 HostDescription description = createHostDesc(HOSTID, ips);
99 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
100
101 Set<Host> hosts = ecXHostStore.getHosts(IP1);
102
103 assertFalse(hosts.size() > 1);
104 assertTrue(hosts.size() == 1);
105
106 HostDescription description1 = createHostDesc(HOSTID1, Sets.newHashSet(IP2));
107 ecXHostStore.createOrUpdateHost(PID, HOSTID1, description1, false);
108
109 Set<Host> hosts1 = ecXHostStore.getHosts(IP2);
110
111 assertFalse(hosts1.size() < 1);
112 assertTrue(hosts1.size() == 2);
113 }
114
115 @Test
116 public void testRemoveHostByIp() {
117 Set<IpAddress> ips = new HashSet<>();
118 ips.add(IP1);
119 ips.add(IP2);
120
121 HostDescription description = createHostDesc(HOSTID, ips);
122 ecXHostStore.createOrUpdateHost(PID, HOSTID, description, false);
123 ecXHostStore.removeIp(HOSTID, IP1);
124 Set<Host> hosts = ecXHostStore.getHosts(IP1);
125 assertTrue(hosts.size() == 0);
126 }
127
Charles Chanb1e99242017-07-07 14:11:09 -0700128 @Test
129 public void testHostOverride() {
130 Host hostInStore;
131 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
132 hostInStore = ecXHostStore.getHost(HOSTID);
133 assertFalse(hostInStore.configured());
134 assertEquals(PID, hostInStore.providerId());
135
136 // Expect: configured host should override learnt host
137 ecXHostStore.createOrUpdateHost(PID2, HOSTID, HOST_CONFIGURED, true);
138 hostInStore = ecXHostStore.getHost(HOSTID);
139 assertTrue(hostInStore.configured());
140 assertEquals(PID2, hostInStore.providerId());
141
142 // Expect: learnt host should not override configured host
143 ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_LEARNT, false);
144 hostInStore = ecXHostStore.getHost(HOSTID);
145 assertTrue(hostInStore.configured());
146 assertEquals(PID2, hostInStore.providerId());
147 }
148
149 private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips) {
150 return createHostDesc(hostId, ips, false);
151 }
152
153 private static HostDescription createHostDesc(HostId hostId, Set<IpAddress> ips,
154 boolean configured) {
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530155 return new DefaultHostDescription(hostId.mac(),
156 hostId.vlanId(),
157 HostLocation.NONE,
Charles Chanb1e99242017-07-07 14:11:09 -0700158 ips,
159 configured);
Deepa Vaddireddy0a71c8b2017-01-19 21:20:45 +0530160 }
Sho SHIMIZU53bcc242016-08-15 11:19:24 -0700161}