ae9c27da043bd4b97c53b521075a2ec491cfbeb0
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / PolicyEnforcerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
17 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
18
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.Set;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
31 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
32 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.PolicyEnforcer.PolicyPair;
33 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
34 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
35 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84
85 import com.google.common.collect.ImmutableList;
86 import com.google.common.collect.ImmutableMap;
87
88 public class PolicyEnforcerTest extends FlowTableTest {
89
90     protected static final Logger LOG = LoggerFactory.getLogger(PolicyEnforcerTest.class);
91
92     @Override
93     @Before
94     public void setup() throws Exception {
95         initCtx();
96         table = new PolicyEnforcer(ctx, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER());
97         super.setup();
98
99         switchManager.addSwitch(
100                 nodeId,
101                 tunnelId,
102                 Collections.<NodeConnectorId>emptySet(),
103                 new OfOverlayNodeConfigBuilder().setTunnel(
104                         ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
105                             .setTunnelType(TunnelTypeVxlan.class)
106                             .setNodeConnectorId(tunnelId)
107                             .build())).build());
108     }
109
110     @Test
111     public void testNoEps() throws Exception {
112         FlowMap fm = dosync(null);
113         assertEquals(2, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
114             .getFlow()
115             .size());
116     }
117
118     @Test
119     public void testSameEg() throws Exception {
120         Endpoint ep1 = localEP().build();
121         endpointManager.addEndpoint(ep1);
122         Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02")).build();
123         endpointManager.addEndpoint(ep2);
124         policyResolver.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(null).build()))
125             .build());
126
127         FlowMap fm = dosync(null);
128         assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
129             .getFlow()
130             .size());
131         int count = 0;
132         HashMap<String, Flow> flowMap = new HashMap<>();
133         for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
134             flowMap.put(f.getId().getValue(), f);
135             if (isAllowSameEpg(f)) {
136                 count += 1;
137             }
138         }
139         assertEquals(1, count);
140         assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
141             .getFlow()
142             .size());
143         fm = dosync(flowMap);
144         assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
145             .getFlow()
146             .size());
147     }
148
149     @Test
150     public void testDifferentEg() throws Exception {
151         assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(null).build())));
152         assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Bidirectional).build())));
153         assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.In).build())));
154         assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Out).build())));
155     }
156
157     @Test
158     public void doTestRule() throws Exception {
159         Rule rule1 = new RuleBuilder().setActionRef(
160                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
161             .setClassifierRef(
162                     createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
163                             Direction.In)))
164             .build();
165         Rule rule2 = new RuleBuilder().setActionRef(
166                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
167             .setClassifierRef(
168                     createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
169                             Direction.Out)))
170             .build();
171         Rule rule3 = new RuleBuilder().setActionRef(
172                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
173             .setClassifierRef(
174                     createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
175                             Direction.Out, "ether_type", Direction.In)))
176             .build();
177         Rule rule4 = new RuleBuilder().setActionRef(
178                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
179             .setClassifierRef(
180                     createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_dst_90",
181                             Direction.In)))
182             .build();
183
184         assertEquals(5,
185                 doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s1", ImmutableList.<Rule>of(rule1)))));
186         assertEquals(7,
187                 doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s2", ImmutableList.<Rule>of(rule2)))));
188         assertEquals(6,
189                 doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s3", ImmutableList.<Rule>of(rule3)))));
190         assertEquals(3,
191                 doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s4", ImmutableList.<Rule>of(rule4)))));
192     }
193
194     private int doTestDifferentEg(List<Subject> subjects) throws Exception {
195         Endpoint ep1 = localEP().build();
196         endpointManager.addEndpoint(ep1);
197         Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02")).setEndpointGroup(eg2).build();
198         endpointManager.addEndpoint(ep2);
199         policyResolver.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(subjects).build()))
200             .build());
201
202         FlowMap fm = dosync(null);
203         assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
204             .getFlow()
205             .size());
206         int count = 0;
207         HashMap<String, Flow> flowMap = new HashMap<>();
208         for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
209             flowMap.put(f.getId().getValue(), f);
210             if (isAllowSameEpg(f)) {
211                 count += 1;
212             } else if (f.getMatch() != null && Objects.equals(tunnelId, f.getMatch().getInPort())) {
213                 assertEquals(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))), f.getInstructions());
214                 count += 1;
215             } else if (f.getMatch() != null
216                     && f.getMatch().getEthernetMatch() != null
217                     && Objects.equals(FlowUtils.IPv4, f.getMatch()
218                         .getEthernetMatch()
219                         .getEthernetType()
220                         .getType()
221                         .getValue())
222                     && f.getMatch().getIpMatch() != null
223                     && Objects.equals(Short.valueOf((short) 6), f.getMatch().getIpMatch().getIpProtocol())
224                     && f.getMatch().getLayer4Match() != null
225                     && (Objects.equals(new PortNumber(Integer.valueOf(80)),
226                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
227                             new PortNumber(Integer.valueOf(80)),
228                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
229                 count += 1;
230             } else if (f.getMatch() != null
231                     && f.getMatch().getEthernetMatch() != null
232                     && Objects.equals(FlowUtils.IPv6, f.getMatch()
233                         .getEthernetMatch()
234                         .getEthernetType()
235                         .getType()
236                         .getValue())
237                     && f.getMatch().getIpMatch() != null
238                     && Objects.equals(Short.valueOf((short) 6), f.getMatch().getIpMatch().getIpProtocol())
239                     && f.getMatch().getLayer4Match() != null
240                     && (Objects.equals(new PortNumber(Integer.valueOf(80)),
241                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
242                             new PortNumber(Integer.valueOf(80)),
243                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
244                 count += 1;
245             }
246         }
247         return count;
248     }
249
250     @Test
251     public void testConditions() throws Exception {
252         Condition cond1 = new ConditionBuilder().setName(new ConditionName("cond1")).build();
253         Condition cond2 = new ConditionBuilder().setName(new ConditionName("cond2")).build();
254
255         Endpoint ep1 = localEP().setCondition(ImmutableList.of(cond1.getName())).build();
256         endpointManager.addEndpoint(ep1);
257         Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02"))
258             .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))
259             .setEndpointGroup(eg2)
260             .build();
261         endpointManager.addEndpoint(ep2);
262
263         TenantBuilder tb = baseTenant().setContract(
264                 ImmutableList.of(new ContractBuilder().setId(cid)
265                     .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))
266                     .setClause(
267                             ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
268                                 .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
269                                 .setConsumerMatchers(
270                                         new ConsumerMatchersBuilder().setConditionMatcher(
271                                                 ImmutableList.of(new ConditionMatcherBuilder().setName(
272                                                         new ConditionMatcherName("m1"))
273                                                     .setCondition(ImmutableList.of(cond1, cond2))
274                                                     .setMatchType(MatchType.Any)
275                                                     .build())).build())
276                                 .setProviderMatchers(
277                                         new ProviderMatchersBuilder().setConditionMatcher(
278                                                 ImmutableList.of(new ConditionMatcherBuilder().setName(
279                                                         new ConditionMatcherName("m2"))
280                                                     .setCondition(ImmutableList.of(cond1, cond2))
281                                                     .setMatchType(MatchType.All)
282                                                     .build())).build())
283                                 .build()))
284                     .build()));
285         policyResolver.addTenant(tb.build());
286
287         PolicyInfo policy = policyResolver.getCurrentPolicy();
288         List<ConditionName> ep1c = endpointManager.getCondsForEndpoint(ep1);
289         ConditionGroup cg1 = policy.getEgCondGroup(new EgKey(tb.getId(), ep1.getEndpointGroup()), ep1c);
290         List<ConditionName> ep2c = endpointManager.getCondsForEndpoint(ep2);
291         ConditionGroup cg2 = policy.getEgCondGroup(new EgKey(tb.getId(), ep2.getEndpointGroup()), ep2c);
292         int cg1Id = OrdinalFactory.getCondGroupOrdinal(cg1);
293         int cg2Id = OrdinalFactory.getCondGroupOrdinal(cg2);
294         int eg1Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep1.getEndpointGroup());
295         int eg2Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep2.getEndpointGroup());
296
297         assertNotEquals(cg1Id, cg2Id);
298
299         MatchBuilder mb = new MatchBuilder();
300         FlowUtils.addNxRegMatch(mb, RegMatch.of(NxmNxReg0.class, Long.valueOf(eg1Id)),
301                 RegMatch.of(NxmNxReg1.class, Long.valueOf(cg1Id)), RegMatch.of(NxmNxReg2.class, Long.valueOf(eg2Id)),
302                 RegMatch.of(NxmNxReg3.class, Long.valueOf(cg2Id)));
303         GeneralAugMatchNodesNodeTableFlow m1 = mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
304         FlowUtils.addNxRegMatch(mb, RegMatch.of(NxmNxReg0.class, Long.valueOf(eg2Id)),
305                 RegMatch.of(NxmNxReg1.class, Long.valueOf(cg2Id)), RegMatch.of(NxmNxReg2.class, Long.valueOf(eg1Id)),
306                 RegMatch.of(NxmNxReg3.class, Long.valueOf(cg1Id)));
307         GeneralAugMatchNodesNodeTableFlow m2 = mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
308         int count = 0;
309         FlowMap fm = dosync(null);
310         assertEquals(7, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
311             .getFlow()
312             .size());
313         HashMap<String, Flow> flowMap = new HashMap<>();
314         for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
315             flowMap.put(f.getId().getValue(), f);
316             if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null) {
317                 count++;
318             }
319         }
320         assertEquals(3, count);
321         fm = dosync(flowMap);
322         int numberOfFlows = fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
323             .getFlow()
324             .size();
325         fm = dosync(flowMap);
326         assertEquals(numberOfFlows, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
327             .getFlow()
328             .size());
329     }
330
331     private boolean isAllowSameEpg(Flow flow) {
332         // flow has to have exactly 2 registers set, namely NxmNxReg0 and NxmNxReg2
333         // (these register values don't have to be equal)
334         boolean res = false;
335         if (flow != null && flow.getMatch() != null) {
336             GeneralAugMatchNodesNodeTableFlow genAug = flow.getMatch().getAugmentation(
337                     GeneralAugMatchNodesNodeTableFlow.class);
338             if (genAug != null) {
339                 List<ExtensionList> extensions = genAug.getExtensionList();
340                 if (extensions != null && extensions.size() == 2) {
341                     Long reg0 = null;
342                     Long reg2 = null;
343                     for (ExtensionList extensionList : extensions) {
344                         Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();
345                         Extension extension = extensionList.getExtension();
346                         if (extensionKey != null && extension != null) {
347                             NxAugMatchNodesNodeTableFlow nxAugMatch = extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
348                             if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {
349                                 if (extensionKey.equals(NxmNxReg0Key.class)) {
350                                     reg0 = nxAugMatch.getNxmNxReg().getValue();
351                                 } else if (extensionKey.equals(NxmNxReg2Key.class)) {
352                                     reg2 = nxAugMatch.getNxmNxReg().getValue();
353                                 }
354                             }
355                         }
356                     }
357                     if (reg0 != null && reg2 != null) {
358                         res = true;
359                     }
360                 }
361             }
362         }
363         return res;
364     }
365
366     PolicyPair policyPair;
367     int consumerEpgId;
368     int providerEpgId;
369     int consumerCondGrpId;
370     int providerCondGrpId;
371     Set<IpPrefix> consumerEicIpPrefixes;
372     Set<IpPrefix> providerEicIpPrefixes;
373     IpPrefix consumerEicIp;
374     IpPrefix providerEicIp;
375     NodeId consumerEpNodeId;
376     NodeId providerEpNodeId;
377
378     @Before
379     public void PolicyPairInitialisation() {
380         consumerEpgId = 5;
381         providerEpgId = 8;
382         consumerCondGrpId = 5;
383         providerCondGrpId = 8;
384         consumerEicIp = mock(IpPrefix.class);
385         providerEicIp = mock(IpPrefix.class);
386         consumerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(consumerEicIp));
387         providerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(providerEicIp));
388         consumerEpNodeId = mock(NodeId.class);
389         when(consumerEpNodeId.getValue()).thenReturn("consumerValue");
390         providerEpNodeId = mock(NodeId.class);
391         when(providerEpNodeId.getValue()).thenReturn("providerValue");
392     }
393
394     @Test
395     public void PolicyPairConstructorTest() {
396         policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
397                 consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
398         Assert.assertEquals(consumerEpgId, policyPair.getConsumerEpgId());
399         Assert.assertEquals(providerEpgId, policyPair.getProviderEpgId());
400         Assert.assertEquals(consumerEpNodeId, policyPair.getConsumerEpNodeId());
401         Assert.assertEquals(providerEpNodeId, policyPair.getProviderEpNodeId());
402         Assert.assertNotNull(policyPair.toString());
403     }
404
405     @Test
406     public void PolicyPairEqualsTest() {
407         policyPair = new PolicyPair(0, 0, 0, 0, null, null, null, null);
408         PolicyPair other;
409         other = new PolicyPair(0, 0, 0, 0, null, null, null, null);
410         Assert.assertTrue(policyPair.equals(other));
411
412         policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
413                 consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
414         Assert.assertTrue(policyPair.equals(policyPair));
415         Assert.assertFalse(policyPair.equals(null));
416         Assert.assertFalse(policyPair.equals(new Object()));
417
418         Assert.assertFalse(other.equals(policyPair));
419         Assert.assertFalse(policyPair.equals(other));
420
421         other = new PolicyPair(0, 0, 0, 0, null, providerEicIpPrefixes, null, null);
422         Assert.assertFalse(other.equals(policyPair));
423         Assert.assertFalse(policyPair.equals(other));
424
425         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, null, null);
426         Assert.assertFalse(other.equals(policyPair));
427
428         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, providerEpNodeId, null);
429         Assert.assertFalse(policyPair.equals(other));
430
431         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, null);
432         Assert.assertFalse(other.equals(policyPair));
433
434         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
435                 consumerEpNodeId);
436         Assert.assertFalse(policyPair.equals(other));
437
438         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
439                 providerEpNodeId);
440         Assert.assertFalse(other.equals(policyPair));
441         Assert.assertFalse(policyPair.equals(other));
442
443         other = new PolicyPair(0, 0, 0, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
444                 providerEpNodeId);
445         Assert.assertFalse(other.equals(policyPair));
446         Assert.assertFalse(policyPair.equals(other));
447
448         other = new PolicyPair(0, 8, 0, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
449                 providerEpNodeId);
450         Assert.assertFalse(other.equals(policyPair));
451         Assert.assertFalse(policyPair.equals(other));
452
453         other = new PolicyPair(0, 8, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
454                 providerEpNodeId);
455         Assert.assertFalse(other.equals(policyPair));
456         Assert.assertFalse(policyPair.equals(other));
457
458         other = new PolicyPair(5, 8, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
459                 providerEpNodeId);
460         Assert.assertTrue(policyPair.equals(other));
461     }
462 }