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