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.FlowspecTypeParser;
15 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.NumericOperand;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeaders;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeadersBuilder;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
25 public final class FSIpv6NextHeaderHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
26 public static final int NEXT_HEADER_VALUE = 3;
29 public void serializeType(final FlowspecType value, final ByteBuf output) {
30 Preconditions.checkArgument(value instanceof NextHeaderCase, "NextHeaderCase class is mandatory!");
31 output.writeByte(NEXT_HEADER_VALUE);
32 NumericOneByteOperandParser.INSTANCE.serialize(((NextHeaderCase) value).getNextHeaders(), output);
36 public FlowspecType parseType(final ByteBuf buffer) {
40 return new NextHeaderCaseBuilder().setNextHeaders(parseNextHeader(buffer)).build();
43 private static List<NextHeaders> parseNextHeader(final ByteBuf nlri) {
44 final List<NextHeaders> headers = new ArrayList<>();
46 // we can do this as all fields will be rewritten in the cycle
47 final NextHeadersBuilder builder = new NextHeadersBuilder();
49 final byte b = nlri.readByte();
50 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
52 builder.setValue(ByteBufUtils.readUint8(nlri));
53 end = op.getEndOfList();
54 headers.add(builder.build());