cd152bc6590a1705befd5407caad5fa28721993d
[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.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;
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(final byte fragment);
33     protected abstract byte serializeFragment(final Fragment fragment);
34
35     @Override
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);
40     }
41
42     @Override
43     public FlowspecType parseType(final ByteBuf buffer) {
44         if (buffer == null) {
45             return null;
46         }
47         return new FragmentCaseBuilder().setFragments(parseFragments(buffer)).build();
48     }
49
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()));
55         }
56     }
57
58     private List<Fragments> parseFragments(final ByteBuf nlri) {
59         final List<Fragments> fragments = new ArrayList<>();
60         boolean end = false;
61         // we can do this as all fields will be rewritten in the cycle
62         final FragmentsBuilder builder = new FragmentsBuilder();
63         while (!end) {
64             final byte b = nlri.readByte();
65             final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
66             builder.setOp(op);
67             builder.setValue(parseFragment(nlri.readByte()));
68             end = op.isEndOfList();
69             fragments.add(builder.build());
70         }
71         return fragments;
72     }
73 }