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.Iterator;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractOperandParser;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
19 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
20 import org.opendaylight.protocol.bgp.flowspec.handlers.Util;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.NumericOperand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabelBuilder;
29 public final class FSIpv6FlowLabelHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
30 static final int FLOW_LABEL_VALUE = 13;
32 private static void serializeNumericFourByteValue(final List<FlowLabel> list, final ByteBuf nlriByteBuf) {
33 for (final Iterator<FlowLabel> it = list.iterator(); it.hasNext(); ) {
34 final FlowLabel label = it.next();
35 final ByteBuf protoBuf = Unpooled.buffer();
36 Util.writeShortest(label.getValue().intValue(), protoBuf);
37 NumericOneByteOperandParser.INSTANCE.serialize(label.getOp(), protoBuf.readableBytes(),
38 !it.hasNext(), nlriByteBuf);
39 nlriByteBuf.writeBytes(protoBuf);
44 public void serializeType(final FlowspecType fsType, final ByteBuf output) {
45 Preconditions.checkArgument(fsType instanceof FlowLabelCase, "FlowLabelCase class is mandatory!");
46 output.writeByte(FLOW_LABEL_VALUE);
47 serializeNumericFourByteValue(((FlowLabelCase) fsType).getFlowLabel(), output);
51 public FlowspecType parseType(final ByteBuf buffer) {
52 return new FlowLabelCaseBuilder().setFlowLabel(parseFlowLabel(buffer)).build();
55 private static List<FlowLabel> parseFlowLabel(final ByteBuf nlri) {
56 final List<FlowLabel> labels = new ArrayList<>();
58 // we can do this as all fields will be rewritten in the cycle
59 final FlowLabelBuilder builder = new FlowLabelBuilder();
61 final byte b = nlri.readByte();
62 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
64 final short length = AbstractOperandParser.parseLength(b);
65 builder.setValue(ByteArray.bytesToLong(ByteArray.readBytes(nlri, length)));
66 end = op.isEndOfList();
67 labels.add(builder.build());