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.rev180329.BitmaskOperand;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.Fragment;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.FragmentCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.FragmentCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.fragment._case.Fragments;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.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(final byte fragment);
33 protected abstract byte serializeFragment(final Fragment fragment);
36 public void serializeType(final FlowspecType fsType, final ByteBuf output) {
37 Preconditions.checkArgument(fsType instanceof FragmentCase, "FragmentCase class is mandatory!");
38 output.writeByte(FRAGMENT_VALUE);
39 serializeFragments(((FragmentCase) fsType).getFragments(), output);
43 public FlowspecType parseType(final ByteBuf buffer) {
47 return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
50 private void serializeFragments(final List<Fragments> fragments, final ByteBuf nlriByteBuf) {
51 for (final Iterator<Fragments> it = fragments.iterator(); it.hasNext(); ) {
52 final Fragments fragment = it.next();
53 BitmaskOperandParser.INSTANCE.serialize(fragment.getOp(), 1, !it.hasNext(), nlriByteBuf);
54 nlriByteBuf.writeByte(serializeFragment(fragment.getValue()));
58 private List<Fragments> parseFragments(final ByteBuf nlri) {
59 final List<Fragments> fragments = new ArrayList<>();
61 // we can do this as all fields will be rewritten in the cycle
62 final FragmentsBuilder builder = new FragmentsBuilder();
64 final byte b = nlri.readByte();
65 final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
67 builder.setValue(parseFragment(nlri.readByte()));
68 end = op.isEndOfList();
69 fragments.add(builder.build());