083230736455f7b9d5e6273906c5138bbcdc415e
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / open / GracefulCapabilityHandler.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.open;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.CapabilityParser;
21 import org.opendaylight.protocol.bgp.parser.spi.CapabilitySerializer;
22 import org.opendaylight.protocol.bgp.parser.spi.CapabilityUtil;
23 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParameters;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.GracefulRestartCapability;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.GracefulRestartCapability.RestartFlags;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.GracefulRestartCapabilityBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.Tables;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.Tables.AfiFlags;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.TablesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public final class GracefulCapabilityHandler implements CapabilityParser, CapabilitySerializer {
41     public static final int CODE = 64;
42
43     private static final Logger LOG = LoggerFactory.getLogger(GracefulCapabilityHandler.class);
44
45     // Restart flag size, in bits
46     private static final int RESTART_FLAGS_SIZE = 4;
47     private static final int RESTART_FLAG_STATE = 0x8000;
48
49     // Restart timer size, in bits
50     private static final int TIMER_SIZE = 12;
51     private static final int TIMER_TOPBITS_MASK = 0x0F;
52
53     // Size of the capability header
54     private static final int HEADER_SIZE = (RESTART_FLAGS_SIZE + TIMER_SIZE) / Byte.SIZE;
55
56     // Length of each AFI/SAFI array member, in bytes
57     private static final int PER_AFI_SAFI_SIZE = 4;
58
59     private static final short AFI_FLAG_FORWARDING_STATE = 0x80;
60
61     private static final int MAX_RESTART_TIME = 4095;
62
63     private final AddressFamilyRegistry afiReg;
64     private final SubsequentAddressFamilyRegistry safiReg;
65
66     public GracefulCapabilityHandler(final AddressFamilyRegistry afiReg,
67             final SubsequentAddressFamilyRegistry safiReg) {
68         this.afiReg = requireNonNull(afiReg);
69         this.safiReg = requireNonNull(safiReg);
70     }
71
72     private void serializeTables(final List<Tables> tables, final ByteBuf bytes) {
73         if (tables == null) {
74             return;
75         }
76         for (final Tables t : tables) {
77             final Class<? extends AddressFamily> afi = t.getAfi();
78             final Integer afival = this.afiReg.numberForClass(afi);
79             Preconditions.checkArgument(afival != null, "Unhandled address family " + afi);
80             bytes.writeShort(afival);
81             final Class<? extends SubsequentAddressFamily> safi = t.getSafi();
82             final Integer safival = this.safiReg.numberForClass(safi);
83             Preconditions.checkArgument(safival != null, "Unhandled subsequent address family " + safi);
84             bytes.writeByte(safival);
85             if (t.getAfiFlags() != null && t.getAfiFlags().isForwardingState()) {
86                 bytes.writeByte(AFI_FLAG_FORWARDING_STATE);
87             } else {
88                 bytes.writeByte(0);
89             }
90         }
91     }
92
93     private ByteBuf serializeCapability(final GracefulRestartCapability grace) {
94         final List<Tables> tables = grace.getTables();
95         final int tablesSize = tables != null ? tables.size() : 0;
96         final ByteBuf bytes = Unpooled.buffer(HEADER_SIZE + PER_AFI_SAFI_SIZE * tablesSize);
97         Uint16 time = grace.getRestartTime();
98         if (time == null) {
99             time = Uint16.ZERO;
100         }
101         final int timeval = time.toJava();
102         Preconditions.checkArgument(timeval >= 0 && timeval <= MAX_RESTART_TIME, "Restart time is " + time);
103         final GracefulRestartCapability.RestartFlags flags = grace.getRestartFlags();
104         if (flags != null && flags.isRestartState()) {
105             bytes.writeShort(RESTART_FLAG_STATE | timeval);
106         } else {
107             bytes.writeShort(timeval);
108         }
109         serializeTables(tables, bytes);
110         return bytes;
111     }
112
113     @Override
114     public void serializeCapability(final CParameters capability, final ByteBuf byteAggregator) {
115         final CParameters1 aug = capability.augmentation(CParameters1.class);
116         if (aug == null) {
117             return;
118         }
119         final GracefulRestartCapability grace = aug.getGracefulRestartCapability();
120         if (grace != null) {
121             final ByteBuf bytes = serializeCapability(grace);
122             CapabilityUtil.formatCapability(CODE, bytes, byteAggregator);
123         }
124     }
125
126     @Override
127     public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
128         final GracefulRestartCapabilityBuilder cb = new GracefulRestartCapabilityBuilder();
129
130         final int flagBits = buffer.getByte(0) >> RESTART_FLAGS_SIZE;
131         cb.setRestartFlags(new RestartFlags((flagBits & Byte.SIZE) != 0));
132
133         final int timer = ((buffer.readUnsignedByte() & TIMER_TOPBITS_MASK) << Byte.SIZE) + buffer.readUnsignedByte();
134         cb.setRestartTime(timer);
135
136         final List<Tables> tables = new ArrayList<>();
137         while (buffer.readableBytes() != 0) {
138             final int afiVal = buffer.readShort();
139             final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal);
140             if (afi == null) {
141                 LOG.debug("Ignoring GR capability for unknown address family {}", afiVal);
142                 buffer.skipBytes(PER_AFI_SAFI_SIZE - 2);
143                 continue;
144             }
145             final int safiVal = buffer.readUnsignedByte();
146             final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
147             if (safi == null) {
148                 LOG.debug("Ignoring GR capability for unknown subsequent address family {}", safiVal);
149                 buffer.skipBytes(1);
150                 continue;
151             }
152             final int flags = buffer.readUnsignedByte();
153             tables.add(new TablesBuilder().setAfi(afi).setSafi(safi)
154                 .setAfiFlags(new AfiFlags((flags & AFI_FLAG_FORWARDING_STATE) != 0)).build());
155         }
156         cb.setTables(tables);
157         return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder()
158             .setGracefulRestartCapability(cb.build()).build()).build();
159     }
160 }