Move route target ext comm container
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ClusterIdAttributeParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import com.google.common.collect.Lists;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
17 import org.opendaylight.protocol.util.Ipv4Util;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterIdBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
23
24 public final class ClusterIdAttributeParser implements AttributeParser, AttributeSerializer {
25
26     public static final int TYPE = 10;
27
28     @Override
29     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
30         final List<ClusterIdentifier> list = Lists.newArrayList();
31         while (buffer.isReadable()) {
32             list.add(new ClusterIdentifier(Ipv4Util.addressForByteBuf(buffer)));
33         }
34         builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
35     }
36
37     @Override
38     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
39         final ClusterId cid = pathAttributes.getClusterId();
40         if (cid == null) {
41             return;
42         }
43         final List<ClusterIdentifier> cluster = cid.getCluster();
44         if (cluster == null || cluster.isEmpty()) {
45             return;
46         }
47         final ByteBuf clusterIdBuffer = Unpooled.buffer();
48         for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) {
49             clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
50         }
51         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);
52     }
53 }