Code clean up
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSIpv6FlowLabelHandler.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 io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractOperandParser;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
19 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
20 import org.opendaylight.protocol.bgp.flowspec.handlers.Util;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.NumericOperand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabelBuilder;
28
29 public final class FSIpv6FlowLabelHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
30     static final int FLOW_LABEL_VALUE = 13;
31
32     private static void serializeNumericFourByteValue(final List<FlowLabel> list, final ByteBuf nlriByteBuf) {
33         for (final Iterator<FlowLabel> it = list.iterator(); it.hasNext(); ) {
34             final FlowLabel label = it.next();
35             final ByteBuf protoBuf = Unpooled.buffer();
36             Util.writeShortest(label.getValue().intValue(), protoBuf);
37             NumericOneByteOperandParser.INSTANCE.serialize(label.getOp(), protoBuf.readableBytes(),
38                     !it.hasNext(), nlriByteBuf);
39             nlriByteBuf.writeBytes(protoBuf);
40         }
41     }
42
43     @Override
44     public void serializeType(final FlowspecType fsType, final ByteBuf output) {
45         Preconditions.checkArgument(fsType instanceof FlowLabelCase, "FlowLabelCase class is mandatory!");
46         output.writeByte(FLOW_LABEL_VALUE);
47         serializeNumericFourByteValue(((FlowLabelCase) fsType).getFlowLabel(), output);
48     }
49
50     @Override
51     public FlowspecType parseType(final ByteBuf buffer) {
52         return new FlowLabelCaseBuilder().setFlowLabel(parseFlowLabel(buffer)).build();
53     }
54
55     private static List<FlowLabel> parseFlowLabel(final ByteBuf nlri) {
56         final List<FlowLabel> labels = new ArrayList<>();
57         boolean end = false;
58         // we can do this as all fields will be rewritten in the cycle
59         final FlowLabelBuilder builder = new FlowLabelBuilder();
60         while (!end) {
61             final byte b = nlri.readByte();
62             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
63             builder.setOp(op);
64             final short length = AbstractOperandParser.parseLength(b);
65             builder.setValue(ByteArray.bytesToLong(ByteArray.readBytes(nlri, length)));
66             end = op.isEndOfList();
67             labels.add(builder.build());
68         }
69         return labels;
70     }
71 }