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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
17 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
20 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
21 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterIdBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class ClusterIdAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
32 private static final Logger LOG = LoggerFactory.getLogger(ClusterIdAttributeParser.class);
34 public static final int TYPE = 10;
37 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
38 final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
39 throws BGPDocumentedException, BGPTreatAsWithdrawException {
40 if (errorHandling == RevisedErrorHandling.EXTERNAL) {
41 // RFC7606 section 7.10
42 LOG.debug("Discarded CLUSTER_LIST attribute from external peer");
46 final int readable = buffer.readableBytes();
47 if (readable == 0 && errorHandling != RevisedErrorHandling.NONE) {
48 throw new BGPTreatAsWithdrawException(BGPError.ATTR_LENGTH_ERROR, "Empty CLUSTER_LIST attribute");
51 if (readable % Ipv4Util.IP4_LENGTH != 0) {
52 throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
53 "Length of CLUSTER_LIST should be a multiple of 4, but is %s", readable);
56 final int count = readable / Ipv4Util.IP4_LENGTH;
57 final List<ClusterIdentifier> list = new ArrayList<>(count);
58 for (int i = 0; i < count; ++i) {
59 list.add(new ClusterIdentifier(Ipv4Util.addressForByteBuf(buffer)));
61 builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
65 public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
66 final ClusterId cid = pathAttributes.getClusterId();
70 final List<ClusterIdentifier> cluster = cid.getCluster();
71 if (cluster == null || cluster.isEmpty()) {
74 final ByteBuf clusterIdBuffer = Unpooled.buffer();
75 for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) {
76 clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
78 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);