e22f419732fcf8c4cbca1eef6c0f72d7899a16c2
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / Configuration.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. 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.restconf.nb.rfc8040.streams;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.MoreObjects;
13
14 /**
15  * Restconf configuration holder and verifier.
16  */
17 public class Configuration {
18
19     private static final int MAX_FRAGMENT_LENGTH = 65535;
20
21     private final int maximumFragmentLength;
22     private final int idleTimeout;
23     private final int heartbeatInterval;
24     private final boolean useSSE;
25
26     /**
27      * Creation of the restconf configuration holder with verification of input parameters.
28      *
29      * @param maximumFragmentLength Maximum web-socket fragment length in number of Unicode code units (characters)
30      *                              (exceeded message length leads to fragmentation of messages).
31      * @param idleTimeout           Maximum idle time of web-socket session before the session is closed (milliseconds).
32      * @param heartbeatInterval     Interval in milliseconds between sending of ping control frames.
33      * @param useSSE                when is true use SSE else use WS
34      */
35     public Configuration(final int maximumFragmentLength, final int idleTimeout, final int heartbeatInterval,
36             final boolean useSSE) {
37         checkArgument(idleTimeout > 0, "Idle timeout must be specified by positive value.");
38         checkArgument(maximumFragmentLength >= 0 && maximumFragmentLength < MAX_FRAGMENT_LENGTH,
39                 "Maximum fragment length must be disabled (0) or specified by positive value less than 64 KB.");
40         checkArgument(heartbeatInterval >= 0, "Heartbeat ping interval must be "
41                 + "disabled (0) or specified by positive value.");
42
43         this.maximumFragmentLength = maximumFragmentLength;
44         this.idleTimeout = idleTimeout;
45         this.heartbeatInterval = heartbeatInterval;
46         this.useSSE = useSSE;
47     }
48
49     public int getMaximumFragmentLength() {
50         return maximumFragmentLength;
51     }
52
53     public int getIdleTimeout() {
54         return idleTimeout;
55     }
56
57     public int getHeartbeatInterval() {
58         return heartbeatInterval;
59     }
60
61     /**
62      * Getter for useSSE.
63      *
64      * @return true in situation when we use SSE. False in situation when we use WS
65      */
66     public boolean isUseSSE() {
67         return useSSE;
68     }
69
70     @Override
71     public String toString() {
72         return MoreObjects.toStringHelper(this)
73                 .add("maximumFragmentLength", maximumFragmentLength)
74                 .add("idleTimeout", idleTimeout)
75                 .add("heartbeatInterval", heartbeatInterval)
76                 .add("useSSE", useSSE)
77                 .toString();
78     }
79 }