Merge "BUG-611 : cleaned up advertized/withdrawn routes."
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AdvertizedRoutesSerializer.java
1 /*
2  * Copyright (c) 2014 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.util.Ipv4Util;
14 import org.opendaylight.protocol.util.Ipv6Util;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Case;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24
25 public class AdvertizedRoutesSerializer implements NlriSerializer {
26
27     @Override
28     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
29         final PathAttributes1 pathAttributes1 = ((PathAttributes) attribute).getAugmentation(PathAttributes1.class);
30         if (pathAttributes1 == null) {
31             return;
32         }
33         final MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
34         if (mpReachNlri == null) {
35             return;
36         }
37         AdvertizedRoutes routes = mpReachNlri.getAdvertizedRoutes();
38         if (routes.getDestinationType() instanceof DestinationIpv4Case) {
39             final DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) routes.getDestinationType();
40             for (final Ipv4Prefix ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
41                 byteAggregator.writeBytes(Ipv4Util.bytesForPrefixBegin(ipv4Prefix));
42             }
43         } else if (routes.getDestinationType() instanceof DestinationIpv6Case) {
44             final DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) routes.getDestinationType();
45             for (final Ipv6Prefix ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
46                 byteAggregator.writeBytes(Ipv6Util.bytesForPrefixBegin(ipv6Prefix));
47             }
48         }
49     }
50 }