YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 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 | package org.onosproject.ovsdb.provider.tunnel; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNotNull; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onosproject.core.DefaultGroupId; |
| 31 | import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription; |
| 32 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 33 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelDescription; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 36 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 37 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 38 | import org.onosproject.incubator.net.tunnel.TunnelProvider; |
| 39 | import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry; |
| 40 | import org.onosproject.incubator.net.tunnel.TunnelProviderService; |
Avantika-Huawei | 1e0a586 | 2016-04-04 14:22:22 +0530 | [diff] [blame] | 41 | import org.onosproject.incubator.net.tunnel.Tunnel.State; |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DefaultAnnotations; |
| 44 | import org.onosproject.net.DefaultLink; |
| 45 | import org.onosproject.net.DefaultPath; |
| 46 | import org.onosproject.net.Link; |
| 47 | import org.onosproject.net.SparseAnnotations; |
| 48 | import org.onosproject.net.provider.AbstractProviderService; |
| 49 | import org.onosproject.net.provider.ProviderId; |
| 50 | |
| 51 | /** |
| 52 | * Test for ovsdb tunnel provider. |
| 53 | */ |
| 54 | public class OvsdbTunnelProviderTest { |
| 55 | private final OvsdbTunnelProvider provider = new OvsdbTunnelProvider(); |
| 56 | private final TestTunnelRegistry tunnelRegistry = new TestTunnelRegistry(); |
| 57 | private TestTunnelProviderService providerService; |
| 58 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 59 | Link link = DefaultLink.builder() |
| 60 | .providerId(this.provider.id()) |
| 61 | .src(ConnectPoint.deviceConnectPoint("192.168.2.3/20")) |
| 62 | .dst(ConnectPoint.deviceConnectPoint("192.168.2.4/30")) |
| 63 | .type(Link.Type.DIRECT) |
| 64 | .build(); |
| 65 | |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 66 | @Before |
| 67 | public void setUp() { |
| 68 | provider.providerRegistry = tunnelRegistry; |
| 69 | provider.activate(); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void basics() { |
| 74 | assertNotNull("registration expected", providerService); |
| 75 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testTunnelAdded() { |
| 80 | TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 81 | .valueOf("192.168.1.1")); |
| 82 | TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 83 | .valueOf("192.168.1.3")); |
| 84 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 85 | .set("bandwidth", "1024").build(); |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 86 | |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 87 | List<Link> links = new ArrayList<Link>(); |
| 88 | links.add(link); |
| 89 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 90 | TunnelId.valueOf("1234"), |
| 91 | src, |
| 92 | dst, |
| 93 | Tunnel.Type.VXLAN, |
| 94 | new DefaultGroupId(0), |
| 95 | this.provider.id(), |
| 96 | TunnelName.tunnelName("tunnel12"), |
| 97 | new DefaultPath(this.provider.id(), links, 0.3), |
| 98 | annotations); |
| 99 | provider.tunnelAdded(tunnel); |
| 100 | assertEquals(1, providerService.tunnelSet.size()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testTunnelRemoved() { |
| 105 | TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 106 | .valueOf("192.168.1.1")); |
| 107 | TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 108 | .valueOf("192.168.1.3")); |
| 109 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 110 | .set("bandwidth", "1024").build(); |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 111 | |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 112 | List<Link> links = new ArrayList<Link>(); |
| 113 | links.add(link); |
| 114 | TunnelDescription tunnel = new DefaultTunnelDescription( |
| 115 | TunnelId.valueOf("1234"), |
| 116 | src, |
| 117 | dst, |
| 118 | Tunnel.Type.VXLAN, |
| 119 | new DefaultGroupId(0), |
| 120 | this.provider.id(), |
| 121 | TunnelName.tunnelName("tunnel1"), |
| 122 | new DefaultPath(this.provider.id(), links, 0.3), |
| 123 | annotations); |
| 124 | provider.tunnelRemoved(tunnel); |
| 125 | assertEquals(0, providerService.tunnelSet.size()); |
| 126 | } |
| 127 | |
| 128 | @After |
| 129 | public void tearDown() { |
| 130 | provider.deactivate(); |
| 131 | provider.providerRegistry = null; |
| 132 | } |
| 133 | |
| 134 | private class TestTunnelRegistry implements TunnelProviderRegistry { |
| 135 | |
| 136 | @Override |
| 137 | public TunnelProviderService register(TunnelProvider provider) { |
| 138 | providerService = new TestTunnelProviderService(provider); |
| 139 | return providerService; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void unregister(TunnelProvider provider) { |
| 144 | |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Set<ProviderId> getProviders() { |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | private class TestTunnelProviderService |
| 155 | extends AbstractProviderService<TunnelProvider> |
| 156 | implements TunnelProviderService { |
| 157 | Set<TunnelDescription> tunnelSet = new HashSet<TunnelDescription>(); |
| 158 | |
| 159 | protected TestTunnelProviderService(TunnelProvider provider) { |
| 160 | super(provider); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public TunnelId tunnelAdded(TunnelDescription tunnel) { |
| 165 | tunnelSet.add(tunnel); |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | @Override |
Avantika-Huawei | 1e0a586 | 2016-04-04 14:22:22 +0530 | [diff] [blame] | 170 | public TunnelId tunnelAdded(TunnelDescription tunnel, State state) { |
| 171 | return null; |
| 172 | } |
| 173 | |
| 174 | @Override |
YuanyouZhang | 6683a07 | 2015-07-22 17:38:31 +0800 | [diff] [blame] | 175 | public void tunnelRemoved(TunnelDescription tunnel) { |
| 176 | tunnelSet.remove(tunnel); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void tunnelUpdated(TunnelDescription tunnel) { |
| 181 | |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Tunnel tunnelQueryById(TunnelId tunnelId) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 | } |