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