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