MVPN RFC6514 Extendend communities
[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.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, final SubsequentAddressFamilyRegistry safiReg) {
67         this.afiReg = requireNonNull(afiReg);
68         this.safiReg = requireNonNull(safiReg);
69     }
70
71     private void serializeTables(final List<Tables> tables, final ByteBuf bytes) {
72         if (tables == null) {
73             return;
74         }
75         for (final Tables t : tables) {
76             final Class<? extends AddressFamily> afi = t.getAfi();
77             final Integer afival = this.afiReg.numberForClass(afi);
78             Preconditions.checkArgument(afival != null, "Unhandled address family " + afi);
79             bytes.writeShort(afival);
80             final Class<? extends SubsequentAddressFamily> safi = t.getSafi();
81             final Integer safival = this.safiReg.numberForClass(safi);
82             Preconditions.checkArgument(safival != null, "Unhandled subsequent address family " + safi);
83             bytes.writeByte(safival);
84             if (t.getAfiFlags() != null && t.getAfiFlags().isForwardingState()) {
85                 bytes.writeByte(AFI_FLAG_FORWARDING_STATE);
86             } else {
87                 bytes.writeZero(1);
88             }
89         }
90     }
91
92     private ByteBuf serializeCapability(final GracefulRestartCapability grace) {
93         final List<Tables> tables = grace.getTables();
94         final int tablesSize = (tables != null) ? tables.size() : 0;
95         final ByteBuf bytes = Unpooled.buffer(HEADER_SIZE + (PER_AFI_SAFI_SIZE * tablesSize));
96         int timeval = 0;
97         Integer time = grace.getRestartTime();
98         if (time == null) {
99             time = 0;
100         }
101         Preconditions.checkArgument(time >= 0 && time <= MAX_RESTART_TIME, "Restart time is " + time);
102         timeval = time;
103         final GracefulRestartCapability.RestartFlags flags = grace.getRestartFlags();
104         if (flags != null && flags.isRestartState()) {
105             writeUnsignedShort(RESTART_FLAG_STATE | timeval, bytes);
106         } else {
107             writeUnsignedShort(timeval, bytes);
108         }
109         serializeTables(tables, bytes);
110         return bytes;
111     }
112
113     @Override
114     public void serializeCapability(final CParameters capability, final ByteBuf byteAggregator) {
115         if (capability.getAugmentation(CParameters1.class) == null
116             || capability.getAugmentation(CParameters1.class).getGracefulRestartCapability() == null) {
117             return;
118         }
119         final GracefulRestartCapability grace = capability.getAugmentation(CParameters1.class).getGracefulRestartCapability();
120
121         final ByteBuf bytes = serializeCapability(grace);
122
123         CapabilityUtil.formatCapability(CODE, bytes, byteAggregator);
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).setAfiFlags(new AfiFlags((flags & AFI_FLAG_FORWARDING_STATE) != 0)).build());
154         }
155         cb.setTables(tables);
156         return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder()
157             .setGracefulRestartCapability(cb.build()).build()).build();
158     }
159 }