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