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.handlers;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.BitmaskOperand;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Fragment;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.FragmentCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.FragmentCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.fragment._case.Fragments;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.fragment._case.FragmentsBuilder;
22 public abstract class AbstractFSFragmentHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
24 public static final int FRAGMENT_VALUE = 12;
26 protected static final int LAST_FRAGMENT = 4;
27 protected static final int FIRST_FRAGMENT = 5;
28 protected static final int IS_A_FRAGMENT = 6;
29 protected static final int DONT_FRAGMENT = 7;
31 protected abstract Fragment parseFragment(final byte fragment);
32 protected abstract byte serializeFragment(final Fragment fragment);
35 public void serializeType(FlowspecType fsType, ByteBuf output) {
36 Preconditions.checkArgument(fsType instanceof FragmentCase, "FragmentCase class is mandatory!");
37 output.writeByte(FRAGMENT_VALUE);
38 serializeFragments(((FragmentCase) fsType).getFragments(), output);
42 public FlowspecType parseType(ByteBuf buffer) {
46 return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
49 protected final void serializeFragments(final List<Fragments> fragments, final ByteBuf nlriByteBuf) {
50 for (final Fragments fragment : fragments) {
51 BitmaskOperandParser.INSTANCE.serialize(fragment.getOp(), 1, nlriByteBuf);
52 nlriByteBuf.writeByte(serializeFragment(fragment.getValue()));
56 protected final List<Fragments> parseFragments(final ByteBuf nlri) {
57 final List<Fragments> fragments = new ArrayList<>();
59 // we can do this as all fields will be rewritten in the cycle
60 final FragmentsBuilder builder = new FragmentsBuilder();
62 final byte b = nlri.readByte();
63 final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
65 builder.setValue(parseFragment(nlri.readByte()));
66 end = op.isEndOfList();
67 fragments.add(builder.build());