blob: 15c21042a9a5b28c9114b7be574a83b254eee612 [file] [log] [blame]
Phaneendra Manda8db7d092016-06-04 00:17:24 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Phaneendra Manda8db7d092016-06-04 00:17:24 +05303 *
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.vtnweb.resources;
17
Ray Milkey094a1352018-01-22 14:03:54 -080018import com.eclipsesource.json.Json;
19import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053027import org.onosproject.codec.CodecService;
28import org.onosproject.net.DeviceId;
29import org.onosproject.vtnrsc.FiveTuple;
30import org.onosproject.vtnrsc.FlowClassifierId;
31import org.onosproject.vtnrsc.LoadBalanceId;
32import org.onosproject.vtnrsc.PortChain;
33import org.onosproject.vtnrsc.PortChainId;
34import org.onosproject.vtnrsc.PortPairGroupId;
35import org.onosproject.vtnrsc.PortPairId;
36import org.onosproject.vtnrsc.TenantId;
37import org.onosproject.vtnrsc.portchain.PortChainService;
38import org.onosproject.vtnweb.web.SfcCodecContext;
39
Ray Milkey094a1352018-01-22 14:03:54 -080040import javax.ws.rs.client.WebTarget;
41import java.util.HashSet;
42import java.util.List;
43import java.util.Objects;
44import java.util.Set;
45
46import static org.easymock.EasyMock.anyObject;
47import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.expect;
49import static org.easymock.EasyMock.replay;
50import static org.hamcrest.Matchers.is;
51import static org.hamcrest.Matchers.notNullValue;
52import static org.junit.Assert.assertThat;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053053
54/**
55 * Unit tests for port chain device map REST APIs.
56 */
57public class PortChainDeviceMapResourceTest extends VtnResourceTest {
58
59 final PortChainService portChainService = createMock(PortChainService.class);
60
61 PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
62 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
63 private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList();
64 private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList();
65
66 final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1",
67 "Mock port chain", portPairGroupList1,
68 flowClassifierList1);
69
70 /**
71 * Mock class for a port chain.
72 */
73 private static class MockPortChain implements PortChain {
74
75 private final PortChainId portChainId;
76 private final TenantId tenantId;
77 private final String name;
78 private final String description;
79 private final List<PortPairGroupId> portPairGroupList;
80 private final List<FlowClassifierId> flowClassifierList;
81
82 public MockPortChain(PortChainId portChainId, TenantId tenantId,
83 String name, String description,
84 List<PortPairGroupId> portPairGroupList,
85 List<FlowClassifierId> flowClassifierList) {
86
87 this.portChainId = portChainId;
88 this.tenantId = tenantId;
89 this.name = name;
90 this.description = description;
91 this.portPairGroupList = portPairGroupList;
92 this.flowClassifierList = flowClassifierList;
93 }
94
95 @Override
96 public PortChainId portChainId() {
97 return portChainId;
98 }
99
100 @Override
101 public TenantId tenantId() {
102 return tenantId;
103 }
104
105 @Override
106 public String name() {
107 return name;
108 }
109
110 @Override
111 public String description() {
112 return description;
113 }
114
115 @Override
116 public List<PortPairGroupId> portPairGroups() {
117 return ImmutableList.copyOf(portPairGroupList);
118 }
119
120 @Override
121 public List<FlowClassifierId> flowClassifiers() {
122 return ImmutableList.copyOf(flowClassifierList);
123 }
124
125 @Override
126 public boolean exactMatch(PortChain portChain) {
127 return this.equals(portChain) &&
128 Objects.equals(this.portChainId, portChain.portChainId()) &&
129 Objects.equals(this.tenantId, portChain.tenantId());
130 }
131
132 @Override
133 public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) {
134 }
135
136 @Override
137 public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) {
138 return null;
139 }
140
141 @Override
142 public Set<FiveTuple> getLoadBalanceIdMapKeys() {
143 return null;
144 }
145
146 @Override
147 public List<PortPairId> getLoadBalancePath(LoadBalanceId id) {
148 return null;
149 }
150
151 @Override
152 public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) {
153 return null;
154 }
155
156 @Override
157 public LoadBalanceId matchPath(List<PortPairId> path) {
158 return null;
159 }
160
161 @Override
162 public int getLoadBalancePathSize() {
163 return 0;
164 }
165
166 @Override
167 public void addSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) {
168 }
169
170 @Override
171 public void addSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) {
172 }
173
174 @Override
175 public void removeSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) {
176 }
177
178 @Override
179 public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) {
180 }
181
182 @Override
183 public List<DeviceId> getSfcClassifiers(LoadBalanceId id) {
184 DeviceId deviceId1 = DeviceId.deviceId("of:000000000000001");
185 List<DeviceId> classifierList = Lists.newArrayList();
186 classifierList.add(deviceId1);
187 return classifierList;
188 }
189
190 @Override
191 public List<DeviceId> getSfcForwarders(LoadBalanceId id) {
192 DeviceId deviceId1 = DeviceId.deviceId("of:000000000000002");
193 DeviceId deviceId2 = DeviceId.deviceId("of:000000000000003");
194 List<DeviceId> forwarderList = Lists.newArrayList();
195 forwarderList.add(deviceId1);
196 forwarderList.add(deviceId2);
197 return forwarderList;
198 }
199
200 @Override
201 public Set<LoadBalanceId> getLoadBalancePathMapKeys() {
202 LoadBalanceId id = LoadBalanceId.of((byte) 1);
203 Set<LoadBalanceId> set = new HashSet<LoadBalanceId>();
204 set.add(id);
205 return set;
206 }
207
208 @Override
209 public PortChain oldPortChain() {
210 return null;
211 }
212 }
213
214 /**
215 * Sets up the global values for all the tests.
216 */
217 @Before
218 public void setUpTest() {
219 SfcCodecContext context = new SfcCodecContext();
220 ServiceDirectory testDirectory = new TestServiceDirectory()
221 .add(PortChainService.class, portChainService)
222 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -0800223 setServiceDirectory(testDirectory);
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530224 }
225
226 /**
227 * Cleans up.
228 */
229 @After
230 public void tearDownTest() {
231 }
232
233 /**
234 * Tests the result of a rest api GET for port chain id.
235 */
236 @Test
237 public void testGetPortChainDeviceMap() {
238
239 expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes();
240 replay(portChainService);
241
242 final WebTarget wt = target();
243 final String response = wt.path("portChainDeviceMap/1278dcd4-459f-62ed-754b-87fc5e4a6751").request()
244 .get(String.class);
245 final JsonObject result = Json.parse(response).asObject();
246 assertThat(result, notNullValue());
247 assertThat(result.names().get(0), is("portChainDeviceMap"));
248
249 }
250}