2 * Copyright (C) 2014 Cisco Systems, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Thomas Bachman
11 package org.opendaylight.groupbasedpolicy.renderer.opflex;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ScheduledExecutorService;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Matchers;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
36 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
42 import org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcEndpoint;
43 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.OpflexConnectionService;
44 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointDeclareRequest;
45 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointIdentity;
46 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointResolveRequest;
47 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointUndeclareRequest;
48 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointUnresolveRequest;
49 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.MitLib;
50 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.PolicyUri;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.OpflexOverlayContext;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.DataObject;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.fasterxml.jackson.databind.JsonNode;
66 import com.google.common.base.Optional;
67 import com.google.common.util.concurrent.CheckedFuture;
74 public class EndpointManagerTest implements DataChangeListener {
75 protected static final Logger logger = LoggerFactory.getLogger(EndpointManagerTest.class);
76 private static final String TEST_CONTEXT = "3de31df2-5a65-4d5a-b42b-01fa3bdd82ea";
77 private static final int TEST_PRR = 1000;
78 private static final String TEST_IDENTIFIER = "00:11:22:33:44:55";
79 private static final String TEST_AGENT_ID = "192.168.194.132:6742";
80 private static final String TEST_EP_URI = "/EprL2Universe/EprL2Ep/3de31df2-5a65-4d5a-b42b-01fa3bdd82ea/00:11:22:33:44:55";
81 private static final String TEST_SUBJECT = "EprL2Ep";
84 private JsonNode TEST_MSG_ID;
86 private DataBroker mockBroker;
88 private RpcProviderRegistry mockRpcRegistry;
90 private ScheduledExecutorService mockExecutor;
92 private OpflexConnectionService mockConnService;
94 private ListenerRegistration<DataChangeListener> mockListener;
96 private ListenerRegistration<DataChangeListener> mockL3Listener;
98 private JsonRpcEndpoint mockAgent;
100 private EndpointDeclareRequest mockRpcMessage;
102 private BindingAwareBroker.RpcRegistration<EndpointService> mockRpcRegistration;
104 private WriteTransaction mockWriteTransaction;
106 private ReadOnlyTransaction mockReadTransaction;
108 private CheckedFuture<Void, TransactionCommitFailedException> mockWriteFuture;
110 private CheckedFuture<Optional<Endpoint>,ReadFailedException> mockReadFuture;
112 private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> mockChange;
114 private Map<InstanceIdentifier<?>, DataObject> mockDaoMap;
116 private Set<InstanceIdentifier<?>> mockDaoSet;
118 private DataObject mockDao;
120 private InstanceIdentifier<?> mockIid;
122 private MitLib mockOpflexLib;
124 private EndpointIdentity testIdentity;
125 private EndpointManager epManager;
129 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
133 public void setUp() throws Exception {
134 MockitoAnnotations.initMocks(this);
135 when(mockBroker.registerDataChangeListener(Matchers.<LogicalDatastoreType>any(),
136 Matchers.<InstanceIdentifier<Endpoint>>any(), Matchers.<DataChangeListener>any(),
137 Matchers.<DataChangeScope>any()))
138 .thenReturn(mockListener);
140 // The following is needed to satisfy the AbstractEndpointRegistry
141 when(mockRpcRegistry.addRpcImplementation(Matchers.<Class<EndpointService>>any(),
142 Matchers.<EndpointService>any())).thenReturn(mockRpcRegistration);
143 when(mockBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction);
144 when(mockBroker.newReadOnlyTransaction()).thenReturn(mockReadTransaction);
145 when(mockReadTransaction.read(Matchers.<LogicalDatastoreType>any(),
146 Matchers.<InstanceIdentifier<Endpoint>>any())).thenReturn(mockReadFuture);
147 when(mockWriteTransaction.submit()).thenReturn(mockWriteFuture);
148 epManager = new EndpointManager(mockBroker,
149 mockRpcRegistry, mockExecutor, mockConnService, mockOpflexLib);
151 PolicyUri puri = new PolicyUri(TEST_EP_URI);
152 testIdentity = new EndpointIdentity();
153 testIdentity.setIdentifier(puri.pop());
154 testIdentity.setContext(puri.getUri());
159 public void testConstructor() throws Exception {
160 verify(mockBroker, times(2)).registerDataChangeListener(Matchers.<LogicalDatastoreType>any(),
161 Matchers.<InstanceIdentifier<Endpoint>>any(), Matchers.<DataChangeListener>any(),
162 Matchers.<DataChangeScope>any());
166 public void testCallbackEpDeclare() throws Exception {
167 JsonRpcEndpoint agent = mock(JsonRpcEndpoint.class);
168 EndpointDeclareRequest mockReq =
169 mock(EndpointDeclareRequest.class);
170 EndpointDeclareRequest.Params mockParams =
171 mock(EndpointDeclareRequest.Params.class);
172 List<EndpointDeclareRequest.Params> paramList =
173 new ArrayList<EndpointDeclareRequest.Params>();
174 paramList.add(mockParams);
175 List<String> idList =
176 new ArrayList<String>();
177 idList.add(TEST_IDENTIFIER);
179 when(mockReq.valid()).thenReturn(true);
180 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
181 when(mockReq.getMethod()).thenReturn(EndpointDeclareRequest.DECLARE_MESSAGE);
182 when(mockReq.getParams()).thenReturn(paramList);
183 when(mockParams.getEndpoint()).thenReturn(null);
184 when(mockParams.getPrr()).thenReturn(TEST_PRR);
185 epManager.callback(agent, mockReq);
186 verify(mockParams).getEndpoint();
191 public void testCallbackEpUndeclare() throws Exception {
192 JsonRpcEndpoint agent = mock(JsonRpcEndpoint.class);
193 EndpointUndeclareRequest mockReq =
194 mock(EndpointUndeclareRequest.class);
195 EndpointUndeclareRequest.Params mockParams =
196 mock(EndpointUndeclareRequest.Params.class);
197 List<EndpointUndeclareRequest.Params> paramList =
198 new ArrayList<EndpointUndeclareRequest.Params>();
199 paramList.add(mockParams);
200 List<String> idList =
201 new ArrayList<String>();
202 idList.add(TEST_IDENTIFIER);
204 when(mockReq.valid()).thenReturn(true);
205 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
206 when(mockReq.getMethod()).thenReturn(EndpointUndeclareRequest.UNDECLARE_MESSAGE);
207 when(mockReq.getParams()).thenReturn(paramList);
208 when(mockParams.getEndpoint_uri()).thenReturn(null);
209 when(mockParams.getSubject()).thenReturn(TEST_SUBJECT);
210 epManager.callback(agent, mockReq);
211 verify(mockParams).getEndpoint_uri();
216 public void testCallbackEpResolve1() throws Exception {
217 JsonRpcEndpoint mockRpcEp = mock(JsonRpcEndpoint.class);
218 EndpointResolveRequest mockReq =
219 mock(EndpointResolveRequest.class);
220 EndpointResolveRequest.Params mockParams =
221 mock(EndpointResolveRequest.Params.class);
222 List<EndpointResolveRequest.Params> paramList =
223 new ArrayList<EndpointResolveRequest.Params>();
224 paramList.add(mockParams);
226 when(mockRpcEp.getIdentifier()).thenReturn(TEST_IDENTIFIER);
227 when(mockReq.valid()).thenReturn(true);
228 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
229 when(mockReq.getMethod()).thenReturn(EndpointResolveRequest.EP_RESOLVE_REQUEST_MESSAGE);
230 when(mockReq.getParams()).thenReturn(paramList);
231 when(mockParams.getEndpoint_ident()).thenReturn(null);
232 when(mockParams.getEndpoint_uri()).thenReturn(new Uri(TEST_EP_URI));
233 when(mockParams.getSubject()).thenReturn(TEST_SUBJECT);
234 epManager.callback(mockRpcEp, mockReq);
235 verify(mockParams, times(3)).getEndpoint_uri();
239 public void testCallbackEpResolve2() throws Exception {
240 JsonRpcEndpoint mockRpcEp = mock(JsonRpcEndpoint.class);
241 EndpointResolveRequest mockReq =
242 mock(EndpointResolveRequest.class);
243 EndpointResolveRequest.Params mockParams =
244 mock(EndpointResolveRequest.Params.class);
245 List<EndpointResolveRequest.Params> paramList =
246 new ArrayList<EndpointResolveRequest.Params>();
247 paramList.add(mockParams);
249 when(mockRpcEp.getIdentifier()).thenReturn(TEST_IDENTIFIER);
250 when(mockReq.valid()).thenReturn(true);
251 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
252 when(mockReq.getMethod()).thenReturn(EndpointResolveRequest.EP_RESOLVE_REQUEST_MESSAGE);
253 when(mockReq.getParams()).thenReturn(paramList);
254 when(mockParams.getEndpoint_ident()).thenReturn(testIdentity);
255 when(mockParams.getEndpoint_uri()).thenReturn(null);
256 when(mockParams.getSubject()).thenReturn(TEST_SUBJECT);
257 epManager.callback(mockRpcEp, mockReq);
258 verify(mockParams, times(3)).getEndpoint_ident();
262 public void testCallbackEpUnresolve1() throws Exception {
263 JsonRpcEndpoint mockRpcEp = mock(JsonRpcEndpoint.class);
264 EndpointUnresolveRequest mockReq =
265 mock(EndpointUnresolveRequest.class);
266 EndpointUnresolveRequest.Params mockParams =
267 mock(EndpointUnresolveRequest.Params.class);
268 List<EndpointUnresolveRequest.Params> paramList =
269 new ArrayList<EndpointUnresolveRequest.Params>();
270 paramList.add(mockParams);
272 when(mockRpcEp.getIdentifier()).thenReturn(TEST_IDENTIFIER);
273 when(mockReq.valid()).thenReturn(true);
274 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
275 when(mockReq.getMethod()).thenReturn(EndpointUnresolveRequest.EP_UNRESOLVE_REQUEST_MESSAGE);
276 when(mockReq.getParams()).thenReturn(paramList);
277 when(mockParams.getEndpoint_ident()).thenReturn(null);
278 when(mockParams.getEndpoint_uri()).thenReturn(new Uri(TEST_EP_URI));
279 when(mockParams.getSubject()).thenReturn(TEST_SUBJECT);
280 epManager.callback(mockRpcEp, mockReq);
281 verify(mockParams, times(2)).getEndpoint_uri();
286 public void testCallbackEpUnresolve2() throws Exception {
287 JsonRpcEndpoint mockRpcEp = mock(JsonRpcEndpoint.class);
288 EndpointUnresolveRequest mockReq =
289 mock(EndpointUnresolveRequest.class);
290 EndpointUnresolveRequest.Params mockParams =
291 mock(EndpointUnresolveRequest.Params.class);
292 List<EndpointUnresolveRequest.Params> paramList =
293 new ArrayList<EndpointUnresolveRequest.Params>();
294 paramList.add(mockParams);
296 when(mockRpcEp.getIdentifier()).thenReturn(TEST_IDENTIFIER);
297 when(mockReq.valid()).thenReturn(true);
298 when(mockReq.getId()).thenReturn(TEST_MSG_ID);
299 when(mockReq.getMethod()).thenReturn(EndpointUnresolveRequest.EP_UNRESOLVE_REQUEST_MESSAGE);
300 when(mockReq.getParams()).thenReturn(paramList);
301 when(mockParams.getEndpoint_ident()).thenReturn(testIdentity);
302 when(mockParams.getEndpoint_uri()).thenReturn(null);
303 when(mockParams.getSubject()).thenReturn(TEST_SUBJECT);
304 epManager.callback(mockRpcEp, mockReq);
305 verify(mockParams, times(2)).getEndpoint_ident();
310 public void testOnDataChangedCreated() throws Exception {
311 List<DataObject> daoList =
312 new ArrayList<DataObject>();
313 Endpoint mockEp = mock(Endpoint.class);
314 EndpointGroupId mockEpgId = mock(EndpointGroupId.class);
315 TenantId mockTid = mock(TenantId.class);
316 L2BridgeDomainId mockBdId = mock(L2BridgeDomainId.class);
317 MacAddress mockMac = mock(MacAddress.class);
318 OpflexOverlayContext mockCtx = mock(OpflexOverlayContext.class);
319 Set<InstanceIdentifier<?>> emptySet =
320 Collections.emptySet();
321 Map<InstanceIdentifier<?>,DataObject> emptyMap =
322 new ConcurrentHashMap<InstanceIdentifier<?>, DataObject>();
325 when(mockEp.getEndpointGroup()).thenReturn(mockEpgId);
326 when(mockEp.getTenant()).thenReturn(mockTid);
327 when(mockEp.getL2Context()).thenReturn(mockBdId);
328 when(mockBdId.getValue()).thenReturn(TEST_CONTEXT);
329 when(mockEp.getMacAddress()).thenReturn(mockMac);
330 when(mockEp.getAugmentation(OpflexOverlayContext.class)).thenReturn(mockCtx);
331 when(mockCtx.getAgentId()).thenReturn(TEST_AGENT_ID);
332 when(mockMac.getValue()).thenReturn(TEST_IDENTIFIER);
334 when(mockChange.getCreatedData()).thenReturn(mockDaoMap);
335 when(mockDaoMap.values()).thenReturn(daoList);
336 when(mockChange.getRemovedPaths()).thenReturn(emptySet);
337 when(mockChange.getUpdatedData()).thenReturn(emptyMap);
338 epManager.onDataChanged(mockChange);
339 verify(mockChange).getCreatedData();
343 public void testOnDataChangedRemoved() throws Exception {
344 List<DataObject> daoList =
345 new ArrayList<DataObject>();
346 Endpoint mockEp = mock(Endpoint.class);
347 EndpointGroupId mockEpgId = mock(EndpointGroupId.class);
348 TenantId mockTid = mock(TenantId.class);
349 L2BridgeDomainId mockBdId = mock(L2BridgeDomainId.class);
350 MacAddress mockMac = mock(MacAddress.class);
351 OpflexOverlayContext mockCtx = mock(OpflexOverlayContext.class);
352 Map<InstanceIdentifier<?>,DataObject> emptyMap =
353 new ConcurrentHashMap<InstanceIdentifier<?>, DataObject>();
354 Map<InstanceIdentifier<?>,Boolean> dummyMap =
355 new ConcurrentHashMap<InstanceIdentifier<?>, Boolean>();
356 Set<InstanceIdentifier<?>> dummySet =
357 Collections.newSetFromMap(dummyMap);
358 dummySet.add(mockIid);
360 when(mockEp.getEndpointGroup()).thenReturn(mockEpgId);
361 when(mockEp.getTenant()).thenReturn(mockTid);
362 when(mockEp.getL2Context()).thenReturn(mockBdId);
363 when(mockBdId.getValue()).thenReturn(TEST_CONTEXT);
364 when(mockEp.getMacAddress()).thenReturn(mockMac);
365 when(mockEp.getAugmentation(OpflexOverlayContext.class)).thenReturn(mockCtx);
366 when(mockCtx.getAgentId()).thenReturn(TEST_AGENT_ID);
367 when(mockMac.getValue()).thenReturn(TEST_IDENTIFIER);
369 when(mockChange.getCreatedData()).thenReturn(emptyMap);
370 when(mockChange.getRemovedPaths()).thenReturn(dummySet);
371 when(mockChange.getOriginalData()).thenReturn(mockDaoMap);
372 when(mockChange.getUpdatedData()).thenReturn(emptyMap);
373 when(mockDaoMap.get(Matchers.<InstanceIdentifier<?>>any())).thenReturn(mockEp);
375 epManager.onDataChanged(mockChange);
376 verify(mockChange).getOriginalData();
380 public void testOnDataChangedUpdated() throws Exception {
381 List<DataObject> daoList =
382 new ArrayList<DataObject>();
383 Endpoint mockEp = mock(Endpoint.class);
384 EndpointGroupId mockEpgId = mock(EndpointGroupId.class);
385 TenantId mockTid = mock(TenantId.class);
386 L2BridgeDomainId mockBdId = mock(L2BridgeDomainId.class);
387 MacAddress mockMac = mock(MacAddress.class);
388 OpflexOverlayContext mockCtx = mock(OpflexOverlayContext.class);
389 Set<InstanceIdentifier<?>> emptySet =
390 Collections.emptySet();
391 Map<InstanceIdentifier<?>,DataObject> emptyMap =
392 new ConcurrentHashMap<InstanceIdentifier<?>, DataObject>();
393 Map<InstanceIdentifier<?>,DataObject> dummyMap =
394 new ConcurrentHashMap<InstanceIdentifier<?>, DataObject>();
395 dummyMap.put(mockIid, mockEp);
398 when(mockEp.getEndpointGroup()).thenReturn(mockEpgId);
399 when(mockEp.getTenant()).thenReturn(mockTid);
400 when(mockEp.getL2Context()).thenReturn(mockBdId);
401 when(mockEp.getMacAddress()).thenReturn(mockMac);
402 when(mockEp.getAugmentation(OpflexOverlayContext.class)).thenReturn(mockCtx);
403 when(mockCtx.getAgentId()).thenReturn(TEST_AGENT_ID);
404 when(mockMac.getValue()).thenReturn(TEST_IDENTIFIER);
406 when(mockChange.getCreatedData()).thenReturn(emptyMap);
407 when(mockChange.getRemovedPaths()).thenReturn(emptySet);
408 when(mockChange.getUpdatedData()).thenReturn(dummyMap);
409 when(mockChange.getOriginalData()).thenReturn(mockDaoMap);
410 when(mockDaoMap.get(Matchers.<InstanceIdentifier<?>>any())).thenReturn(mockEp);
412 when(mockDaoMap.values()).thenReturn(daoList);
413 epManager.onDataChanged(mockChange);
414 verify(mockChange).getOriginalData();