583762cc04f7cdbd37df69959de6853d5a00b868
[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.dto.ConditionGroup;
31 import org.opendaylight.groupbasedpolicy.dto.EgKey;
32 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
33 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
34 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.PolicyEnforcer.PolicyPair;
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         OfWriter 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         ctx.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(null).build()))
125             .build());
126
127         OfWriter 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         ctx.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(subjects).build()))
200             .build());
201
202         OfWriter 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         ctx.addTenant(tb.build());
286
287         PolicyInfo policy = ctx.getCurrentPolicy();
288         List<ConditionName> ep1c = endpointManager.getConditionsForEndpoint(ep1);
289         ConditionGroup cg1 = policy.getEgCondGroup(new EgKey(tb.getId(), ep1.getEndpointGroup()), ep1c);
290         List<ConditionName> ep2c = endpointManager.getConditionsForEndpoint(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         int count = 0;
304         OfWriter fm = dosync(null);
305         assertEquals(7, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
306             .getFlow()
307             .size());
308         HashMap<String, Flow> flowMap = new HashMap<>();
309         for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
310             flowMap.put(f.getId().getValue(), f);
311             if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null) {
312                 count++;
313             }
314         }
315         assertEquals(3, count);
316         fm = dosync(flowMap);
317         int numberOfFlows = fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
318             .getFlow()
319             .size();
320         fm = dosync(flowMap);
321         assertEquals(numberOfFlows, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
322             .getFlow()
323             .size());
324     }
325
326     private boolean isAllowSameEpg(Flow flow) {
327         // flow has to have exactly 2 registers set, namely NxmNxReg0 and NxmNxReg2
328         // (these register values don't have to be equal)
329         boolean res = false;
330         if (flow != null && flow.getMatch() != null) {
331             GeneralAugMatchNodesNodeTableFlow genAug = flow.getMatch().getAugmentation(
332                     GeneralAugMatchNodesNodeTableFlow.class);
333             if (genAug != null) {
334                 List<ExtensionList> extensions = genAug.getExtensionList();
335                 if (extensions != null && extensions.size() == 2) {
336                     Long reg0 = null;
337                     Long reg2 = null;
338                     for (ExtensionList extensionList : extensions) {
339                         Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();
340                         Extension extension = extensionList.getExtension();
341                         if (extensionKey != null && extension != null) {
342                             NxAugMatchNodesNodeTableFlow nxAugMatch = extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
343                             if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {
344                                 if (extensionKey.equals(NxmNxReg0Key.class)) {
345                                     reg0 = nxAugMatch.getNxmNxReg().getValue();
346                                 } else if (extensionKey.equals(NxmNxReg2Key.class)) {
347                                     reg2 = nxAugMatch.getNxmNxReg().getValue();
348                                 }
349                             }
350                         }
351                     }
352                     if (reg0 != null && reg2 != null) {
353                         res = true;
354                     }
355                 }
356             }
357         }
358         return res;
359     }
360
361     PolicyPair policyPair;
362     int consumerEpgId;
363     int providerEpgId;
364     int consumerCondGrpId;
365     int providerCondGrpId;
366     Set<IpPrefix> consumerEicIpPrefixes;
367     Set<IpPrefix> providerEicIpPrefixes;
368     IpPrefix consumerEicIp;
369     IpPrefix providerEicIp;
370     NodeId consumerEpNodeId;
371     NodeId providerEpNodeId;
372
373     @Before
374     public void PolicyPairInitialisation() {
375         consumerEpgId = 5;
376         providerEpgId = 8;
377         consumerCondGrpId = 5;
378         providerCondGrpId = 8;
379         consumerEicIp = mock(IpPrefix.class);
380         providerEicIp = mock(IpPrefix.class);
381         consumerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(consumerEicIp));
382         providerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(providerEicIp));
383         consumerEpNodeId = mock(NodeId.class);
384         when(consumerEpNodeId.getValue()).thenReturn("consumerValue");
385         providerEpNodeId = mock(NodeId.class);
386         when(providerEpNodeId.getValue()).thenReturn("providerValue");
387     }
388
389     @Test
390     public void PolicyPairConstructorTest() {
391         policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
392                 consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
393         Assert.assertEquals(consumerEpgId, policyPair.getConsumerEpgId());
394         Assert.assertEquals(providerEpgId, policyPair.getProviderEpgId());
395         Assert.assertEquals(consumerEpNodeId, policyPair.getConsumerEpNodeId());
396         Assert.assertEquals(providerEpNodeId, policyPair.getProviderEpNodeId());
397         Assert.assertNotNull(policyPair.toString());
398     }
399
400     @Test
401     public void PolicyPairEqualsTest() {
402         policyPair = new PolicyPair(0, 0, 0, 0, null, null, null, null);
403         PolicyPair other;
404         other = new PolicyPair(0, 0, 0, 0, null, null, null, null);
405         Assert.assertTrue(policyPair.equals(other));
406
407         policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
408                 consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
409         Assert.assertTrue(policyPair.equals(policyPair));
410         Assert.assertFalse(policyPair.equals(null));
411         Assert.assertFalse(policyPair.equals(new Object()));
412
413         Assert.assertFalse(other.equals(policyPair));
414         Assert.assertFalse(policyPair.equals(other));
415
416         other = new PolicyPair(0, 0, 0, 0, null, providerEicIpPrefixes, null, null);
417         Assert.assertFalse(other.equals(policyPair));
418         Assert.assertFalse(policyPair.equals(other));
419
420         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, null, null);
421         Assert.assertFalse(other.equals(policyPair));
422
423         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, providerEpNodeId, null);
424         Assert.assertFalse(policyPair.equals(other));
425
426         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, null);
427         Assert.assertFalse(other.equals(policyPair));
428
429         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
430                 consumerEpNodeId);
431         Assert.assertFalse(policyPair.equals(other));
432
433         other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
434                 providerEpNodeId);
435         Assert.assertFalse(other.equals(policyPair));
436         Assert.assertFalse(policyPair.equals(other));
437
438         other = new PolicyPair(0, 0, 0, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
439                 providerEpNodeId);
440         Assert.assertFalse(other.equals(policyPair));
441         Assert.assertFalse(policyPair.equals(other));
442
443         other = new PolicyPair(0, 8, 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, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
449                 providerEpNodeId);
450         Assert.assertFalse(other.equals(policyPair));
451         Assert.assertFalse(policyPair.equals(other));
452
453         other = new PolicyPair(5, 8, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
454                 providerEpNodeId);
455         Assert.assertTrue(policyPair.equals(other));
456     }
457 }