Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / tlvs / LinkIdTlvParser.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.linkstate.impl.tlvs;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkLrIdentifiers;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Uint32;
17 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
18
19 public final class LinkIdTlvParser implements LinkstateTlvParser.LinkstateTlvSerializer<LinkLrIdentifiers>,
20         LinkstateTlvParser<LinkLrIdentifiers> {
21     private static final int LINK_LR_IDENTIFIERS = 258;
22
23     @Override
24     public void serializeTlvBody(final LinkLrIdentifiers tlv, final ByteBuf body) {
25         ByteBufUtils.writeOrZero(body, tlv.getLinkLocalIdentifier());
26         ByteBufUtils.writeOrZero(body, tlv.getLinkRemoteIdentifier());
27     }
28
29     @Override
30     public LinkLrIdentifiers parseTlvBody(final ByteBuf value) {
31         final Uint32 localId = ByteBufUtils.readUint32(value);
32         final Uint32 remoteId = ByteBufUtils.readUint32(value);
33         return new AnonymousLinkLrIdentifiers(localId, remoteId);
34     }
35
36     @Override
37     public QName getTlvQName() {
38         return LinkLrIdentifiers.QNAME;
39     }
40
41     @Override
42     public int getType() {
43         return LINK_LR_IDENTIFIERS;
44     }
45
46     private static final class AnonymousLinkLrIdentifiers implements LinkLrIdentifiers {
47         private final Uint32 localId;
48         private final Uint32 remoteId;
49
50         AnonymousLinkLrIdentifiers(final Uint32 localId, final Uint32 remoteId) {
51             this.localId = requireNonNull(localId);
52             this.remoteId = requireNonNull(remoteId);
53         }
54
55         @Override
56         public Class<LinkLrIdentifiers> implementedInterface() {
57             return LinkLrIdentifiers.class;
58         }
59
60         @Override
61         public Uint32 getLinkRemoteIdentifier() {
62             return remoteId;
63         }
64
65         @Override
66         public Uint32 getLinkLocalIdentifier() {
67             return localId;
68         }
69     }
70 }