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