Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / 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.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;
22
23 public abstract class AbstractFSFragmentHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
24
25     public static final int FRAGMENT_VALUE = 12;
26
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;
31
32     protected abstract Fragment parseFragment(byte fragment);
33
34     protected abstract byte serializeFragment(Fragment fragment);
35
36     @Override
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);
41     }
42
43     @Override
44     public FlowspecType parseType(final ByteBuf buffer) {
45         if (buffer == null) {
46             return null;
47         }
48         return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
49     }
50
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()));
56         }
57     }
58
59     private List<Fragments> parseFragments(final ByteBuf nlri) {
60         final List<Fragments> fragments = new ArrayList<>();
61         boolean end = false;
62         // we can do this as all fields will be rewritten in the cycle
63         final FragmentsBuilder builder = new FragmentsBuilder();
64         while (!end) {
65             final byte b = nlri.readByte();
66             final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
67             builder.setOp(op);
68             builder.setValue(parseFragment(nlri.readByte()));
69             end = op.getEndOfList();
70             fragments.add(builder.build());
71         }
72         return fragments;
73     }
74 }