Merge "Feature : binding-bridge"
[jsonrpc.git] / bus / messagelib / src / main / java / org / opendaylight / jsonrpc / bus / messagelib / EndpointBuilders.java
1 /*
2  * Copyright (c) 2018 Lumina Networks, Inc. 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.jsonrpc.bus.messagelib;
9
10 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.DEFAULT_PROXY_RETRY_COUNT;
11 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.DEFAULT_PROXY_RETRY_DELAY;
12 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.DEFAULT_TIMEOUT;
13 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.PARAM_PROXY_RETRY_COUNT;
14 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.PARAM_PROXY_RETRY_DELAY;
15 import static org.opendaylight.jsonrpc.bus.messagelib.MessageLibraryConstants.PARAM_TIMEOUT;
16
17 import java.net.URISyntaxException;
18
19 /**
20  * Fluent builders to simply creation of endpoints.
21  *
22  * @author <a href="mailto:richard.kosegi@gmail.com">Richard Kosegi</a>
23  * @since Nov 16, 2018
24  */
25 public final class EndpointBuilders {
26     private EndpointBuilders() {
27         // no instantiation of this class
28     }
29
30     public static class EndpointBuilder {
31         private AbstractTransportFactory factory;
32
33         EndpointBuilder(AbstractTransportFactory factory) {
34             this.factory = factory;
35         }
36
37         /**
38          * Create new builder for {@link RequesterSession} or requester proxy.
39          *
40          * @return {@link RequesterBuilder} instance
41          */
42         public RequesterBuilder requester() {
43             return new RequesterBuilder(factory);
44         }
45
46         public ResponderBuilder responder() {
47             return new ResponderBuilder(factory);
48         }
49
50         public PublisherBuilder publisher() {
51             return new PublisherBuilder(factory);
52         }
53
54         public SubscriberBuilder subscriber() {
55             return new SubscriberBuilder(factory);
56         }
57     }
58
59     /**
60      * Base of other endpoint builders.
61      */
62     public abstract static class BaseEndpointBuilder<T extends BaseEndpointBuilder<?>> {
63         protected final AbstractTransportFactory factory;
64         protected boolean useCache = false;
65
66         BaseEndpointBuilder(AbstractTransportFactory factory) {
67             this.factory = factory;
68         }
69
70         /**
71          * Enable endpoint cache (which is disabled by default).
72          *
73          * @return this builder instance
74          */
75         @SuppressWarnings("unchecked")
76         public T useCache() {
77             useCache = true;
78             return (T) this;
79         }
80     }
81
82     /**
83      * Builder for {@link RequesterSession} or requester proxy.
84      */
85     public static final class RequesterBuilder extends BaseEndpointBuilder<RequesterBuilder> {
86         private int proxyRetryCount = DEFAULT_PROXY_RETRY_COUNT;
87         private long proxyRetryDelay = DEFAULT_PROXY_RETRY_DELAY;
88         private long requestTimeout = DEFAULT_TIMEOUT;
89
90         private RequesterBuilder(AbstractTransportFactory factory) {
91             super(factory);
92         }
93
94         /**
95          * Configure request timeout.
96          *
97          * @param requestTimeoutMilliseconds request timeout value in milliseconds
98          * @return this builder instance
99          */
100         public RequesterBuilder withRequestTimeout(long requestTimeoutMilliseconds) {
101             this.requestTimeout = requestTimeoutMilliseconds;
102             return this;
103         }
104
105         /**
106          * Configure proxy retry parameters.
107          *
108          * @param retryCount number of retries before giving-up
109          * @param delayMiliseconds delay between retries, in milliseconds
110          * @return this builder instance
111          */
112         public RequesterBuilder withProxyConfig(int retryCount, long delayMiliseconds) {
113             this.proxyRetryCount = retryCount;
114             this.proxyRetryDelay = delayMiliseconds;
115             return this;
116         }
117
118         /**
119          * Create requester proxy using given API contract and URI.
120          *
121          * @param api API contract to create proxy for
122          * @param uri remote responder endpoint
123          * @param <T> API contract class
124          * @return proxy object of given API class
125          * @throws URISyntaxException if URI is invalid
126          */
127         public <T extends AutoCloseable> T createProxy(Class<T> api, String uri) throws URISyntaxException {
128             String modified = uri;
129             modified = Util.injectQueryParam(modified, PARAM_PROXY_RETRY_COUNT, String.valueOf(proxyRetryCount));
130             modified = Util.injectQueryParam(modified, PARAM_PROXY_RETRY_DELAY, String.valueOf(proxyRetryDelay));
131             modified = Util.injectQueryParam(modified, PARAM_TIMEOUT, String.valueOf(requestTimeout));
132             return factory.createRequesterProxy(api, modified, !useCache);
133         }
134
135         /**
136          * Create {@link RequesterSession} using given {@link ReplyMessageHandler} and URI.
137          *
138          * @param uri remote responder endpoint
139          * @param handler handler used to handle responses
140          * @return {@link RequesterSession}
141          * @throws URISyntaxException if URI is invalid
142          */
143         public RequesterSession create(String uri, ReplyMessageHandler handler) throws URISyntaxException {
144             return factory.createRequester(uri, handler, !useCache);
145         }
146     }
147
148     /**
149      * Builder for {@link RequesterSession} or requester proxy.
150      */
151     public static final class PublisherBuilder extends BaseEndpointBuilder<PublisherBuilder> {
152         private PublisherBuilder(AbstractTransportFactory factory) {
153             super(factory);
154         }
155
156         /**
157          * Create publisher proxy.
158          *
159          * @param api API contract of publisher
160          * @param uri local endpoint to bind to
161          * @param <T> API contract type
162          * @return proxy for given API
163          * @throws URISyntaxException if URI is invalid
164          */
165         public <T extends AutoCloseable> T createProxy(Class<T> api, String uri) throws URISyntaxException {
166             return factory.createPublisherProxy(api, uri, !useCache);
167         }
168     }
169
170     /**
171      * Builder of {@link SubscriberSession}.
172      */
173     public static final class SubscriberBuilder extends BaseEndpointBuilder<SubscriberBuilder> {
174         private SubscriberBuilder(AbstractTransportFactory factory) {
175             super(factory);
176         }
177
178         /**
179          * Create new {@link SubscriberSession} using provided implementation and endpoint.
180          *
181          * @param uri remote endpoint to subscribe to
182          * @param handler handler that will be invoked on incoming notification
183          * @param <T> type of handler
184          * @return {@link SubscriberSession}
185          * @throws URISyntaxException if URI is invalid
186          */
187         public <T extends AutoCloseable> SubscriberSession createSubscriber(String uri, T handler)
188                 throws URISyntaxException {
189             return factory.createSubscriber(uri, handler, !useCache);
190         }
191     }
192
193     /**
194      * Builder of {@link ResponderSession}.
195      */
196     public static final class ResponderBuilder extends BaseEndpointBuilder<ResponderBuilder> {
197         private ResponderBuilder(AbstractTransportFactory factory) {
198             super(factory);
199         }
200
201         /**
202          * Create {@link ResponderBuilder} using provided instance and local endpoint to bound to.
203          *
204          * @param uri URI to bound to
205          * @param handler service instance that will be invoked on incoming request
206          * @param <T> type of handler
207          * @return {@link ResponderSession}
208          * @throws URISyntaxException if URI is invalid
209          */
210         public <T extends AutoCloseable> ResponderSession create(String uri, T handler) throws URISyntaxException {
211             return factory.createResponder(uri, handler, !useCache);
212         }
213     }
214 }