Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSIpProtocolHandler.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIps;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIpsBuilder;
25
26 public final class FSIpProtocolHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
27     public static final int IP_PROTOCOL_VALUE = 3;
28
29     @Override
30     public void serializeType(FlowspecType fsType, ByteBuf output) {
31         Preconditions.checkArgument(fsType instanceof ProtocolIpCase, "ProtocolIpCase class is mandatory!");
32         output.writeByte(IP_PROTOCOL_VALUE);
33         NumericOneByteOperandParser.INSTANCE.serialize(((ProtocolIpCase) fsType).getProtocolIps(), output);
34     }
35
36     @Override
37     public FlowspecType parseType(ByteBuf buffer) {
38         requireNonNull(buffer, "input buffer is null, missing data to parse.");
39         return new ProtocolIpCaseBuilder().setProtocolIps(parseProtocolIp(buffer)).build();
40     }
41
42     private static List<ProtocolIps> parseProtocolIp(final ByteBuf nlri) {
43         final List<ProtocolIps> ips = new ArrayList<>();
44         boolean end = false;
45         // we can do this as all fields will be rewritten in the cycle
46         final ProtocolIpsBuilder builder = new ProtocolIpsBuilder();
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             ips.add(builder.build());
54         }
55         return ips;
56     }
57 }