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