BGP statistics CLI
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / WithdrawnRoutesSerializer.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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
13 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
14 import org.opendaylight.protocol.util.ByteBufWriteUtil;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6Case;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24
25 public class WithdrawnRoutesSerializer implements NlriSerializer {
26
27     @Override
28     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
29         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
30         final Attributes2 pathAttributes2 = ((Attributes) attribute).getAugmentation(Attributes2.class);
31         if (pathAttributes2 == null) {
32             return;
33         }
34         final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
35         if (mpUnreachNlri == null) {
36             return;
37         }
38         final WithdrawnRoutes routes = mpUnreachNlri.getWithdrawnRoutes();
39         if (routes != null) {
40             serializeRoutes(routes, byteAggregator);
41         }
42     }
43
44     private static void serializeRoutes(final WithdrawnRoutes routes, final ByteBuf byteAggregator) {
45         if (routes.getDestinationType() instanceof DestinationIpv4Case) {
46             final DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) routes.getDestinationType();
47             if (destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes() != null) {
48                 for (final Ipv4Prefixes ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
49                     PathIdUtil.writePathId(ipv4Prefix.getPathId(), byteAggregator);
50                     ByteBufWriteUtil.writeMinimalPrefix(ipv4Prefix.getPrefix(), byteAggregator);
51                 }
52             }
53         } else if (routes.getDestinationType() instanceof DestinationIpv6Case) {
54             final DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) routes.getDestinationType();
55             if (destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes() != null) {
56                 for (final Ipv6Prefixes ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
57                     PathIdUtil.writePathId(ipv6Prefix.getPathId(), byteAggregator);
58                     ByteBufWriteUtil.writeMinimalPrefix(ipv6Prefix.getPrefix(), byteAggregator);
59                 }
60             }
61         }
62     }
63 }