Create common parent for extensions families
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSIpv6NextHeaderHandler.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;
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.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
15 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.NumericOperand;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeaders;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeadersBuilder;
23
24 public final class FSIpv6NextHeaderHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
25     public static final int NEXT_HEADER_VALUE = 3;
26
27     @Override
28     public void serializeType(FlowspecType value, ByteBuf output) {
29         Preconditions.checkArgument(value instanceof NextHeaderCase, "NextHeaderCase class is mandatory!");
30         output.writeByte(NEXT_HEADER_VALUE);
31         NumericOneByteOperandParser.INSTANCE.serialize(((NextHeaderCase) value).getNextHeaders(), output);
32     }
33
34     @Override
35     public FlowspecType parseType(ByteBuf buffer) {
36         if (buffer == null) {
37             return null;
38         }
39         return new NextHeaderCaseBuilder().setNextHeaders(parseNextHeader(buffer)).build();
40     }
41
42     private static List<NextHeaders> parseNextHeader(final ByteBuf nlri) {
43         final List<NextHeaders> headers = new ArrayList<>();
44         boolean end = false;
45         // we can do this as all fields will be rewritten in the cycle
46         final NextHeadersBuilder builder = new NextHeadersBuilder();
47         while (!end) {
48             final byte b = nlri.readByte();
49             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
50             builder.setOp(op);
51             builder.setValue(nlri.readUnsignedByte());
52             end = op.isEndOfList();
53             headers.add(builder.build());
54         }
55         return headers;
56     }
57 }