blob: b8d4d1464c391b58686f0899d3fa55f0e7c89fdf [file] [log] [blame]
YuanyouZhang6683a072015-07-22 17:38:31 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
YuanyouZhang6683a072015-07-22 17:38:31 +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 */
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;
Avantika-Huawei1e0a5862016-04-04 14:22:22 +053041import org.onosproject.incubator.net.tunnel.Tunnel.State;
YuanyouZhang6683a072015-07-22 17:38:31 +080042import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DefaultAnnotations;
44import org.onosproject.net.DefaultLink;
45import org.onosproject.net.DefaultPath;
46import org.onosproject.net.Link;
47import org.onosproject.net.SparseAnnotations;
48import org.onosproject.net.provider.AbstractProviderService;
49import org.onosproject.net.provider.ProviderId;
50
51/**
52 * Test for ovsdb tunnel provider.
53 */
54public class OvsdbTunnelProviderTest {
55 private final OvsdbTunnelProvider provider = new OvsdbTunnelProvider();
56 private final TestTunnelRegistry tunnelRegistry = new TestTunnelRegistry();
57 private TestTunnelProviderService providerService;
58
Ray Milkey2693bda2016-01-22 16:08:14 -080059 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
YuanyouZhang6683a072015-07-22 17:38:31 +080066 @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 Milkey2693bda2016-01-22 16:08:14 -080086
YuanyouZhang6683a072015-07-22 17:38:31 +080087 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 Milkey2693bda2016-01-22 16:08:14 -0800111
YuanyouZhang6683a072015-07-22 17:38:31 +0800112 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-Huawei1e0a5862016-04-04 14:22:22 +0530170 public TunnelId tunnelAdded(TunnelDescription tunnel, State state) {
171 return null;
172 }
173
174 @Override
YuanyouZhang6683a072015-07-22 17:38:31 +0800175 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}