Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / renderers / opflex / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / PolicyManagerTest.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
3  *
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
7  *
8  *  Authors : Thomas Bachman
9  */
10
11 package org.opendaylight.groupbasedpolicy.renderer.opflex;
12
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;
18
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25
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;
57
58 import com.fasterxml.jackson.databind.JsonNode;
59 import com.google.common.util.concurrent.CheckedFuture;
60
61
62
63 /**
64  *
65  */
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";
71
72     @Mock
73     private JsonNode TEST_MSG_ID;
74     @Mock
75     private PolicyResolver mockResolver;
76     @Mock
77     private OpflexConnectionService mockConnService;
78     @Mock
79     private ListenerRegistration<DataChangeListener> mockListener;
80     @Mock
81     private ListenerRegistration<DataChangeListener> mockL3Listener;
82     @Mock
83     private EndpointDeclareRequest mockRpcMessage;
84     @Mock
85     private BindingAwareBroker.RpcRegistration<EndpointService> mockRpcRegistration;
86     @Mock
87     private WriteTransaction mockWriteTransaction;
88     @Mock
89     private CheckedFuture<Void, TransactionCommitFailedException> mockFuture;
90     @Mock
91     private PolicyInfo mockPolicyInfo;
92     @Mock
93     private OpflexAgent mockAgent;
94     @Mock
95     private PolicyScope mockScope;
96     @Mock
97     private List<PolicyResolveRequest.Params> mockParamsList;
98     @Mock
99     private MitLib mockOpflexLib;
100
101
102     private JsonRpcEndpoint dummyEndpoint;
103
104     private PolicyManager policyManager;
105     private ScheduledExecutorService executor;
106
107     @Override
108     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
109     }
110
111     @Before
112     public void setUp() throws Exception {
113         MockitoAnnotations.initMocks(this);
114         int numCPU = Runtime.getRuntime().availableProcessors();
115         executor = Executors.newScheduledThreadPool(numCPU * 2);
116         dummyEndpoint =
117                 new JsonRpcEndpoint(null, null,
118                         null, null, new RpcMessageMap(), null);
119         when(mockResolver
120                 .registerListener(Matchers.<PolicyListener>any())).thenReturn(mockScope);
121
122         policyManager = new PolicyManager(mockResolver,
123                 mockConnService, executor, mockOpflexLib);
124     }
125
126
127     //@Test
128     public void testPolicyUpdated() throws Exception {
129         EgKey sepgKey = mock(EgKey.class);
130         EgKey depgKey = mock(EgKey.class);
131         Policy mockPolicy = mock(Policy.class);
132
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);
139
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);
145
146         /*
147          * Add some EPGs to enable messaging
148          */
149         //policyManager.dirty.get().addEndpointGroup(sepgKey);
150         //policyManager.dirty.get().addEndpointGroup(depgKey);
151
152         /*
153          * Add a single agent
154          */
155         //policyManager.dirty.get().addAgent(TEST_AGENT_ID);
156
157         policyManager.policyUpdated(segKeySet);
158
159         verify(mockAgent, timeout(TEST_TIMEOUT)).getEndpoint();
160
161     }
162
163     //@Test
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);
170
171         Set<EgKey> egKeySet = Collections.
172                 newSetFromMap(new ConcurrentHashMap<EgKey, Boolean>());
173         egKeySet.add(depgKey);
174
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);
182
183         /*
184          * Add some EPGs to enable messaging
185          */
186         //policyManager.dirty.get().addEndpointGroup(sepgKey);
187         //policyManager.dirty.get().addEndpointGroup(depgKey);
188
189         /*
190          * Add a single agent
191          */
192         //policyManager.dirty.get().addAgent(TEST_AGENT_ID);
193
194         //policyManager.groupEndpointUpdated(sepgKey, epKey);
195
196         verify(mockAgent, timeout(TEST_TIMEOUT)).getEndpoint();
197
198     }
199
200
201     //@Test
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);
208
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);
216
217         policyManager.callback(mockEp,  request);
218
219         verify(mockEp, timeout(TEST_TIMEOUT).times(2)).getIdentifier();
220     }
221
222 }