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