blob: 03642f49f1e868285d9e4a7526bacb13f44e6263 [file] [log] [blame]
YuanyouZhang6683a072015-07-22 17:38:31 +08001/*
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 */
16package org.onosproject.ovsdb.provider.tunnel;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNotNull;
20
21import java.util.ArrayList;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Set;
25
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.onlab.packet.IpAddress;
30import org.onosproject.core.DefaultGroupId;
31import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
32import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
33import org.onosproject.incubator.net.tunnel.Tunnel;
34import org.onosproject.incubator.net.tunnel.TunnelDescription;
35import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
36import org.onosproject.incubator.net.tunnel.TunnelId;
37import org.onosproject.incubator.net.tunnel.TunnelName;
38import org.onosproject.incubator.net.tunnel.TunnelProvider;
39import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
40import org.onosproject.incubator.net.tunnel.TunnelProviderService;
41import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.DefaultAnnotations;
43import org.onosproject.net.DefaultLink;
44import org.onosproject.net.DefaultPath;
45import org.onosproject.net.Link;
46import org.onosproject.net.SparseAnnotations;
47import org.onosproject.net.provider.AbstractProviderService;
48import org.onosproject.net.provider.ProviderId;
49
50/**
51 * Test for ovsdb tunnel provider.
52 */
53public class OvsdbTunnelProviderTest {
54 private final OvsdbTunnelProvider provider = new OvsdbTunnelProvider();
55 private final TestTunnelRegistry tunnelRegistry = new TestTunnelRegistry();
56 private TestTunnelProviderService providerService;
57
Ray Milkey2693bda2016-01-22 16:08:14 -080058 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
YuanyouZhang6683a072015-07-22 17:38:31 +080065 @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 Milkey2693bda2016-01-22 16:08:14 -080085
YuanyouZhang6683a072015-07-22 17:38:31 +080086 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 Milkey2693bda2016-01-22 16:08:14 -0800110
YuanyouZhang6683a072015-07-22 17:38:31 +0800111 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}