2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec;
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.IcmpCodeCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.IcmpCodeCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.code._case.Codes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.flowspec.type.icmp.code._case.CodesBuilder;
24 public final class FSIcmpCodeHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
25 public static final int ICMP_CODE_VALUE = 8;
28 public void serializeType(FlowspecType fsType, ByteBuf output) {
29 Preconditions.checkArgument(fsType instanceof IcmpCodeCase, "IcmpCodeCase class is mandatory!");
30 output.writeByte(ICMP_CODE_VALUE);
31 NumericOneByteOperandParser.INSTANCE.serialize(((IcmpCodeCase) fsType).getCodes(), output);
35 public FlowspecType parseType(ByteBuf buffer) {
36 Preconditions.checkNotNull(buffer, "input buffer is null, missing data to parse.");
37 return new IcmpCodeCaseBuilder().setCodes(parseIcmpCode(buffer)).build();
40 private static List<Codes> parseIcmpCode(final ByteBuf nlri) {
41 final List<Codes> codes = new ArrayList<>();
43 // we can do this as all fields will be rewritten in the cycle
44 final CodesBuilder builder = new CodesBuilder();
46 final byte b = nlri.readByte();
47 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
49 builder.setValue(nlri.readUnsignedByte());
50 end = op.isEndOfList();
51 codes.add(builder.build());