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