Bug 5503 - remove package cyclic dependency in BGP-FS
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSIcmpTypeHandler.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.flowspec.flowspec.type.IcmpTypeCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpTypeCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.type._case.Types;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.type._case.TypesBuilder;
23
24 public final class FSIcmpTypeHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
25     public static final int ICMP_TYPE_VALUE = 7;
26
27     @Override
28     public void serializeType(FlowspecType fsType, ByteBuf output) {
29         Preconditions.checkArgument(fsType instanceof IcmpTypeCase, "IcmpTypeCase class is mandatory!");
30         output.writeByte(ICMP_TYPE_VALUE);
31         NumericOneByteOperandParser.INSTANCE.serialize(((IcmpTypeCase) fsType).getTypes(), 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 IcmpTypeCaseBuilder().setTypes(parseIcmpType(buffer)).build();
38     }
39
40     private static List<Types> parseIcmpType(final ByteBuf nlri) {
41         final List<Types> icmps = new ArrayList<>();
42         boolean end = false;
43         // we can do this as all fields will be rewritten in the cycle
44         final TypesBuilder builder = new TypesBuilder();
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             icmps.add(builder.build());
52         }
53         return icmps;
54     }
55 }