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