2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Joiner;
14 import com.google.common.base.Preconditions;
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;
21 import java.util.stream.Collectors;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.opendaylight.protocol.bgp.flowspec.handlers.BitmaskOperandParser;
25 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
26 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericTwoByteOperandParser;
27 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
28 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
29 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
30 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
31 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
32 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.Dscp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.Fragment;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.Flowspec;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.FlowspecBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DestinationPortCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DestinationPortCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DscpCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DscpCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.FragmentCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.FragmentCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpCodeCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpCodeCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpTypeCase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpTypeCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.PacketLengthCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.PacketLengthCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.PortCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.PortCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.SourcePortCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.SourcePortCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.TcpFlagsCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.TcpFlagsCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.destination.port._case.DestinationPorts;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.destination.port._case.DestinationPortsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.dscp._case.Dscps;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.dscp._case.DscpsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.fragment._case.Fragments;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.fragment._case.FragmentsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.code._case.Codes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.code._case.CodesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.type._case.Types;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.type._case.TypesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengths;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengthsBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.port._case.Ports;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.port._case.PortsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePorts;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePortsBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlags;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlagsBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
90 import org.opendaylight.yangtools.yang.binding.DataObject;
91 import org.opendaylight.yangtools.yang.common.QName;
92 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
93 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
94 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
95 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
96 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
97 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
98 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
99 import org.slf4j.Logger;
100 import org.slf4j.LoggerFactory;
102 public abstract class AbstractFlowspecNlriParser implements NlriParser, NlriSerializer {
103 private static final Logger LOG = LoggerFactory.getLogger(AbstractFlowspecNlriParser.class);
106 static final NodeIdentifier FLOWSPEC_NID = new NodeIdentifier(Flowspec.QNAME);
108 static final NodeIdentifier FLOWSPEC_TYPE_NID = new NodeIdentifier(FlowspecType.QNAME);
109 public static final NodeIdentifier DEST_PREFIX_NID
110 = new NodeIdentifier(QName.create(DestinationPrefixCase.QNAME, "destination-prefix").intern());
111 public static final NodeIdentifier SOURCE_PREFIX_NID
112 = new NodeIdentifier(QName.create(SourcePrefixCase.QNAME, "source-prefix").intern());
114 static final NodeIdentifier PORTS_NID = new NodeIdentifier(Ports.QNAME);
116 static final NodeIdentifier DEST_PORT_NID = new NodeIdentifier(DestinationPorts.QNAME);
118 static final NodeIdentifier SOURCE_PORT_NID = new NodeIdentifier(SourcePorts.QNAME);
120 static final NodeIdentifier ICMP_TYPE_NID = new NodeIdentifier(Types.QNAME);
122 static final NodeIdentifier ICMP_CODE_NID = new NodeIdentifier(Codes.QNAME);
124 static final NodeIdentifier TCP_FLAGS_NID = new NodeIdentifier(TcpFlags.QNAME);
126 static final NodeIdentifier PACKET_LENGTHS_NID = new NodeIdentifier(PacketLengths.QNAME);
128 static final NodeIdentifier DSCP_NID = new NodeIdentifier(Dscps.QNAME);
130 static final NodeIdentifier FRAGMENT_NID = new NodeIdentifier(Fragments.QNAME);
132 public static final NodeIdentifier OP_NID
133 = new NodeIdentifier(QName.create(Flowspec.QNAME.getModule(), "op"));
135 public static final NodeIdentifier VALUE_NID
136 = new NodeIdentifier(QName.create(Flowspec.QNAME.getModule(), "value"));
138 protected final SimpleFlowspecTypeRegistry flowspecTypeRegistry;
141 * Add this constant to length value to achieve all ones in the leftmost nibble.
143 private static final int LENGTH_MAGIC = 61440;
144 private static final int MAX_NLRI_LENGTH = 0xFFF;
145 private static final int MAX_NLRI_LENGTH_ONE_BYTE = 0xF0;
148 static final String DO_NOT_VALUE = "do-not";
150 static final String FIRST_VALUE = "first";
152 static final String LAST_VALUE = "last";
154 static final String IS_A_VALUE = "is-a";
156 private static final String FLOW_SEPARATOR = " AND ";
158 protected AbstractFlowspecNlriParser(final SimpleFlowspecTypeRegistry flowspecTypeRegistry) {
159 this.flowspecTypeRegistry = requireNonNull(flowspecTypeRegistry);
162 protected abstract void serializeMpReachNlri(final DestinationType dstType, final ByteBuf byteAggregator);
164 protected abstract void serializeMpUnreachNlri(final DestinationType dstType, final ByteBuf byteAggregator);
166 public abstract void extractSpecificFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder);
168 protected abstract void stringSpecificFSNlriType(final FlowspecType value, final StringBuilder buffer);
171 * Create withdrawn destination type
173 * @param nlriFields a list of NLRI fields to be included in the destination type
174 * @param pathId associated path id with given NLRI
175 * @return created destination type
177 public abstract DestinationType createWithdrawnDestinationType(@Nonnull Object[] nlriFields,
178 @Nullable PathId pathId);
181 * Create advertized destination type
183 * @param nlriFields a list of NLRI fields to be included in the destination type
184 * @param pathId associated path id with given NLRI
185 * @return created destination type
187 public abstract DestinationType createAdvertizedRoutesDestinationType(@Nonnull Object[] nlriFields, @Nullable PathId pathId);
190 public final void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
191 Preconditions.checkArgument(attribute instanceof Attributes,
192 "Attribute parameter is not a PathAttribute object.");
193 final Attributes pathAttributes = (Attributes) attribute;
194 final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
195 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
197 if (pathAttributes1 != null) {
198 final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
199 if (routes != null) {
200 serializeMpReachNlri(routes.getDestinationType(), byteAggregator);
204 if (pathAttributes2 != null) {
205 final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
206 if (routes != null) {
207 serializeMpUnreachNlri(routes.getDestinationType(), byteAggregator);
212 protected void serializeNlri(@Nonnull final Object[] nlriFields, @Nonnull final ByteBuf buffer) {
213 final List<Flowspec> flowspecList = (List<Flowspec>) nlriFields[0];
214 serializeNlri(flowspecList, buffer);
217 protected final void serializeNlri(final List<Flowspec> flowspecList, @Nonnull final ByteBuf buffer) {
218 if (flowspecList != null) {
219 for (final Flowspec flow : flowspecList) {
220 this.flowspecTypeRegistry.serializeFlowspecType(flow.getFlowspecType(), buffer);
226 * Serializes Flowspec NLRI to ByteBuf.
228 * @param nlriFields NLRI fields to be serialized
230 * @param buffer where flowspec NLRI will be serialized
232 protected final void serializeNlri(@Nonnull final Object[] nlriFields, @Nullable final PathId pathId,
233 @Nonnull final ByteBuf buffer) {
234 final ByteBuf nlriByteBuf = Unpooled.buffer();
235 PathIdUtil.writePathId(pathId, buffer);
237 serializeNlri(nlriFields, nlriByteBuf);
239 Preconditions.checkState(nlriByteBuf.readableBytes() <= MAX_NLRI_LENGTH,
240 "Maximum length of Flowspec NLRI reached.");
241 if (nlriByteBuf.readableBytes() <= MAX_NLRI_LENGTH_ONE_BYTE) {
242 buffer.writeByte(nlriByteBuf.readableBytes());
244 buffer.writeShort(nlriByteBuf.readableBytes() + LENGTH_MAGIC);
246 buffer.writeBytes(nlriByteBuf);
249 public String stringNlri(final DataContainerNode<?> flowspec) {
250 return stringNlri(extractFlowspec(flowspec));
253 public final List<Flowspec> extractFlowspec(final DataContainerNode<?> route) {
254 requireNonNull(route, "Cannot extract flowspec from null route.");
255 final List<Flowspec> fsList = new ArrayList<>();
256 final Optional<DataContainerChild<? extends PathArgument, ?>> flowspecs = route.getChild(FLOWSPEC_NID);
257 if (flowspecs.isPresent()) {
258 for (final UnkeyedListEntryNode flowspec : ((UnkeyedListNode) flowspecs.get()).getValue()) {
259 final FlowspecBuilder fsBuilder = new FlowspecBuilder();
260 final Optional<DataContainerChild<?, ?>> flowspecType = flowspec.getChild(FLOWSPEC_TYPE_NID);
261 if (flowspecType.isPresent()) {
262 final ChoiceNode fsType = (ChoiceNode) flowspecType.get();
263 processFlowspecType(fsType, fsBuilder);
265 fsList.add(fsBuilder.build());
271 private void processFlowspecType(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
272 if (fsType.getChild(PORTS_NID).isPresent()) {
273 fsBuilder.setFlowspecType(new PortCaseBuilder()
274 .setPorts(createPorts((UnkeyedListNode) fsType.getChild(PORTS_NID).get())).build());
275 } else if (fsType.getChild(DEST_PORT_NID).isPresent()) {
276 fsBuilder.setFlowspecType(new DestinationPortCaseBuilder()
277 .setDestinationPorts(createDestinationPorts((UnkeyedListNode) fsType.getChild(DEST_PORT_NID).get()))
279 } else if (fsType.getChild(SOURCE_PORT_NID).isPresent()) {
280 fsBuilder.setFlowspecType(new SourcePortCaseBuilder()
281 .setSourcePorts(createSourcePorts((UnkeyedListNode) fsType.getChild(SOURCE_PORT_NID).get()))
283 } else if (fsType.getChild(ICMP_TYPE_NID).isPresent()) {
284 fsBuilder.setFlowspecType(new IcmpTypeCaseBuilder()
285 .setTypes(createTypes((UnkeyedListNode) fsType.getChild(ICMP_TYPE_NID).get())).build());
286 } else if (fsType.getChild(ICMP_CODE_NID).isPresent()) {
287 fsBuilder.setFlowspecType(new IcmpCodeCaseBuilder()
288 .setCodes(createCodes((UnkeyedListNode) fsType.getChild(ICMP_CODE_NID).get())).build());
289 } else if (fsType.getChild(TCP_FLAGS_NID).isPresent()) {
290 fsBuilder.setFlowspecType(new TcpFlagsCaseBuilder()
291 .setTcpFlags(createTcpFlags((UnkeyedListNode) fsType.getChild(TCP_FLAGS_NID).get())).build());
292 } else if (fsType.getChild(PACKET_LENGTHS_NID).isPresent()) {
293 fsBuilder.setFlowspecType(new PacketLengthCaseBuilder()
294 .setPacketLengths(createPacketLengths((UnkeyedListNode) fsType.getChild(PACKET_LENGTHS_NID).get()))
296 } else if (fsType.getChild(DSCP_NID).isPresent()) {
297 fsBuilder.setFlowspecType(new DscpCaseBuilder()
298 .setDscps(createDscpsLengths((UnkeyedListNode) fsType.getChild(DSCP_NID).get())).build());
299 } else if (fsType.getChild(FRAGMENT_NID).isPresent()) {
300 fsBuilder.setFlowspecType(new FragmentCaseBuilder()
301 .setFragments(createFragments((UnkeyedListNode) fsType.getChild(FRAGMENT_NID).get())).build());
303 extractSpecificFlowspec(fsType, fsBuilder);
307 private static List<Ports> createPorts(final UnkeyedListNode portsData) {
308 final List<Ports> ports = new ArrayList<>();
310 for (final UnkeyedListEntryNode node : portsData.getValue()) {
311 final PortsBuilder portsBuilder = new PortsBuilder();
312 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
313 opValue.ifPresent(dataContainerChild -> portsBuilder
314 .setOp(NumericTwoByteOperandParser.INSTANCE.create((Set<String>) dataContainerChild.getValue())));
315 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
316 valueNode.ifPresent(dataContainerChild -> portsBuilder.setValue((Integer) dataContainerChild.getValue()));
317 ports.add(portsBuilder.build());
323 private static List<DestinationPorts> createDestinationPorts(final UnkeyedListNode destinationPortsData) {
324 final List<DestinationPorts> destinationPorts = new ArrayList<>();
326 for (final UnkeyedListEntryNode node : destinationPortsData.getValue()) {
327 final DestinationPortsBuilder destPortsBuilder = new DestinationPortsBuilder();
328 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
329 opValue.ifPresent(dataContainerChild -> destPortsBuilder.setOp(NumericTwoByteOperandParser
330 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
331 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
332 valueNode.ifPresent(dataContainerChild
333 -> destPortsBuilder.setValue((Integer) dataContainerChild.getValue()));
334 destinationPorts.add(destPortsBuilder.build());
337 return destinationPorts;
340 private static List<SourcePorts> createSourcePorts(final UnkeyedListNode sourcePortsData) {
341 final List<SourcePorts> sourcePorts = new ArrayList<>();
343 for (final UnkeyedListEntryNode node : sourcePortsData.getValue()) {
344 final SourcePortsBuilder sourcePortsBuilder = new SourcePortsBuilder();
345 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
346 opValue.ifPresent(dataContainerChild -> sourcePortsBuilder.setOp(NumericTwoByteOperandParser
347 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
348 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
349 valueNode.ifPresent(dataContainerChild
350 -> sourcePortsBuilder.setValue((Integer) dataContainerChild.getValue()));
351 sourcePorts.add(sourcePortsBuilder.build());
357 private static List<Types> createTypes(final UnkeyedListNode typesData) {
358 final List<Types> types = new ArrayList<>();
360 for (final UnkeyedListEntryNode node : typesData.getValue()) {
361 final TypesBuilder typesBuilder = new TypesBuilder();
362 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
363 opValue.ifPresent(dataContainerChild -> typesBuilder.setOp(NumericOneByteOperandParser
364 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
365 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
366 valueNode.ifPresent(dataContainerChild -> typesBuilder.setValue((Short) dataContainerChild.getValue()));
367 types.add(typesBuilder.build());
373 private static List<Codes> createCodes(final UnkeyedListNode codesData) {
374 final List<Codes> codes = new ArrayList<>();
376 for (final UnkeyedListEntryNode node : codesData.getValue()) {
377 final CodesBuilder codesBuilder = new CodesBuilder();
378 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
379 opValue.ifPresent(dataContainerChild -> codesBuilder.setOp(NumericOneByteOperandParser
380 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
381 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
382 valueNode.ifPresent(dataContainerChild -> codesBuilder.setValue((Short) dataContainerChild.getValue()));
383 codes.add(codesBuilder.build());
389 private static List<TcpFlags> createTcpFlags(final UnkeyedListNode tcpFlagsData) {
390 final List<TcpFlags> tcpFlags = new ArrayList<>();
392 for (final UnkeyedListEntryNode node : tcpFlagsData.getValue()) {
393 final TcpFlagsBuilder tcpFlagsBuilder = new TcpFlagsBuilder();
394 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
395 opValue.ifPresent(dataContainerChild -> tcpFlagsBuilder
396 .setOp(BitmaskOperandParser.INSTANCE.create((Set<String>) dataContainerChild.getValue())));
397 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
398 valueNode.ifPresent(dataContainerChild
399 -> tcpFlagsBuilder.setValue((Integer) dataContainerChild.getValue()));
400 tcpFlags.add(tcpFlagsBuilder.build());
406 private static List<PacketLengths> createPacketLengths(final UnkeyedListNode packetLengthsData) {
407 final List<PacketLengths> packetLengths = new ArrayList<>();
409 for (final UnkeyedListEntryNode node : packetLengthsData.getValue()) {
410 final PacketLengthsBuilder packetLengthsBuilder = new PacketLengthsBuilder();
411 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
412 opValue.ifPresent(dataContainerChild -> packetLengthsBuilder.setOp(NumericTwoByteOperandParser
413 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
414 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
415 valueNode.ifPresent(dataContainerChild
416 -> packetLengthsBuilder.setValue((Integer) dataContainerChild.getValue()));
417 packetLengths.add(packetLengthsBuilder.build());
420 return packetLengths;
423 private static List<Dscps> createDscpsLengths(final UnkeyedListNode dscpLengthsData) {
424 final List<Dscps> dscpsLengths = new ArrayList<>();
426 for (final UnkeyedListEntryNode node : dscpLengthsData.getValue()) {
427 final DscpsBuilder dscpsLengthsBuilder = new DscpsBuilder();
428 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
429 opValue.ifPresent(dataContainerChild -> dscpsLengthsBuilder.setOp(NumericOneByteOperandParser
430 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
431 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
432 valueNode.ifPresent(dataContainerChild
433 -> dscpsLengthsBuilder.setValue(new Dscp((Short) dataContainerChild.getValue())));
434 dscpsLengths.add(dscpsLengthsBuilder.build());
440 private static List<Fragments> createFragments(final UnkeyedListNode fragmentsData) {
441 final List<Fragments> fragments = new ArrayList<>();
443 for (final UnkeyedListEntryNode node : fragmentsData.getValue()) {
444 final FragmentsBuilder fragmentsBuilder = new FragmentsBuilder();
445 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
446 opValue.ifPresent(dataContainerChild -> fragmentsBuilder.setOp(BitmaskOperandParser
447 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
448 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
449 valueNode.ifPresent(dataContainerChild
450 -> fragmentsBuilder.setValue(createFragment((Set<String>) dataContainerChild.getValue())));
451 fragments.add(fragmentsBuilder.build());
457 private static Fragment createFragment(final Set<String> data) {
458 return new Fragment(data.contains(DO_NOT_VALUE), data.contains(FIRST_VALUE), data.contains(IS_A_VALUE),
459 data.contains(LAST_VALUE));
462 protected final String stringNlri(final List<Flowspec> flows) {
463 final StringBuilder buffer = new StringBuilder("all packets ");
464 final Joiner joiner = Joiner.on(FLOW_SEPARATOR);
465 joiner.appendTo(buffer, flows.stream().map(this::encodeFlow).collect(Collectors.toList()));
466 return buffer.toString().replace(" ", " ");
470 final String encodeFlow(final Flowspec flow) {
471 final StringBuilder buffer = new StringBuilder();
472 final FlowspecType value = flow.getFlowspecType();
473 if (value instanceof PortCase) {
474 buffer.append("where port ");
475 buffer.append(NumericTwoByteOperandParser.INSTANCE.toString(((PortCase) value).getPorts()));
476 } else if (value instanceof DestinationPortCase) {
477 buffer.append("where destination port ");
478 buffer.append(NumericTwoByteOperandParser
479 .INSTANCE.toString(((DestinationPortCase) value).getDestinationPorts()));
480 } else if (value instanceof SourcePortCase) {
481 buffer.append("where source port ");
482 buffer.append(NumericTwoByteOperandParser.INSTANCE.toString(((SourcePortCase) value).getSourcePorts()));
483 } else if (value instanceof IcmpTypeCase) {
484 buffer.append("where ICMP type ");
485 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((IcmpTypeCase) value).getTypes()));
486 } else if (value instanceof IcmpCodeCase) {
487 buffer.append("where ICMP code ");
488 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((IcmpCodeCase) value).getCodes()));
489 } else if (value instanceof TcpFlagsCase) {
490 buffer.append(stringTcpFlags(((TcpFlagsCase) value).getTcpFlags()));
491 } else if (value instanceof PacketLengthCase) {
492 buffer.append("where packet length ");
493 buffer.append(NumericTwoByteOperandParser.INSTANCE.toString(((PacketLengthCase) value).getPacketLengths()));
494 } else if (value instanceof DscpCase) {
495 buffer.append(stringDscp(((DscpCase) value).getDscps()));
496 } else if (value instanceof FragmentCase) {
497 buffer.append(stringFragment(((FragmentCase) value).getFragments()));
499 stringSpecificFSNlriType(value, buffer);
501 return buffer.toString();
504 private static String stringTcpFlags(final List<TcpFlags> flags) {
505 final StringBuilder buffer = new StringBuilder("where TCP flags ");
506 boolean isFirst = true;
507 for (final TcpFlags item : flags) {
508 buffer.append(BitmaskOperandParser.INSTANCE.toString(item.getOp(), isFirst));
509 buffer.append(item.getValue());
515 return buffer.toString();
518 private static String stringDscp(final List<Dscps> dscps) {
519 final StringBuilder buffer = new StringBuilder("where DSCP ");
520 boolean isFirst = true;
521 for (final Dscps item : dscps) {
522 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(item.getOp(), isFirst));
523 buffer.append(item.getValue().getValue());
529 return buffer.toString();
532 private static String stringFragment(final List<Fragments> fragments) {
533 final StringBuilder buffer = new StringBuilder("where fragment ");
534 boolean isFirst = true;
535 for (final Fragments item : fragments) {
536 buffer.append(BitmaskOperandParser.INSTANCE.toString(item.getOp(), isFirst));
537 buffer.append(stringFragment(item.getValue()));
542 return buffer.toString();
545 private static String stringFragment(final Fragment fragment) {
546 final StringBuilder buffer = new StringBuilder();
547 if (fragment.isDoNot()) {
548 buffer.append("'DO NOT' ");
550 if (fragment.isFirst()) {
551 buffer.append("'IS FIRST' ");
553 if (fragment.isLast()) {
554 buffer.append("'IS LAST' ");
556 if (fragment.isIsA()) {
557 buffer.append("'IS A' ");
559 return buffer.toString();
562 public static int readNlriLength(@Nonnull final ByteBuf nlri) {
563 requireNonNull(nlri, "NLRI information cannot be null");
564 Preconditions.checkState(nlri.isReadable(), "NLRI Byte buffer is not readable.");
565 int length = nlri.readUnsignedByte();
566 if (length >= MAX_NLRI_LENGTH_ONE_BYTE) {
567 length = (length << Byte.SIZE | nlri.readUnsignedByte()) & MAX_NLRI_LENGTH;
569 Preconditions.checkState(length > 0 && length <= nlri.readableBytes(),
570 "Invalid flowspec NLRI length %s", length);
575 * Parses Flowspec NLRI into list of Flowspec.
577 * @param nlri byte representation of NLRI which will be parsed
578 * @return list of Flowspec
580 protected final List<Flowspec> parseNlriFlowspecList(@Nonnull final ByteBuf nlri) throws BGPParsingException {
581 if (!nlri.isReadable()) {
584 final List<Flowspec> fss = new ArrayList<>();
586 while (nlri.isReadable()) {
587 int nlriLength = readNlriLength(nlri);
588 Preconditions.checkState(nlriLength > 0 && nlriLength <= nlri.readableBytes(),
589 "Invalid flowspec NLRI length %s", nlriLength);
590 LOG.trace("Flowspec NLRI length is {}", nlriLength);
592 while (nlriLength > 0) {
593 final int readableLength = nlri.readableBytes();
594 final FlowspecBuilder builder = new FlowspecBuilder();
595 builder.setFlowspecType(this.flowspecTypeRegistry.parseFlowspecType(nlri));
596 fss.add(builder.build());
597 final int flowspecTypeLength = readableLength - nlri.readableBytes();
598 nlriLength -= flowspecTypeLength;
600 Preconditions.checkState(nlriLength == 0,
601 "Remain NLRI length should be 0 instead of %s", nlriLength);
608 * Override this function to parse additional NLRI fields
610 * @param nlri NLRI buffer
611 * @return Parsed additional fields
614 protected Object[] parseNlri(@Nonnull final ByteBuf nlri) throws BGPParsingException {
615 return new Object[] {parseNlriFlowspecList(nlri)};
619 public final void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpReachNlriBuilder builder,
620 @Nullable final PeerSpecificParserConstraint constraint)
621 throws BGPParsingException {
622 if (!nlri.isReadable()) {
625 final PathId pathId = readPathId(nlri, builder.getAfi(), builder.getSafi(), constraint);
626 final Object[] nlriFields = parseNlri(nlri);
627 builder.setAdvertizedRoutes(
628 new AdvertizedRoutesBuilder()
630 createAdvertizedRoutesDestinationType(nlriFields, pathId)
636 protected static PathId readPathId(@Nonnull final ByteBuf nlri, final Class<? extends AddressFamily> afi,
637 final Class<? extends SubsequentAddressFamily> safi, final PeerSpecificParserConstraint constraint) {
638 if (MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(afi, safi))) {
639 return PathIdUtil.readPathId(nlri);
645 public final void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder,
646 @Nullable final PeerSpecificParserConstraint constraint)
647 throws BGPParsingException {
648 if (!nlri.isReadable()) {
651 final PathId pathId = readPathId(nlri, builder.getAfi(), builder.getSafi(), constraint);
652 final Object[] nlriFields = parseNlri(nlri);
653 builder.setWithdrawnRoutes(
654 new WithdrawnRoutesBuilder()
656 createWithdrawnDestinationType(nlriFields, pathId)