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.Iterator;
14 import java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.BitmaskOperand;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.Fragment;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.FlowspecType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.FragmentCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.FragmentCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.fragment._case.Fragments;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.fragment._case.FragmentsBuilder;
23 public abstract class AbstractFSFragmentHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
25 public static final int FRAGMENT_VALUE = 12;
27 static final int LAST_FRAGMENT = 4;
28 static final int FIRST_FRAGMENT = 5;
29 static final int IS_A_FRAGMENT = 6;
30 static final int DONT_FRAGMENT = 7;
32 protected abstract Fragment parseFragment(byte fragment);
34 protected abstract byte serializeFragment(Fragment fragment);
37 public void serializeType(final FlowspecType fsType, final ByteBuf output) {
38 Preconditions.checkArgument(fsType instanceof FragmentCase, "FragmentCase class is mandatory!");
39 output.writeByte(FRAGMENT_VALUE);
40 serializeFragments(((FragmentCase) fsType).getFragments(), output);
44 public FlowspecType parseType(final ByteBuf buffer) {
48 return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
51 private void serializeFragments(final List<Fragments> fragments, final ByteBuf nlriByteBuf) {
52 for (final Iterator<Fragments> it = fragments.iterator(); it.hasNext(); ) {
53 final Fragments fragment = it.next();
54 BitmaskOperandParser.INSTANCE.serialize(fragment.getOp(), 1, !it.hasNext(), nlriByteBuf);
55 nlriByteBuf.writeByte(serializeFragment(fragment.getValue()));
59 private List<Fragments> parseFragments(final ByteBuf nlri) {
60 final List<Fragments> fragments = new ArrayList<>();
62 // we can do this as all fields will be rewritten in the cycle
63 final FragmentsBuilder builder = new FragmentsBuilder();
65 final byte b = nlri.readByte();
66 final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
68 builder.setValue(parseFragment(nlri.readByte()));
69 end = op.getEndOfList();
70 fragments.add(builder.build());