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