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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractOperandParser;
15 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericTwoByteOperandParser;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PacketLengthCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.PacketLengthCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengths;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.packet.length._case.PacketLengthsBuilder;
27 public final class FSPacketLengthHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
28 public static final int PACKET_LENGTH_VALUE = 10;
31 public void serializeType(FlowspecType fsType, ByteBuf output) {
32 Preconditions.checkArgument(fsType instanceof PacketLengthCase, "PacketLengthCase class is mandatory!");
33 output.writeByte(PACKET_LENGTH_VALUE);
34 NumericTwoByteOperandParser.INSTANCE.serialize(((PacketLengthCase) fsType).getPacketLengths(), output);
38 public FlowspecType parseType(ByteBuf buffer) {
39 Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
40 return new PacketLengthCaseBuilder().setPacketLengths(parsePacketLength(buffer)).build();
43 private static List<PacketLengths> parsePacketLength(final ByteBuf nlri) {
44 final List<PacketLengths> packetLengths = new ArrayList<>();
46 // we can do this as all fields will be rewritten in the cycle
47 final PacketLengthsBuilder builder = new PacketLengthsBuilder();
49 final byte b = nlri.readByte();
50 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
52 final short length = AbstractOperandParser.parseLength(b);
53 builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length)));
54 end = op.isEndOfList();
55 packetLengths.add(builder.build());