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 io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.*;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.FlowLabelCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabel;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabelBuilder;
25 public final class FSIpv6FlowLabelHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
26 public static final int FLOW_LABEL_VALUE = 13;
29 public void serializeType(FlowspecType fsType, ByteBuf output) {
30 Preconditions.checkArgument(fsType instanceof FlowLabelCase, "FlowLabelCase class is mandatory!");
31 output.writeByte(FLOW_LABEL_VALUE);
32 serializeNumericFourByteValue(((FlowLabelCase) fsType).getFlowLabel(), output);
36 public FlowspecType parseType(ByteBuf buffer) {
37 return new FlowLabelCaseBuilder().setFlowLabel(parseFlowLabel(buffer)).build();
40 private static void serializeNumericFourByteValue(final List<FlowLabel> list, final ByteBuf nlriByteBuf) {
41 for (final FlowLabel item : list) {
42 final ByteBuf protoBuf = Unpooled.buffer();
43 Util.writeShortest(item.getValue().intValue(), protoBuf);
44 NumericOneByteOperandParser.INSTANCE.serialize(item.getOp(), protoBuf.readableBytes(), nlriByteBuf);
45 nlriByteBuf.writeBytes(protoBuf);
49 private static List<FlowLabel> parseFlowLabel(final ByteBuf nlri) {
50 final List<FlowLabel> labels = new ArrayList<>();
52 // we can do this as all fields will be rewritten in the cycle
53 final FlowLabelBuilder builder = new FlowLabelBuilder();
55 final byte b = nlri.readByte();
56 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
58 final short length = AbstractOperandParser.parseLength(b);
59 builder.setValue(ByteArray.bytesToLong(ByteArray.readBytes(nlri, length)));
60 end = op.isEndOfList();
61 labels.add(builder.build());