BUG-5785 Support for dissemination of L3VPN flow speci I
[bgpcep.git] / bgp / flowspec / src / test / java / org / opendaylight / protocol / bgp / flowspec / SimpleFlowspecIpv4NlriParserTest.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 package org.opendaylight.protocol.bgp.flowspec;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractNumericOperandParser;
25 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractOperandParser;
26 import org.opendaylight.protocol.bgp.flowspec.handlers.BitmaskOperandParser;
27 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
28 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
29 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.BitmaskOperand;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.FlowspecSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Fragment;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.Flowspec;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.FlowspecBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DestinationPortCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DscpCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.FragmentCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpCodeCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpTypeCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PacketLengthCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PacketLengthCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PortCase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PortCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.SourcePortCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.TcpFlagsCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.TcpFlagsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.destination.port._case.DestinationPorts;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.destination.port._case.DestinationPortsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.dscp._case.Dscps;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.dscp._case.DscpsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.fragment._case.Fragments;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.fragment._case.FragmentsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.code._case.Codes;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.code._case.CodesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.type._case.Types;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.type._case.TypesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengths;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengthsBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.port._case.Ports;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.port._case.PortsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePorts;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePortsBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlags;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlagsBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCaseBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIps;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIpsBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.DestinationFlowspecBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv4.route.FlowspecRoute;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
90 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
91 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
92 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
93 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
94
95 public class SimpleFlowspecIpv4NlriParserTest {
96
97     static final PathId PATH_ID = new PathId(1L);
98     @Mock
99     private PeerSpecificParserConstraint constraint;
100     @Mock
101     private MultiPathSupport muliPathSupport;
102
103     private static final byte[] REACHED_NLRI = new byte[]{
104         0x21,   // NLRI length: 33
105         01, 0x20, 0x0a, 00, 01, 00,
106         02, 0x20, 01, 02, 03, 04,
107         03, (byte) 0x81, 06,
108         04, 03, (byte) 0x89, 0x45, (byte) 0x8b, (byte) 0x91, 0x1f, (byte) 0x90,
109         05, 0x12, 0x0f, (byte) 0xf9, (byte) 0x81, (byte) 0xb3,
110         06, (byte) 0x91, 0x1f, (byte) 0x90
111     };
112
113     private static final byte[] REACHED_NLRI_ADD_PATH = new byte[]{
114         0x0, 0x0, 0x0, 0x1,
115         0x21,   // NLRI length: 33
116         01, 0x20, 0x0a, 00, 01, 00,
117         02, 0x20, 01, 02, 03, 04,
118         03, (byte) 0x81, 06,
119         04, 03, (byte) 0x89, 0x45, (byte) 0x8b, (byte) 0x91, 0x1f, (byte) 0x90,
120         05, 0x12, 0x0f, (byte) 0xf9, (byte) 0x81, (byte) 0xb3,
121         06, (byte) 0x91, 0x1f, (byte) 0x90
122     };
123
124     private static final byte[] UNREACHED_NLRI = new byte[]{
125         0x1B,   // NLRI length: 27
126         07, 4, 2, (byte) 0x84, 3,
127         0x08, 4, 04, (byte) 0x80, 05,
128         0x09, 0x12, 04, 01, (byte) 0x91, 0x56, (byte) 0xb1,
129         0x0a, (byte) 0x94, (byte) 0xde, (byte) 0xad,
130         0x0b, (byte) 0x82, 0x2a,
131         0x0c, (byte) 0x81, (byte) 0x0e
132     };
133
134     private static final byte[] UNREACHED_NLRI_ADD_PATH = new byte[]{
135         0x0, 0x0, 0x0, 0x1,
136         0x1B,   // NLRI length: 27
137         07, 4, 2, (byte) 0x84, 3,
138         0x08, 4, 04, (byte) 0x80, 05,
139         0x09, 0x12, 04, 01, (byte) 0x91, 0x56, (byte) 0xb1,
140         0x0a, (byte) 0x94, (byte) 0xde, (byte) 0xad,
141         0x0b, (byte) 0x82, 0x2a,
142         0x0c, (byte) 0x81, (byte) 0x0e
143     };
144
145     private final SimpleFlowspecExtensionProviderContext flowspecContext = new SimpleFlowspecExtensionProviderContext();
146     private final FlowspecActivator fsa = new FlowspecActivator(flowspecContext);
147     private final SimpleFlowspecIpv4NlriParser FS_PARSER = new SimpleFlowspecIpv4NlriParser(flowspecContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
148
149     @Before
150     public void setUp() {
151         MockitoAnnotations.initMocks(this);
152         Mockito.doReturn(Optional.of(this.muliPathSupport)).when(constraint).getPeerConstraint(Mockito.any());
153         Mockito.doReturn(true).when(this.muliPathSupport).isTableTypeSupported(Mockito.any());
154     }
155
156     @Test
157     public void testParseMpReachNlri() throws BGPParsingException {
158         final List<Flowspec> fs = new ArrayList<>();
159         final MpReachNlriBuilder mp = new MpReachNlriBuilder();
160
161         final FlowspecBuilder builder = new FlowspecBuilder();
162         final DestinationPrefixCase destinationPrefix = new DestinationPrefixCaseBuilder().setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
163         builder.setFlowspecType(destinationPrefix);
164         fs.add(builder.build());
165         final SourcePrefixCase sourcePrefix = new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix("1.2.3.4/32")).build();
166         builder.setFlowspecType(sourcePrefix);
167         fs.add(builder.build());
168
169         final FlowspecType prots = createProts();
170         builder.setFlowspecType(prots);
171         fs.add(builder.build());
172
173         final PortCase ps = createPorts();
174         builder.setFlowspecType(ps);
175         fs.add(builder.build());
176
177         final FlowspecType dps = createDps();
178         builder.setFlowspecType(dps);
179         fs.add(builder.build());
180
181         final FlowspecType sps = createSps();
182         builder.setFlowspecType(sps);
183         fs.add(builder.build());
184
185         mp.setAdvertizedRoutes(
186                 new AdvertizedRoutesBuilder().setDestinationType(
187                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder()
188                                 .setDestinationFlowspec(
189                                         new DestinationFlowspecBuilder()
190                                                 .setFlowspec(fs)
191                                                 .build()
192                                 ).build()
193                 ).build()
194         );
195
196         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(flowspecContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
197
198         final MpReachNlriBuilder result = new MpReachNlriBuilder();
199         result.setAfi(Ipv4AddressFamily.class);
200         result.setSafi(FlowspecSubsequentAddressFamily.class);
201         parser.parseNlri(Unpooled.wrappedBuffer(REACHED_NLRI), result);
202
203         final List<Flowspec> flows = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase) (result.getAdvertizedRoutes().getDestinationType()))
204                 .getDestinationFlowspec()
205                 .getFlowspec();
206         testFlows(flows, destinationPrefix, sourcePrefix, prots, prots, ps, dps, sps);
207
208         final ByteBuf buffer = Unpooled.buffer();
209         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mp.setAfi(Ipv4AddressFamily.class).build()).build()).build(), buffer);
210         assertArrayEquals(REACHED_NLRI, ByteArray.readAllBytes(buffer));
211
212         assertEquals("all packets to 10.0.1.0/32 AND from 1.2.3.4/32 AND where IP protocol equals to 6 AND where port is greater than or equals to 137 and is less than or equals to 139 or equals to 8080 AND where destination port is greater than 4089 or equals to 179 AND where source port equals to 8080 ", FS_PARSER.stringNlri(flows));
213     }
214
215     private void testFlows(final List<Flowspec> flows, final DestinationPrefixCase destinationPrefix, final SourcePrefixCase sourcePrefix, final FlowspecType prots, final FlowspecType flowspecType, final PortCase ps, final FlowspecType dps, final FlowspecType sps) {
216         assertEquals(6, flows.size());
217         assertEquals(destinationPrefix, flows.get(0).getFlowspecType());
218         assertEquals(sourcePrefix, flows.get(1).getFlowspecType());
219         assertEquals(prots, flows.get(2).getFlowspecType());
220         assertEquals(ps, flows.get(3).getFlowspecType());
221         assertEquals(dps, flows.get(4).getFlowspecType());
222         assertEquals(sps, flows.get(5).getFlowspecType());
223     }
224
225     private FlowspecType createSps() {
226         final List<SourcePorts> sports = Lists.newArrayList(new SourcePortsBuilder().setOp(new NumericOperand(false, true, true, false, false)).setValue(8080).build());
227         return new SourcePortCaseBuilder().setSourcePorts(sports).build();
228     }
229
230     private FlowspecType createProts() {
231         final List<ProtocolIps> protocols = Lists.newArrayList(new ProtocolIpsBuilder().setOp(new NumericOperand(false, true, true, false, false)).setValue((short) 6).build());
232         return new ProtocolIpCaseBuilder().setProtocolIps(protocols).build();
233     }
234
235     private FlowspecType createDps() {
236         final List<DestinationPorts> destports = Lists.newArrayList(new DestinationPortsBuilder().setOp(new NumericOperand(false, false, false, true, false)).setValue(4089).build(),
237                 new DestinationPortsBuilder().setOp(new NumericOperand(false, true, true, false, false)).setValue(179).build());
238         return new DestinationPortCaseBuilder().setDestinationPorts(destports).build();
239     }
240
241     @Test
242     public void testParseMpReachNlriConstraint() throws BGPParsingException {
243         final List<Flowspec> fs = new ArrayList<>();
244         final MpReachNlriBuilder mp = new MpReachNlriBuilder();
245
246         final FlowspecBuilder builder = new FlowspecBuilder();
247         final DestinationPrefixCase destinationPrefix = new DestinationPrefixCaseBuilder().setDestinationPrefix(new Ipv4Prefix("10.0.1.0/32")).build();
248         builder.setFlowspecType(destinationPrefix);
249         fs.add(builder.build());
250         final SourcePrefixCase sourcePrefix = new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix("1.2.3.4/32")).build();
251         builder.setFlowspecType(sourcePrefix);
252         fs.add(builder.build());
253
254         final FlowspecType prots = createProts();
255         builder.setFlowspecType(prots);
256         fs.add(builder.build());
257
258         final PortCase ps = createPorts();
259         builder.setFlowspecType(ps);
260         fs.add(builder.build());
261
262         final FlowspecType dps = createDps();
263         builder.setFlowspecType(dps);
264         fs.add(builder.build());
265
266         final FlowspecType sps = createSps();
267         builder.setFlowspecType(sps);
268         fs.add(builder.build());
269
270         mp.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
271                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCaseBuilder()
272                         .setDestinationFlowspec(
273                                 new DestinationFlowspecBuilder()
274                                         .setPathId(PATH_ID)
275                                         .setFlowspec(fs)
276                                         .build()
277                         ).build()
278                 ).build()
279         );
280
281         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(flowspecContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
282
283         final MpReachNlriBuilder result = new MpReachNlriBuilder();
284         result.setAfi(Ipv4AddressFamily.class);
285         result.setSafi(FlowspecSubsequentAddressFamily.class);
286         parser.parseNlri(Unpooled.wrappedBuffer(REACHED_NLRI_ADD_PATH), result, this.constraint);
287
288         final List<Flowspec> flows = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecCase) (result.getAdvertizedRoutes().getDestinationType()))
289                 .getDestinationFlowspec()
290                 .getFlowspec();
291         testFlows(flows, destinationPrefix, sourcePrefix, prots, prots, ps, dps, sps);
292
293         final ByteBuf buffer = Unpooled.buffer();
294         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mp.setAfi(Ipv4AddressFamily.class).build()).build()).build(), buffer);
295         assertArrayEquals(REACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
296
297         assertEquals("all packets to 10.0.1.0/32 AND from 1.2.3.4/32 AND where IP protocol equals to 6 AND where port is greater than or equals to 137 and is less than or equals to 139 or equals to 8080 AND where destination port is greater than 4089 or equals to 179 AND where source port equals to 8080 ", FS_PARSER.stringNlri(flows));
298     }
299
300     private PortCase createPorts() {
301         final List<Ports> ports = Lists.newArrayList(new PortsBuilder().setOp(new NumericOperand(false, false, true, true, false)).setValue(137).build(),
302                 new PortsBuilder().setOp(new NumericOperand(true, false, true, false, true)).setValue(139).build(),
303                 new PortsBuilder().setOp(new NumericOperand(false, true, true, false, false)).setValue(8080).build());
304
305         return new PortCaseBuilder().setPorts(ports).build();
306     }
307
308     @Test
309     public void testParseMpUnreachNlri() throws BGPParsingException {
310         final List<Flowspec> fs = new ArrayList<>();
311         final MpUnreachNlriBuilder mp = new MpUnreachNlriBuilder();
312
313         final FlowspecBuilder builder = new FlowspecBuilder();
314
315         final FlowspecType icmpType = createIcmpType();
316         builder.setFlowspecType(icmpType);
317         fs.add(builder.build());
318
319         final FlowspecType icmpCode = createIcmpCode();
320         builder.setFlowspecType(icmpCode);
321         fs.add(builder.build());
322
323         final TcpFlagsCase tcp = createTcp();
324         builder.setFlowspecType(tcp);
325         fs.add(builder.build());
326
327         final PacketLengthCase packet = createPackets();
328         builder.setFlowspecType(packet);
329         fs.add(builder.build());
330
331         final FlowspecType dscp = createDscp();
332         builder.setFlowspecType(dscp);
333         fs.add(builder.build());
334
335         final FlowspecType fragment = createFragment();
336         builder.setFlowspecType(fragment);
337         fs.add(builder.build());
338
339         mp.setAfi(Ipv4AddressFamily.class).setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
340                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder()
341                         .setDestinationFlowspec(
342                                 new DestinationFlowspecBuilder()
343                                         .setFlowspec(fs)
344                                         .build()
345                         ).build()
346                 ).build()
347         );
348
349         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(flowspecContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
350
351         final MpUnreachNlriBuilder result = new MpUnreachNlriBuilder();
352         result.setAfi(Ipv4AddressFamily.class);
353         result.setSafi(FlowspecSubsequentAddressFamily.class);
354         parser.parseNlri(Unpooled.wrappedBuffer(UNREACHED_NLRI), result);
355
356         final List<Flowspec> flows = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase) (result.getWithdrawnRoutes().getDestinationType()))
357                 .getDestinationFlowspec()
358                 .getFlowspec();
359         checkUnreachFlows(flows, icmpType, icmpCode, tcp, packet, dscp, fragment);
360
361         final ByteBuf buffer = Unpooled.buffer();
362         parser.serializeNlri(flows, null, buffer);
363         assertArrayEquals(UNREACHED_NLRI, ByteArray.readAllBytes(buffer));
364
365         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mp.build()).build()).build(), buffer);
366         assertArrayEquals(UNREACHED_NLRI, ByteArray.readAllBytes(buffer));
367
368         assertEquals("all packets where ICMP type is less than 2 or is less than 3 AND where ICMP code is less than 4 or 5 AND where TCP flags is not 1025 or does match 22193 AND where packet length is less than 57005 AND where DSCP is greater than 42 AND where fragment does match 'IS FIRST' 'IS LAST' 'IS A' ", FS_PARSER.stringNlri(flows));
369
370     }
371
372     private FlowspecType createFragment() {
373         final List<Fragments> fragments = Lists.newArrayList(new FragmentsBuilder().setOp(new BitmaskOperand(false, true, true, false)).setValue(new Fragment(false, true, true, true)).build());
374         return new FragmentCaseBuilder().setFragments(fragments).build();
375     }
376
377     private FlowspecType createDscp() {
378         final List<Dscps> dscps = Lists.newArrayList(new DscpsBuilder().setOp(new NumericOperand(false, true, false, true, false)).setValue(new Dscp((short) 42)).build());
379         return new DscpCaseBuilder().setDscps(dscps).build();
380     }
381
382     private PacketLengthCase createPackets() {
383         final List<PacketLengths> packets = Lists.newArrayList(new PacketLengthsBuilder().setOp(new NumericOperand(false, true, false, false, true))
384                 .setValue(57005).build());
385         return new PacketLengthCaseBuilder().setPacketLengths(packets).build();
386     }
387
388     private TcpFlagsCase createTcp() {
389         final List<TcpFlags> flags = Lists.newArrayList(new TcpFlagsBuilder().setOp(new BitmaskOperand(false, false, false, true)).setValue(1025).build(),
390                 new TcpFlagsBuilder().setOp(new BitmaskOperand(false, true, true, false)).setValue(22193).build());
391         return new TcpFlagsCaseBuilder().setTcpFlags(flags).build();
392     }
393
394     private FlowspecType createIcmpCode() {
395         final List<Codes> codes = Lists.newArrayList(new CodesBuilder().setOp(new NumericOperand(false, false, false, false, true)).setValue((short) 4).build(),
396                 new CodesBuilder().setOp(new NumericOperand(false, true, false, false, false)).setValue((short) 5).build());
397         return new IcmpCodeCaseBuilder().setCodes(codes).build();
398     }
399
400     private FlowspecType createIcmpType() {
401         final List<Types> types = Lists.newArrayList(new TypesBuilder().setOp(new NumericOperand(false, false, false, false, true)).setValue((short) 2).build(),
402                 new TypesBuilder().setOp(new NumericOperand(false, true, false, false, true)).setValue((short) 3).build());
403         return new IcmpTypeCaseBuilder().setTypes(types).build();
404     }
405
406     private void checkUnreachFlows(final List<Flowspec> flows, final FlowspecType icmpType, final FlowspecType icmpCode, final TcpFlagsCase tcp,
407                                    final PacketLengthCase packet, final FlowspecType dscp, final FlowspecType fragment) {
408         assertEquals(6, flows.size());
409         assertEquals(icmpType, flows.get(0).getFlowspecType());
410         assertEquals(icmpCode, flows.get(1).getFlowspecType());
411         assertEquals(tcp, flows.get(2).getFlowspecType());
412         assertEquals(packet, flows.get(3).getFlowspecType());
413         assertEquals(dscp, flows.get(4).getFlowspecType());
414         assertEquals(fragment, flows.get(5).getFlowspecType());
415     }
416
417     @Test
418     public void testParseMpUnreachNlriConstraint() throws BGPParsingException {
419         final List<Flowspec> fs = new ArrayList<>();
420         final MpUnreachNlriBuilder mp = new MpUnreachNlriBuilder();
421
422         final FlowspecBuilder builder = new FlowspecBuilder();
423
424         final FlowspecType icmpType = createIcmpType();
425         builder.setFlowspecType(icmpType);
426         fs.add(builder.build());
427
428         final FlowspecType icmpCode = createIcmpCode();
429         builder.setFlowspecType(icmpCode);
430         fs.add(builder.build());
431
432         final TcpFlagsCase tcp = createTcp();
433         builder.setFlowspecType(tcp);
434         fs.add(builder.build());
435
436         final PacketLengthCase packet = createPackets();
437         builder.setFlowspecType(packet);
438         fs.add(builder.build());
439
440         final FlowspecType dscp = createDscp();
441         builder.setFlowspecType(dscp);
442         fs.add(builder.build());
443
444         final FlowspecType fragment = createFragment();
445         builder.setFlowspecType(fragment);
446         fs.add(builder.build());
447
448         mp.setAfi(Ipv4AddressFamily.class).setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
449                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCaseBuilder()
450                         .setDestinationFlowspec(
451                                 new DestinationFlowspecBuilder()
452                                         .setPathId(PATH_ID)
453                                         .setFlowspec(fs)
454                                         .build()
455                         ).build()
456                 ).build()
457         );
458
459         final SimpleFlowspecIpv4NlriParser parser = new SimpleFlowspecIpv4NlriParser(flowspecContext.getFlowspecTypeRegistry(SimpleFlowspecExtensionProviderContext.AFI.IPV4, SimpleFlowspecExtensionProviderContext.SAFI.FLOWSPEC));
460
461         final MpUnreachNlriBuilder result = new MpUnreachNlriBuilder();
462         result.setAfi(Ipv4AddressFamily.class);
463         result.setSafi(FlowspecSubsequentAddressFamily.class);
464         parser.parseNlri(Unpooled.wrappedBuffer(UNREACHED_NLRI_ADD_PATH), result, this.constraint);
465
466         final List<Flowspec> flows = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecCase) (result.getWithdrawnRoutes().getDestinationType()))
467                 .getDestinationFlowspec()
468                 .getFlowspec();
469         checkUnreachFlows(flows, icmpType, icmpCode, tcp, packet, dscp, fragment);
470
471
472         final ByteBuf buffer = Unpooled.buffer();
473         parser.serializeNlri(flows, PATH_ID, buffer);
474         assertArrayEquals(UNREACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
475
476         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mp.build()).build()).build(), buffer);
477         assertArrayEquals(UNREACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
478
479         assertEquals("all packets where ICMP type is less than 2 or is less than 3 AND where ICMP code is less than 4 or 5 AND where TCP flags is not 1025 or does match 22193 AND where packet length is less than 57005 AND where DSCP is greater than 42 AND where fragment does match 'IS FIRST' 'IS LAST' 'IS A' ", FS_PARSER.stringNlri(flows));
480
481     }
482
483     @Test
484     public void testExtractFlowspecDestPrefix() {
485         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
486         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
487         entry.withChild(Builders.unkeyedListBuilder()
488                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
489                 .withChild(Builders.unkeyedListEntryBuilder()
490                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
491                         .withChild(Builders.choiceBuilder()
492                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
493                                 .withChild(Builders.leafBuilder().withNodeIdentifier(AbstractFlowspecNlriParser.DEST_PREFIX_NID).withValue("127.0.0.5/32").build()).build()).build()).build());
494
495         final FlowspecBuilder expectedFS = new FlowspecBuilder();
496         expectedFS.setFlowspecType(new DestinationPrefixCaseBuilder().setDestinationPrefix(new Ipv4Prefix("127.0.0.5/32")).build());
497         final List<Flowspec> expected = new ArrayList<>();
498         expected.add(expectedFS.build());
499         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
500     }
501
502     @Test
503     public void testExtractFlowspecSourcePrefix() {
504         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
505         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
506         entry.withChild(Builders.unkeyedListBuilder()
507                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
508                 .withChild(Builders.unkeyedListEntryBuilder()
509                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
510                         .withChild(Builders.choiceBuilder()
511                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
512                                 .withChild(Builders.leafBuilder().withNodeIdentifier(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).withValue("127.0.0.6/32").build()).build()).build()).build());
513
514         final FlowspecBuilder expectedFS = new FlowspecBuilder();
515         expectedFS.setFlowspecType(new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix("127.0.0.6/32")).build());
516         final List<Flowspec> expected = new ArrayList<>();
517         expected.add(expectedFS.build());
518         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
519     }
520
521     @Test
522     public void testExtractFlowspecProtocolIps() {
523         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
524         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
525         entry.withChild(Builders.unkeyedListBuilder()
526                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
527                 .withChild(Builders.unkeyedListEntryBuilder()
528                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
529                         .withChild(Builders.choiceBuilder()
530                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
531                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PROTOCOL_IP_NID)
532                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PROTOCOL_IP_NID)
533                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.END_OF_LIST_VALUE, AbstractOperandParser.AND_BIT_VALUE)).build())
534                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 100).build()).build())
535                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PROTOCOL_IP_NID)
536                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE)).build())
537                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 200).build()).build())
538                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PROTOCOL_IP_NID)
539                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.END_OF_LIST_VALUE, AbstractOperandParser.AND_BIT_VALUE, AbstractNumericOperandParser.EQUALS_VALUE)).build())
540                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 240).build()).build())
541                                         .build()).build()).build()).build());
542
543         final FlowspecBuilder expectedFS = new FlowspecBuilder();
544         expectedFS.setFlowspecType(new ProtocolIpCaseBuilder().setProtocolIps(Lists.<ProtocolIps>newArrayList(
545                 new ProtocolIpsBuilder().setValue((short) 100).setOp(new NumericOperand(true, true, false, false, false)).build(),
546                 new ProtocolIpsBuilder().setValue((short) 200).setOp(new NumericOperand(true, false, false, false, false)).build(),
547                 new ProtocolIpsBuilder().setValue((short) 240).setOp(new NumericOperand(true, true, true, false, false)).build())).build());
548         final List<Flowspec> expected = new ArrayList<>();
549         expected.add(expectedFS.build());
550         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
551     }
552
553     @Test
554     public void testExtractFlowspecPorts() {
555         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
556         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
557         entry.withChild(Builders.unkeyedListBuilder()
558                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
559                 .withChild(Builders.unkeyedListEntryBuilder()
560                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
561                         .withChild(Builders.choiceBuilder()
562                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
563                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PORTS_NID)
564                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PROTOCOL_IP_NID)
565                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.END_OF_LIST_VALUE, AbstractOperandParser.AND_BIT_VALUE, AbstractNumericOperandParser.LESS_THAN_VALUE)).build())
566                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(100).build()).build())
567                                         .build()).build()).build()).build());
568
569         final FlowspecBuilder expectedFS = new FlowspecBuilder();
570         expectedFS.setFlowspecType(new PortCaseBuilder().setPorts(Lists.<Ports>newArrayList(new PortsBuilder().setValue(100).setOp(new NumericOperand(true, true, false, false, true)).build())).build());
571         final List<Flowspec> expected = new ArrayList<>();
572         expected.add(expectedFS.build());
573         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
574     }
575
576     @Test
577     public void testExtractFlowspecDestinationPorts() {
578         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
579         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
580         entry.withChild(Builders.unkeyedListBuilder()
581                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
582                 .withChild(Builders.unkeyedListEntryBuilder()
583                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
584                         .withChild(Builders.choiceBuilder()
585                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
586                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.DEST_PORT_NID)
587                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
588                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.END_OF_LIST_VALUE, AbstractNumericOperandParser.EQUALS_VALUE)).build())
589                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(1024).build()).build())
590                                         .build()).build()).build()).build());
591         final FlowspecBuilder expectedFS = new FlowspecBuilder();
592         expectedFS.setFlowspecType(new DestinationPortCaseBuilder().setDestinationPorts(Lists.<DestinationPorts>newArrayList(new DestinationPortsBuilder().setValue(1024).setOp(new NumericOperand(false, true, true, false, false)).build())).build());
593         final List<Flowspec> expected = new ArrayList<>();
594         expected.add(expectedFS.build());
595         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
596     }
597
598     @Test
599     public void testExtractFlowspecSourcePorts() {
600         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
601         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
602         entry.withChild(Builders.unkeyedListBuilder()
603                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
604                 .withChild(Builders.unkeyedListEntryBuilder()
605                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
606                         .withChild(Builders.choiceBuilder()
607                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
608                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.SOURCE_PORT_NID)
609                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
610                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractOperandParser.END_OF_LIST_VALUE, AbstractNumericOperandParser.EQUALS_VALUE, AbstractNumericOperandParser.GREATER_THAN_VALUE, AbstractNumericOperandParser.LESS_THAN_VALUE)).build())
611                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(8080).build()).build())
612                                         .build()).build()).build()).build());
613         final FlowspecBuilder expectedFS = new FlowspecBuilder();
614         expectedFS.setFlowspecType(new SourcePortCaseBuilder().setSourcePorts(Lists.<SourcePorts>newArrayList(new SourcePortsBuilder().setValue(8080).setOp(new NumericOperand(true, true, true, true, true)).build())).build());
615         final List<Flowspec> expected = new ArrayList<>();
616         expected.add(expectedFS.build());
617         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
618     }
619
620     @Test
621     public void testExtractFlowspecSourceTypes() {
622         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
623         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
624         entry.withChild(Builders.unkeyedListBuilder()
625                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
626                 .withChild(Builders.unkeyedListEntryBuilder()
627                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
628                         .withChild(Builders.choiceBuilder()
629                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
630                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.ICMP_TYPE_NID)
631                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
632                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractOperandParser.END_OF_LIST_VALUE, AbstractNumericOperandParser.EQUALS_VALUE, AbstractNumericOperandParser.GREATER_THAN_VALUE, AbstractNumericOperandParser.LESS_THAN_VALUE)).build())
633                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 22).build()).build())
634                                         .build()).build()).build()).build());
635         final FlowspecBuilder expectedFS = new FlowspecBuilder();
636         expectedFS.setFlowspecType(new IcmpTypeCaseBuilder().setTypes(Lists.<Types>newArrayList(new TypesBuilder().setValue((short) 22).setOp(new NumericOperand(true, true, true, true, true)).build())).build());
637         final List<Flowspec> expected = new ArrayList<>();
638         expected.add(expectedFS.build());
639         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
640     }
641
642     @Test
643     public void testExtractFlowspecSourceCodes() {
644         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
645         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
646         entry.withChild(Builders.unkeyedListBuilder()
647                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
648                 .withChild(Builders.unkeyedListEntryBuilder()
649                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
650                         .withChild(Builders.choiceBuilder()
651                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
652                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.ICMP_CODE_NID)
653                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
654                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet()).build())
655                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 23).build()).build())
656                                         .build()).build()).build()).build());
657         final FlowspecBuilder expectedFS = new FlowspecBuilder();
658         expectedFS.setFlowspecType(new IcmpCodeCaseBuilder().setCodes(Lists.<Codes>newArrayList(new CodesBuilder().setValue((short) 23).setOp(new NumericOperand(false, false, false, false, false)).build())).build());
659         final List<Flowspec> expected = new ArrayList<>();
660         expected.add(expectedFS.build());
661         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
662     }
663
664     @Test
665     public void testExtractFlowspecSourceTcpFlags() {
666         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
667         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
668         entry.withChild(Builders.unkeyedListBuilder()
669                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
670                 .withChild(Builders.unkeyedListEntryBuilder()
671                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
672                         .withChild(Builders.choiceBuilder()
673                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
674                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.TCP_FLAGS_NID)
675                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
676                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractOperandParser.END_OF_LIST_VALUE)).build())
677                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(99).build()).build())
678                                         .build()).build()).build()).build());
679         final FlowspecBuilder expectedFS = new FlowspecBuilder();
680         expectedFS.setFlowspecType(new TcpFlagsCaseBuilder().setTcpFlags(Lists.<TcpFlags>newArrayList(new TcpFlagsBuilder().setValue(99).setOp(new BitmaskOperand(true, true, false, false)).build())).build());
681         final List<Flowspec> expected = new ArrayList<>();
682         expected.add(expectedFS.build());
683         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
684     }
685
686     @Test
687     public void testExtractFlowspecPacketLengths() {
688         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
689         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
690         entry.withChild(Builders.unkeyedListBuilder()
691                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
692                 .withChild(Builders.unkeyedListEntryBuilder()
693                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
694                         .withChild(Builders.choiceBuilder()
695                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
696                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.PACKET_LENGTHS_NID)
697                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
698                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractNumericOperandParser.GREATER_THAN_VALUE)).build())
699                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(101).build()).build())
700                                         .build()).build()).build()).build());
701         final FlowspecBuilder expectedFS = new FlowspecBuilder();
702         expectedFS.setFlowspecType(new PacketLengthCaseBuilder().setPacketLengths(Lists.<PacketLengths>newArrayList(new PacketLengthsBuilder().setValue(101).setOp(new NumericOperand(true, false, false, true, false)).build())).build());
703         final List<Flowspec> expected = new ArrayList<>();
704         expected.add(expectedFS.build());
705         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
706     }
707
708     @Test
709     public void testExtractFlowspecDscps() {
710         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
711         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
712         entry.withChild(Builders.unkeyedListBuilder()
713                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
714                 .withChild(Builders.unkeyedListEntryBuilder()
715                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
716                         .withChild(Builders.choiceBuilder()
717                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
718                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.DSCP_NID)
719                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
720                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractOperandParser.END_OF_LIST_VALUE, AbstractNumericOperandParser.GREATER_THAN_VALUE)).build())
721                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue((short) 15).build()).build())
722                                         .build()).build()).build()).build());
723         final FlowspecBuilder expectedFS = new FlowspecBuilder();
724         expectedFS.setFlowspecType(new DscpCaseBuilder().setDscps(Lists.<Dscps>newArrayList(new DscpsBuilder().setValue(new Dscp((short) 15)).setOp(new NumericOperand(true, true, false, true, false)).build())).build());
725         final List<Flowspec> expected = new ArrayList<>();
726         expected.add(expectedFS.build());
727         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
728     }
729
730     @Test
731     public void testExtractFlowspecFragments() {
732         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> entry = Builders.mapEntryBuilder();
733         entry.withNodeIdentifier(new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, FlowspecRoute.QNAME, entry));
734         entry.withChild(Builders.unkeyedListBuilder()
735                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
736                 .withChild(Builders.unkeyedListEntryBuilder()
737                         .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_NID)
738                         .withChild(Builders.choiceBuilder()
739                                 .withNodeIdentifier(AbstractFlowspecNlriParser.FLOWSPEC_TYPE_NID)
740                                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.FRAGMENT_NID)
741                                         .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID)
742                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.OP_NID).withValue(Sets.<String>newHashSet(AbstractOperandParser.AND_BIT_VALUE, AbstractOperandParser.END_OF_LIST_VALUE, BitmaskOperandParser.MATCH_VALUE, BitmaskOperandParser.NOT_VALUE)).build())
743                                                 .withChild(Builders.leafBuilder().withNodeIdentifier(SimpleFlowspecIpv4NlriParser.VALUE_NID).withValue(Sets.newHashSet(SimpleFlowspecIpv4NlriParser.DO_NOT_VALUE, SimpleFlowspecIpv4NlriParser.FIRST_VALUE, SimpleFlowspecIpv4NlriParser.IS_A_VALUE, SimpleFlowspecIpv4NlriParser.LAST_VALUE)).build()).build())
744                                         .build()).build()).build()).build());
745         final FlowspecBuilder expectedFS = new FlowspecBuilder();
746         expectedFS.setFlowspecType(new FragmentCaseBuilder().setFragments(Lists.<Fragments>newArrayList(new FragmentsBuilder().setValue(new Fragment(true, true, true, true)).setOp(new BitmaskOperand(true, true, true, true)).build())).build());
747         final List<Flowspec> expected = new ArrayList<>();
748         expected.add(expectedFS.build());
749         assertEquals(expected, FS_PARSER.extractFlowspec(entry.build()));
750     }
751 }