Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSDscpHandler.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.protocol.bgp.flowspec.handlers.Util;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DscpCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.DscpCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.dscp._case.Dscps;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.dscp._case.DscpsBuilder;
26
27 public final class FSDscpHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
28     public static final int DSCP_VALUE = 11;
29
30     @Override
31     public void serializeType(FlowspecType fsType, ByteBuf output) {
32         Preconditions.checkArgument(fsType instanceof DscpCase, "DscpCase class is mandatory!");
33         output.writeByte(DSCP_VALUE);
34         serializeDscps(((DscpCase) fsType).getDscps(), output);
35     }
36
37     @Override
38     public FlowspecType parseType(ByteBuf buffer) {
39         requireNonNull(buffer, "input buffer is null, missing data to parse.");
40         return new DscpCaseBuilder().setDscps(parseDscps(buffer)).build();
41     }
42
43     private static final void serializeDscps(final List<Dscps> dscps, final ByteBuf nlriByteBuf) {
44         for (final Dscps dscp : dscps) {
45             NumericOneByteOperandParser.INSTANCE.serialize(dscp.getOp(), 1, nlriByteBuf);
46             Util.writeShortest(dscp.getValue().getValue(), nlriByteBuf);
47         }
48     }
49
50     private static List<Dscps> parseDscps(final ByteBuf nlri) {
51         final List<Dscps> dscps = new ArrayList<>();
52         boolean end = false;
53         // we can do this as all fields will be rewritten in the cycle
54         final DscpsBuilder builder = new DscpsBuilder();
55         while (!end) {
56             final byte b = nlri.readByte();
57             // RFC does not specify operator
58             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
59             builder.setOp(op);
60             builder.setValue(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp(nlri.readUnsignedByte()));
61             end = op.isEndOfList();
62             dscps.add(builder.build());
63         }
64         return dscps;
65     }
66 }