Bug 5503 - remove package cyclic dependency in BGP-FS
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / AbstractFSFragmentHandler.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.flowspec.handlers;
9
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;
21
22 public abstract class AbstractFSFragmentHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
23
24     public static final int FRAGMENT_VALUE = 12;
25
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;
30
31     protected abstract Fragment parseFragment(final byte fragment);
32     protected abstract byte serializeFragment(final Fragment fragment);
33
34     @Override
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);
39     }
40
41     @Override
42     public FlowspecType parseType(ByteBuf buffer) {
43         if (buffer == null) {
44             return null;
45         }
46         return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
47     }
48
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()));
53         }
54     }
55
56     protected final List<Fragments> parseFragments(final ByteBuf nlri) {
57         final List<Fragments> fragments = new ArrayList<>();
58         boolean end = false;
59         // we can do this as all fields will be rewritten in the cycle
60         final FragmentsBuilder builder = new FragmentsBuilder();
61         while (!end) {
62             final byte b = nlri.readByte();
63             final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
64             builder.setOp(op);
65             builder.setValue(parseFragment(nlri.readByte()));
66             end = op.isEndOfList();
67             fragments.add(builder.build());
68         }
69         return fragments;
70     }
71 }