YANG revision dates mass-update
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSSourcePortHandler.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.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.NumericTwoByteOperandParser;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.NumericOperand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.FlowspecType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.SourcePortCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.SourcePortCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePorts;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.source.port._case.SourcePortsBuilder;
28
29 public final class FSSourcePortHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
30     public static final int SOURCE_PORT_VALUE = 6;
31
32     @Override
33     public void serializeType(FlowspecType fsType, ByteBuf output) {
34         Preconditions.checkArgument(fsType instanceof SourcePortCase, "SourcePortCase class is mandatory!");
35         output.writeByte(SOURCE_PORT_VALUE);
36         NumericTwoByteOperandParser.INSTANCE.serialize(((SourcePortCase) fsType).getSourcePorts(), output);
37     }
38
39     @Override
40     public FlowspecType parseType(ByteBuf buffer) {
41         requireNonNull(buffer, "input buffer is null, missing data to parse.");
42         return new SourcePortCaseBuilder().setSourcePorts(parseSourcePort(buffer)).build();
43     }
44
45     private static List<SourcePorts> parseSourcePort(final ByteBuf nlri) {
46         final List<SourcePorts> ports = new ArrayList<>();
47         boolean end = false;
48         // we can do this as all fields will be rewritten in the cycle
49         final SourcePortsBuilder builder = new SourcePortsBuilder();
50         while (!end) {
51             final byte b = nlri.readByte();
52             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
53             builder.setOp(op);
54             final short length = AbstractOperandParser.parseLength(b);
55             builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length)));
56             end = op.isEndOfList();
57             ports.add(builder.build());
58         }
59         return ports;
60     }
61 }