a19341f5d3db52c65fb00f168ad02b841a5d4da9
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / Ipv4NlriParser.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 io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
13 import org.opendaylight.protocol.concepts.Ipv4Util;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4CaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.destination.ipv4._case.DestinationIpv4Builder;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21
22 public final class Ipv4NlriParser extends IpNlriParser implements NlriSerializer {
23
24     @Override
25     protected DestinationIpv4Case parseNlri(final ByteBuf nlri) {
26         return new DestinationIpv4CaseBuilder().setDestinationIpv4(
27                 new DestinationIpv4Builder().setIpv4Prefixes(Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(nlri))).build()).build();
28     }
29
30     @Override
31     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
32         Nlri nlri = (Nlri) attribute;
33         for (Ipv4Prefix ipv4Prefix : nlri.getNlri()) {
34             byteAggregator.writeBytes(Ipv4Util.bytesForPrefix(ipv4Prefix));
35         }
36     }
37 }