Bug 5503 - remove package cyclic dependency in BGP-FS
[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 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.rev150807.NumericOperand;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.ProtocolIpCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.ProtocolIpCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIps;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIpsBuilder;
23
24 public final class FSIpProtocolHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
25     public static final int IP_PROTOCOL_VALUE = 3;
26
27     @Override
28     public void serializeType(FlowspecType fsType, ByteBuf output) {
29         Preconditions.checkArgument(fsType instanceof ProtocolIpCase, "ProtocolIpCase class is mandatory!");
30         output.writeByte(IP_PROTOCOL_VALUE);
31         NumericOneByteOperandParser.INSTANCE.serialize(((ProtocolIpCase) fsType).getProtocolIps(), output);
32     }
33
34     @Override
35     public FlowspecType parseType(ByteBuf buffer) {
36         Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
37         return new ProtocolIpCaseBuilder().setProtocolIps(parseProtocolIp(buffer)).build();
38     }
39
40     private static List<ProtocolIps> parseProtocolIp(final ByteBuf nlri) {
41         final List<ProtocolIps> ips = new ArrayList<>();
42         boolean end = false;
43         // we can do this as all fields will be rewritten in the cycle
44         final ProtocolIpsBuilder builder = new ProtocolIpsBuilder();
45         while (!end) {
46             final byte b = nlri.readByte();
47             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
48             builder.setOp(op);
49             builder.setValue(nlri.readUnsignedByte());
50             end = op.isEndOfList();
51             ips.add(builder.build());
52         }
53         return ips;
54     }
55 }