BUG 611 - MPReach/Unreach uses serializers from NlriRegistry for linkstate originated...
[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 io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
12 import org.opendaylight.protocol.util.Ipv4Util;
13 import org.opendaylight.protocol.util.Ipv6Util;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Case;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22
23 public class WithdrawnRoutesSerializer implements NlriSerializer {
24     @Override
25     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
26         PathAttributes pathAttributes = (PathAttributes) attribute;
27         PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
28         if (pathAttributes2 == null) {
29             return;
30         }
31         MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
32         if (mpUnreachNlri == null) {
33             return;
34         }
35
36         if (mpUnreachNlri.getWithdrawnRoutes() != null) {
37             if (mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationIpv4Case) {
38                 DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
39                 if (destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes() != null) {
40                     for (Ipv4Prefix ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
41                         byteAggregator.writeByte(Ipv4Util.getPrefixLength(ipv4Prefix.getValue()));
42                         byteAggregator.writeBytes(Ipv4Util.bytesForPrefixByPrefixLength(ipv4Prefix));
43                     }
44                 }
45             } else if (mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationIpv6Case) {
46                 DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
47                 if (destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes() != null) {
48                     for (Ipv6Prefix ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
49                         byteAggregator.writeByte(Ipv4Util.getPrefixLength(ipv6Prefix.getValue()));
50                         byteAggregator.writeBytes(Ipv6Util.bytesForPrefixByPrefixLength(ipv6Prefix));
51                     }
52                 }
53             }
54         }
55     }
56 }