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