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