fc2f5c733a47e30bb4bcb75a82d137296b4a3bb9
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / policyenforcer / 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.mapper.policyenforcer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
15 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
16 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.opendaylight.groupbasedpolicy.dto.ConditionGroup;
29 import org.opendaylight.groupbasedpolicy.dto.EgKey;
30 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
31 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.MockOfContext;
32 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.MockPolicyManager;
33 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
34 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.MockEndpointManager;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
38 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
39 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
40 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.MockSwitchManager;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchersBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchersBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
89 import org.powermock.api.mockito.PowerMockito;
90 import org.powermock.core.classloader.annotations.PrepareForTest;
91 import org.powermock.modules.junit4.PowerMockRunner;
92
93 import com.google.common.collect.ImmutableList;
94 import com.google.common.collect.ImmutableMap;
95
96 @RunWith(PowerMockRunner.class)
97 @PrepareForTest({PolicyManager.class})
98 public class PolicyEnforcerTest extends MapperUtilsTest {
99
100     //TODO (att: kblagov) XXX needs redesign
101     private final int sameEpgFlows = 1;
102     private final int allowTunnelFlows = 1;
103     private final int layer4flowsIPv4 = 1;
104     private final int layer4flowsIPv6 = 1;
105     private final int dropAllFlow = 1;
106     private final int arpFlows = 1;
107     private MockEndpointManager endpointManagerMock;
108     private MockPolicyManager policyManagerMock;
109     private MockSwitchManager switchManagerMock;
110     private MockOfContext ctxMock;
111
112     private NodeConnectorId tunnelId =
113             new NodeConnectorId(nodeId.getValue() + ":42");
114     @Before
115     public void setup() throws Exception {
116         PowerMockito.stub(PowerMockito.method(PolicyManager.class, "setSfcTableOffset")).toReturn(true);
117
118         endpointManagerMock = new MockEndpointManager();
119         policyManagerMock = new MockPolicyManager(endpointManagerMock);
120         switchManagerMock = new MockSwitchManager();
121         ctxMock = new MockOfContext(null, policyManagerMock, switchManagerMock, endpointManagerMock, null);
122         table = new PolicyEnforcer(ctxMock, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER());
123
124         switchManagerMock.addSwitch(
125                 nodeId,
126                 tunnelId,
127                 Collections.<NodeConnectorId>emptySet(),
128                 new OfOverlayNodeConfigBuilder().setTunnel(
129                         ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
130                             .setTunnelType(TunnelTypeVxlan.class)
131                             .setNodeConnectorId(tunnelId)
132                             .build())).build());
133     }
134
135     @Test
136     public void testSameEg() throws Exception {
137         Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
138                 .build();
139         endpointManagerMock.addEndpoint(ep1);
140         Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg, bd)
141                 .build();
142         endpointManagerMock.addEndpoint(ep2);
143         ctxMock.addTenant(baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy())
144             .setContract(ImmutableList.of(baseContract(null).build())).build()).build());
145
146         ofWriter = new OfWriter();
147         table.sync(ep1, ofWriter);
148         assertTrue(!ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
149             .getFlow()
150             .isEmpty());
151         int count = 0;
152         HashMap<String, Flow> flowMap = new HashMap<>();
153         for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
154             flowMap.put(f.getId().getValue(), f);
155             if (isAllowSameEpg(f)) {
156                 count += 1;
157             }
158         }
159         assertEquals(sameEpgFlows, count);
160         int totalFlows = sameEpgFlows + allowTunnelFlows + dropAllFlow;
161         assertEquals(totalFlows, ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
162             .getFlow()
163             .size());
164     }
165
166     @Test
167     public void testDifferentEg() throws Exception {
168         int totalFlows = sameEpgFlows + allowTunnelFlows;
169         assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(null).build())));
170         // one layer4 flow for each direction
171         totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + (2 * layer4flowsIPv6);
172         assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Bidirectional).build())));
173         totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6;
174         assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.In).build())));
175         assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Out).build())));
176     }
177
178     @Test
179     public void doTestRule() throws Exception {
180         Rule rule1 = new RuleBuilder().setActionRef(
181                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
182             .setClassifierRef(
183                     createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
184                             Direction.In)))
185             .build();
186         Rule rule2 = new RuleBuilder().setActionRef(
187                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
188             .setClassifierRef(
189                     createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
190                             Direction.Out)))
191             .build();
192         Rule rule3 = new RuleBuilder().setActionRef(
193                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
194             .setClassifierRef(
195                     createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
196                             Direction.Out, "ether_type", Direction.In)))
197             .build();
198         Rule rule4 = new RuleBuilder().setActionRef(
199                 ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
200             .setClassifierRef(
201                     createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_dst_90",
202                             Direction.In)))
203             .build();
204
205         int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6;
206         assertEquals(totalFlows,
207                 doTestDifferentEg(ImmutableList.of(createSubject("s1", ImmutableList.of(rule1)))));
208         // one layer4 flow for each direction
209         totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + (2 * layer4flowsIPv6);
210         assertEquals(totalFlows,
211                 doTestDifferentEg(ImmutableList.of(createSubject("s2", ImmutableList.of(rule2)))));
212          // only one ether_type for out direction
213         totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + layer4flowsIPv6;
214         assertEquals(totalFlows,
215                 doTestDifferentEg(ImmutableList.of(createSubject("s3", ImmutableList.of(rule3)))));
216         totalFlows = sameEpgFlows + allowTunnelFlows;
217         assertEquals(totalFlows,
218                 doTestDifferentEg(ImmutableList.of(createSubject("s4", ImmutableList.of(rule4)))));
219     }
220
221     private int doTestDifferentEg(List<Subject> subjects) throws Exception {
222         Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
223                 .build();
224         endpointManagerMock.addEndpoint(ep1);
225         Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg2, bd)
226                 .build();
227         endpointManagerMock.addEndpoint(ep2);
228         ctxMock.addTenant(baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy())
229             .setContract(ImmutableList.of(baseContract(subjects).build())).build()).build());
230
231         ofWriter = new OfWriter();
232         table.sync(ep1, ofWriter);
233         assertTrue(!ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
234             .getFlow()
235             .isEmpty());
236         int count = 0;
237         for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
238             if (isAllowSameEpg(f)) {
239                 count += 1;
240             } else if (f.getMatch() != null && Objects.equals(tunnelId, f.getMatch().getInPort())) {
241                 assertEquals(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))), f.getInstructions());
242                 count += 1;
243             } else if (f.getMatch() != null
244                     && f.getMatch().getEthernetMatch() != null
245                     && Objects.equals(FlowUtils.IPv4, f.getMatch()
246                         .getEthernetMatch()
247                         .getEthernetType()
248                         .getType()
249                         .getValue())
250                     && f.getMatch().getIpMatch() != null
251                     && Objects.equals((short) 6, f.getMatch().getIpMatch().getIpProtocol())
252                     && f.getMatch().getLayer4Match() != null
253                     && (Objects.equals(new PortNumber(80),
254                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
255                             new PortNumber(80),
256                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
257                 count += 1;
258             } else if (f.getMatch() != null
259                     && f.getMatch().getEthernetMatch() != null
260                     && Objects.equals(FlowUtils.IPv6, f.getMatch()
261                         .getEthernetMatch()
262                         .getEthernetType()
263                         .getType()
264                         .getValue())
265                     && f.getMatch().getIpMatch() != null
266                     && Objects.equals((short) 6, f.getMatch().getIpMatch().getIpProtocol())
267                     && f.getMatch().getLayer4Match() != null
268                     && (Objects.equals(new PortNumber(80),
269                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
270                             new PortNumber(80),
271                             ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
272                 count += 1;
273             }
274         }
275         return count;
276     }
277
278     @Test
279     public void testConditions() throws Exception {
280         Condition cond1 = new ConditionBuilder().setName(new ConditionName("cond1")).build();
281         Condition cond2 = new ConditionBuilder().setName(new ConditionName("cond2")).build();
282
283         Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
284                 .setCondition(ImmutableList.of(cond1.getName())).build();
285         endpointManagerMock.addEndpoint(ep1);
286         Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg2, bd)
287             .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))
288             .build();
289         endpointManagerMock.addEndpoint(ep2);
290
291         TenantBuilder tb = baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy()).setContract(
292                 ImmutableList.of(new ContractBuilder().setId(cid)
293                     .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))
294                     .setClause(
295                             ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
296                                 .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
297                                 .setConsumerMatchers(
298                                         new ConsumerMatchersBuilder().setConditionMatcher(
299                                                 ImmutableList.of(new ConditionMatcherBuilder().setName(
300                                                         new ConditionMatcherName("m1"))
301                                                     .setCondition(ImmutableList.of(cond1, cond2))
302                                                     .setMatchType(MatchType.Any)
303                                                     .build())).build())
304                                 .setProviderMatchers(
305                                         new ProviderMatchersBuilder().setConditionMatcher(
306                                                 ImmutableList.of(new ConditionMatcherBuilder().setName(
307                                                         new ConditionMatcherName("m2"))
308                                                     .setCondition(ImmutableList.of(cond1, cond2))
309                                                     .setMatchType(MatchType.All)
310                                                     .build())).build())
311                                 .build()))
312                     .build())).build());
313         ctxMock.addTenant(tb.build());
314
315         PolicyInfo policy = ctxMock.getCurrentPolicy();
316         List<ConditionName> ep1c = endpointManagerMock.getConditionsForEndpoint(ep1);
317         ConditionGroup cg1 = policy.getEgCondGroup(new EgKey(tb.getId(), ep1.getEndpointGroup()), ep1c);
318         List<ConditionName> ep2c = endpointManagerMock.getConditionsForEndpoint(ep2);
319         ConditionGroup cg2 = policy.getEgCondGroup(new EgKey(tb.getId(), ep2.getEndpointGroup()), ep2c);
320         int cg1Id = OrdinalFactory.getCondGroupOrdinal(cg1);
321         int cg2Id = OrdinalFactory.getCondGroupOrdinal(cg2);
322         int eg1Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep1.getEndpointGroup());
323         int eg2Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep2.getEndpointGroup());
324
325         assertNotEquals(cg1Id, cg2Id);
326
327         MatchBuilder mb = new MatchBuilder();
328         FlowUtils.addNxRegMatch(mb, RegMatch.of(NxmNxReg0.class, (long) eg1Id),
329                 RegMatch.of(NxmNxReg1.class, (long) cg1Id), RegMatch.of(NxmNxReg2.class, (long) eg2Id),
330                 RegMatch.of(NxmNxReg3.class, (long) cg2Id));
331         int count = 0;
332         ofWriter = new OfWriter();
333         table.sync(ep1, ofWriter);
334         // one layer4 flow for each direction
335         int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6 + arpFlows + dropAllFlow;;
336         assertEquals(totalFlows, ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
337             .getFlow()
338             .size());
339         HashMap<String, Flow> flowMap = new HashMap<>();
340         for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
341             flowMap.put(f.getId().getValue(), f);
342             if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null) {
343                 count++;
344             }
345         }
346         //flows with ether_type match
347         totalFlows = layer4flowsIPv4 + layer4flowsIPv6 + arpFlows;
348         assertEquals(totalFlows, count);
349     }
350
351     private boolean isAllowSameEpg(Flow flow) {
352         // flow has to have exactly 2 registers set, namely NxmNxReg0 and NxmNxReg2
353         // (these register values don't have to be equal)
354         boolean res = false;
355         if (flow != null && flow.getMatch() != null) {
356             GeneralAugMatchNodesNodeTableFlow genAug = flow.getMatch().getAugmentation(
357                     GeneralAugMatchNodesNodeTableFlow.class);
358             if (genAug != null) {
359                 List<ExtensionList> extensions = genAug.getExtensionList();
360                 if (extensions != null && extensions.size() == 2) {
361                     Long reg0 = null;
362                     Long reg2 = null;
363                     for (ExtensionList extensionList : extensions) {
364                         Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();
365                         Extension extension = extensionList.getExtension();
366                         if (extensionKey != null && extension != null) {
367                             NxAugMatchNodesNodeTableFlow nxAugMatch = extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
368                             if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {
369                                 if (extensionKey.equals(NxmNxReg0Key.class)) {
370                                     reg0 = nxAugMatch.getNxmNxReg().getValue();
371                                 } else if (extensionKey.equals(NxmNxReg2Key.class)) {
372                                     reg2 = nxAugMatch.getNxmNxReg().getValue();
373                                 }
374                             }
375                         }
376                     }
377                     if (reg0 != null && reg2 != null) {
378                         res = true;
379                     }
380                 }
381             }
382         }
383         return res;
384     }
385
386     protected ContractBuilder baseContract(List<Subject> subjects) {
387         ContractBuilder contractBuilder = new ContractBuilder().setId(cid).setSubject(subjects);
388         // TODO refactor
389         if (subjects == null) {
390             return contractBuilder.setClause(ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
391                 .setSubjectRefs(ImmutableList.<SubjectName>of(new SubjectName("s1")))
392                 .build()));
393         }
394         List<SubjectName> subjectNames = new ArrayList<>();
395         for (Subject subject : subjects) {
396             subjectNames.add(subject.getName());
397         }
398         return contractBuilder.setClause(ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
399             .setSubjectRefs(subjectNames)
400             .build()));
401     }
402
403     protected SubjectBuilder baseSubject(Direction direction) {
404         return new SubjectBuilder()
405             .setName(new SubjectName("s1"))
406             .setRule(ImmutableList.of(new RuleBuilder()
407                 .setActionRef(ImmutableList.of(new ActionRefBuilder()
408                     .setName(new ActionName("allow"))
409                     .build()))
410                 .setClassifierRef(ImmutableList.of(new ClassifierRefBuilder()
411                     .setName(new ClassifierName("tcp_dst_80"))
412                     .setDirection(direction)
413                     .setInstanceName(new ClassifierName("tcp_dst_80"))
414                     .build()))
415                 .build()));
416     }
417
418     protected Subject createSubject(String name, List<Rule> rules){
419         return new SubjectBuilder().setName(new SubjectName(name)).setRule(rules).build();
420     }
421
422     protected List<ClassifierRef> createClassifierRefs(Map<String, Direction> refNamesAndDirections) {
423         List<ClassifierRef> refs = new ArrayList<>();
424         for (String refName : refNamesAndDirections.keySet()) {
425             refs.add(new ClassifierRefBuilder().setName(new ClassifierName(refName))
426                 .setDirection(refNamesAndDirections.get(refName))
427                 .setInstanceName(new ClassifierName(refName))
428                 .build());
429         }
430         return refs;
431     }
432 }