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.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.util.Collections;
20 import java.util.List;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
26 import org.junit.Before;
27 import org.mockito.Matchers;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
35 import org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcEndpoint;
36 import org.opendaylight.groupbasedpolicy.jsonrpc.RpcMessageMap;
37 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.OpflexAgent;
38 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.OpflexConnectionService;
39 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.EndpointDeclareRequest;
40 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.messages.PolicyResolveRequest;
41 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.MitLib;
42 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
43 import org.opendaylight.groupbasedpolicy.resolver.Policy;
44 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
45 import org.opendaylight.groupbasedpolicy.resolver.PolicyListener;
46 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
47 import org.opendaylight.groupbasedpolicy.resolver.PolicyScope;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.fasterxml.jackson.databind.JsonNode;
59 import com.google.common.util.concurrent.CheckedFuture;
66 public class PolicyManagerTest implements DataChangeListener {
67 protected static final Logger logger = LoggerFactory.getLogger(PolicyManagerTest.class);
68 private static final String TEST_AGENT_ID = "192.168.194.11:6723";
69 private static final int TEST_TIMEOUT = 500;
70 private static final String TEST_POLICY = "foo-boo";
73 private JsonNode TEST_MSG_ID;
75 private PolicyResolver mockResolver;
77 private OpflexConnectionService mockConnService;
79 private ListenerRegistration<DataChangeListener> mockListener;
81 private ListenerRegistration<DataChangeListener> mockL3Listener;
83 private EndpointDeclareRequest mockRpcMessage;
85 private BindingAwareBroker.RpcRegistration<EndpointService> mockRpcRegistration;
87 private WriteTransaction mockWriteTransaction;
89 private CheckedFuture<Void, TransactionCommitFailedException> mockFuture;
91 private PolicyInfo mockPolicyInfo;
93 private OpflexAgent mockAgent;
95 private PolicyScope mockScope;
97 private List<PolicyResolveRequest.Params> mockParamsList;
99 private MitLib mockOpflexLib;
102 private JsonRpcEndpoint dummyEndpoint;
104 private PolicyManager policyManager;
105 private ScheduledExecutorService executor;
108 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
112 public void setUp() throws Exception {
113 MockitoAnnotations.initMocks(this);
114 int numCPU = Runtime.getRuntime().availableProcessors();
115 executor = Executors.newScheduledThreadPool(numCPU * 2);
117 new JsonRpcEndpoint(null, null,
118 null, null, new RpcMessageMap(), null);
120 .registerListener(Matchers.<PolicyListener>any())).thenReturn(mockScope);
122 policyManager = new PolicyManager(mockResolver,
123 mockConnService, executor, mockOpflexLib);
128 public void testPolicyUpdated() throws Exception {
129 EgKey sepgKey = mock(EgKey.class);
130 EgKey depgKey = mock(EgKey.class);
131 Policy mockPolicy = mock(Policy.class);
133 Set<EgKey> degKeySet = Collections.
134 newSetFromMap(new ConcurrentHashMap<EgKey, Boolean>());
135 degKeySet.add(depgKey);
136 Set<EgKey> segKeySet = Collections.
137 newSetFromMap(new ConcurrentHashMap<EgKey, Boolean>());
138 segKeySet.add(sepgKey);
140 when(mockResolver.getCurrentPolicy()).thenReturn(mockPolicyInfo);
141 when(mockConnService.getOpflexAgent(anyString())).thenReturn(mockAgent);
142 when(mockPolicyInfo.getPeers(sepgKey)).thenReturn(degKeySet);
143 when(mockPolicyInfo.getPolicy(sepgKey, depgKey)).thenReturn(mockPolicy);
144 when(mockAgent.getEndpoint()).thenReturn(dummyEndpoint);
147 * Add some EPGs to enable messaging
149 //policyManager.dirty.get().addEndpointGroup(sepgKey);
150 //policyManager.dirty.get().addEndpointGroup(depgKey);
155 //policyManager.dirty.get().addAgent(TEST_AGENT_ID);
157 policyManager.policyUpdated(segKeySet);
159 verify(mockAgent, timeout(TEST_TIMEOUT)).getEndpoint();
164 public void testGroupEndpointUpdated() throws Exception {
165 EgKey sepgKey = mock(EgKey.class);
166 EgKey depgKey = mock(EgKey.class);
167 Policy mockPolicy = mock(Policy.class);
168 TenantId tId = mock(TenantId.class);
169 EndpointGroupId epgId = mock(EndpointGroupId.class);
171 Set<EgKey> egKeySet = Collections.
172 newSetFromMap(new ConcurrentHashMap<EgKey, Boolean>());
173 egKeySet.add(depgKey);
175 when(mockResolver.getCurrentPolicy()).thenReturn(mockPolicyInfo);
176 when(mockConnService.getOpflexAgent(anyString())).thenReturn(mockAgent);
177 when(mockPolicyInfo.getPeers(sepgKey)).thenReturn(egKeySet);
178 when(mockPolicyInfo.getPolicy(sepgKey, depgKey)).thenReturn(mockPolicy);
179 when(mockAgent.getEndpoint()).thenReturn(dummyEndpoint);
180 when(sepgKey.getTenantId()).thenReturn(tId);
181 when(sepgKey.getEgId()).thenReturn(epgId);
184 * Add some EPGs to enable messaging
186 //policyManager.dirty.get().addEndpointGroup(sepgKey);
187 //policyManager.dirty.get().addEndpointGroup(depgKey);
192 //policyManager.dirty.get().addAgent(TEST_AGENT_ID);
194 //policyManager.groupEndpointUpdated(sepgKey, epKey);
196 verify(mockAgent, timeout(TEST_TIMEOUT)).getEndpoint();
202 public void testCallback() throws Exception {
203 JsonRpcEndpoint mockEp = mock(JsonRpcEndpoint.class);
204 PolicyResolveRequest request =
205 mock(PolicyResolveRequest.class);
206 PolicyResolveRequest.Params mockParams =
207 mock(PolicyResolveRequest.Params.class);
209 when(request.getId()).thenReturn(TEST_MSG_ID);
210 when(request.valid()).thenReturn(true);
211 when(request.getMethod()).thenReturn(PolicyResolveRequest.RESOLVE_MESSAGE);
212 when(request.getParams()).thenReturn(mockParamsList);
213 when(mockParamsList.get(0)).thenReturn(mockParams);
214 when(mockParams.getPolicy_uri()).thenReturn(new Uri(TEST_POLICY));
215 when(mockEp.getIdentifier()).thenReturn(TEST_AGENT_ID);
217 policyManager.callback(mockEp, request);
219 verify(mockEp, timeout(TEST_TIMEOUT).times(2)).getIdentifier();