Removed checkstyle warnings.
[bgpcep.git] / tcp-md5 / netty / src / main / java / org / opendaylight / bgpcep / tcpmd5 / netty / DefaultMD5SocketChannelConfig.java
1 /*
2  * Copyright (c) 2014 Robert Varga. 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.bgpcep.tcpmd5.netty;
9
10 import io.netty.buffer.ByteBufAllocator;
11 import io.netty.channel.ChannelOption;
12 import io.netty.channel.MessageSizeEstimator;
13 import io.netty.channel.RecvByteBufAllocator;
14 import io.netty.channel.socket.DefaultSocketChannelConfig;
15
16 import java.util.Map;
17
18 import org.opendaylight.bgpcep.tcpmd5.KeyAccessFactory;
19 import org.opendaylight.bgpcep.tcpmd5.KeyMapping;
20
21 public class DefaultMD5SocketChannelConfig extends DefaultSocketChannelConfig implements MD5SocketChannelConfig {
22     private final NettyKeyAccess keyAccess;
23
24     public DefaultMD5SocketChannelConfig(final MD5NioSocketChannel channel, final KeyAccessFactory keyAccessFactory) {
25         super(channel, channel.javaChannel().socket());
26         this.keyAccess = NettyKeyAccess.create(keyAccessFactory, channel.javaChannel());
27     }
28
29     @Override
30     public KeyMapping getMD5SignatureKeys() {
31         return keyAccess.getKeys();
32     }
33
34     @Override
35     public MD5SocketChannelConfig setMD5SignatureKeys(final KeyMapping keys) {
36         keyAccess.setKeys(keys);
37         return this;
38     }
39
40     @Override
41     public MD5SocketChannelConfig setTcpNoDelay(final boolean tcpNoDelay) {
42         super.setTcpNoDelay(tcpNoDelay);
43         return this;
44     }
45
46     @Override
47     public MD5SocketChannelConfig setSoLinger(final int soLinger) {
48         super.setSoLinger(soLinger);
49         return this;
50     }
51
52     @Override
53     public MD5SocketChannelConfig setSendBufferSize(final int sendBufferSize) {
54         super.setSendBufferSize(sendBufferSize);
55         return this;
56     }
57
58     @Override
59     public MD5SocketChannelConfig setReceiveBufferSize(final int receiveBufferSize) {
60         super.setReceiveBufferSize(receiveBufferSize);
61         return this;
62     }
63
64     @Override
65     public MD5SocketChannelConfig setKeepAlive(final boolean keepAlive) {
66         super.setKeepAlive(keepAlive);
67         return this;
68     }
69
70     @Override
71     public MD5SocketChannelConfig setTrafficClass(final int trafficClass) {
72         super.setTrafficClass(trafficClass);
73         return this;
74     }
75
76     @Override
77     public MD5SocketChannelConfig setReuseAddress(final boolean reuseAddress) {
78         super.setReuseAddress(reuseAddress);
79         return this;
80     }
81
82     @Override
83     public MD5SocketChannelConfig setPerformancePreferences(final int connectionTime, final int latency, final int bandwidth) {
84         super.setPerformancePreferences(connectionTime, latency, bandwidth);
85         return this;
86     }
87
88     @Override
89     public MD5SocketChannelConfig setAllowHalfClosure(final boolean allowHalfClosure) {
90         super.setAllowHalfClosure(allowHalfClosure);
91         return this;
92     }
93
94     @Override
95     public MD5SocketChannelConfig setConnectTimeoutMillis(final int connectTimeoutMillis) {
96         super.setConnectTimeoutMillis(connectTimeoutMillis);
97         return this;
98     }
99
100     @Override
101     public MD5SocketChannelConfig setMaxMessagesPerRead(final int maxMessagesPerRead) {
102         super.setMaxMessagesPerRead(maxMessagesPerRead);
103         return this;
104     }
105
106     @Override
107     public MD5SocketChannelConfig setWriteSpinCount(final int writeSpinCount) {
108         super.setWriteSpinCount(writeSpinCount);
109         return this;
110     }
111
112     @Override
113     public MD5SocketChannelConfig setAllocator(final ByteBufAllocator allocator) {
114         super.setAllocator(allocator);
115         return this;
116     }
117
118     @Override
119     public MD5SocketChannelConfig setRecvByteBufAllocator(final RecvByteBufAllocator allocator) {
120         super.setRecvByteBufAllocator(allocator);
121         return this;
122     }
123
124     @Override
125     public MD5SocketChannelConfig setAutoRead(final boolean autoRead) {
126         super.setAutoRead(autoRead);
127         return this;
128     }
129
130     @Override
131     public MD5SocketChannelConfig setMessageSizeEstimator(final MessageSizeEstimator estimator) {
132         super.setMessageSizeEstimator(estimator);
133         return this;
134     }
135
136     @Override
137     public Map<ChannelOption<?>, Object> getOptions() {
138         final Map<ChannelOption<?>, Object> opts = super.getOptions();
139         final KeyMapping keys = keyAccess.getKeys();
140         if (keys != null) {
141             opts.put(MD5ChannelOption.TCP_MD5SIG, keys);
142         }
143
144         return opts;
145     }
146
147     @Override
148     public boolean setOptions(final Map<ChannelOption<?>, ?> options) {
149         boolean setAllOptions = true;
150         if (options.containsKey(MD5ChannelOption.TCP_MD5SIG)) {
151             setAllOptions = setOption(MD5ChannelOption.TCP_MD5SIG, (KeyMapping) options.get(MD5ChannelOption.TCP_MD5SIG));
152         }
153
154         return super.setOptions(options) && setAllOptions;
155     }
156
157     @Override
158     public <T> T getOption(final ChannelOption<T> option) {
159         if (option == MD5ChannelOption.TCP_MD5SIG) {
160             @SuppressWarnings("unchecked")
161             final T ret = (T) keyAccess.getKeys();
162             return ret;
163         }
164
165         return super.getOption(option);
166     }
167
168     @Override
169     public <T> boolean setOption(final ChannelOption<T> option, final T value) {
170         if (option == MD5ChannelOption.TCP_MD5SIG) {
171             keyAccess.setKeys((KeyMapping) value);
172             return true;
173         }
174
175         return super.setOption(option, value);
176     }
177 }