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.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;
23 public final class FSIpProtocolHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
24 public static final int IP_PROTOCOL_VALUE = 3;
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);
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();
39 private static List<ProtocolIps> parseProtocolIp(final ByteBuf nlri) {
40 final List<ProtocolIps> ips = new ArrayList<>();
42 // we can do this as all fields will be rewritten in the cycle
43 final ProtocolIpsBuilder builder = new ProtocolIpsBuilder();
45 final byte b = nlri.readByte();
46 final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
48 builder.setValue(nlri.readUnsignedByte());
49 end = op.isEndOfList();
50 ips.add(builder.build());