2 * Copyright (c) 2013 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.parser.impl.message.update;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
18 import org.opendaylight.protocol.util.Ipv4Util;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterIdBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
26 public final class ClusterIdAttributeParser implements AttributeParser, AttributeSerializer {
28 public static final int TYPE = 10;
31 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
32 final List<ClusterIdentifier> list = Lists.newArrayList();
33 while (buffer.isReadable()) {
34 list.add(new ClusterIdentifier(Ipv4Util.addressForByteBuf(buffer)));
36 builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
40 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
41 Preconditions.checkArgument(attribute instanceof Attributes,
42 "Attribute parameter is not a PathAttribute object.");
43 final ClusterId cid = ((Attributes) attribute).getClusterId();
47 final List<ClusterIdentifier> cluster = cid.getCluster();
48 if (cluster == null || cluster.isEmpty()) {
51 final ByteBuf clusterIdBuffer = Unpooled.buffer();
52 for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) {
53 clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
55 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);