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