bca983cd1504025d027c04986fd36059d6d172fa
[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.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.rev171207.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ClusterId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ClusterIdBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25
26 public final class ClusterIdAttributeParser implements AttributeParser, AttributeSerializer {
27
28     public static final int TYPE = 10;
29
30     @Override
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)));
35         }
36         builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
37     }
38
39     @Override
40     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
41         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
42         final ClusterId cid = ((Attributes) attribute).getClusterId();
43         if (cid == null) {
44             return;
45         }
46         final List<ClusterIdentifier> cluster = cid.getCluster();
47         if (cluster == null  || cluster.isEmpty()) {
48             return;
49         }
50         final ByteBuf clusterIdBuffer = Unpooled.buffer();
51         for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) {
52             clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
53         }
54         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);
55     }
56 }