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 static java.util.Objects.requireNonNull;
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.FlowspecTypeParser;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
19 import org.opendaylight.protocol.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.NumericOperand;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpTypeCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.IcmpTypeCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.type._case.Types;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.icmp.type._case.TypesBuilder;
27 public final class FSIcmpTypeHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
28 public static final int ICMP_TYPE_VALUE = 7;
31 public void serializeType(final FlowspecType fsType, final ByteBuf output) {
32 Preconditions.checkArgument(fsType instanceof IcmpTypeCase, "IcmpTypeCase class is mandatory!");
33 output.writeByte(ICMP_TYPE_VALUE);
34 NumericOneByteOperandParser.INSTANCE.serialize(((IcmpTypeCase) fsType).getTypes(), output);
38 public FlowspecType parseType(final ByteBuf buffer) {
39 requireNonNull(buffer, "input buffer is null, missing data to parse.");
40 return new IcmpTypeCaseBuilder().setTypes(parseIcmpType(buffer)).build();
43 private static List<Types> parseIcmpType(final ByteBuf nlri) {
44 final List<Types> icmps = new ArrayList<>();
46 // we can do this as all fields will be rewritten in the cycle
47 final TypesBuilder builder = new TypesBuilder();
49 final byte b = nlri.readByte();
50 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
52 builder.setValue(ByteBufUtils.readUint8(nlri));
53 end = op.isEndOfList();
54 icmps.add(builder.build());