2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.messaging;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import com.google.common.base.Preconditions;
13 import java.io.Serializable;
14 import java.util.function.Consumer;
15 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
16 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
17 import org.opendaylight.yangtools.concepts.Identifier;
20 * Options for slicing a message with {@link MessageSlicer#slice(SliceOptions)}.
22 * @author Thomas Pantelis
24 public final class SliceOptions {
25 private final Builder builder;
27 private SliceOptions(final Builder builder) {
28 this.builder = builder;
31 public Identifier getIdentifier() {
32 return builder.identifier;
35 public FileBackedOutputStream getFileBackedStream() {
36 return builder.fileBackedStream;
39 public Serializable getMessage() {
40 return builder.message;
43 public ActorRef getSendToRef() {
44 return builder.sendToRef;
47 public ActorSelection getSendToSelection() {
48 return builder.sendToSelection;
51 public ActorRef getReplyTo() {
52 return builder.replyTo;
55 public Consumer<Throwable> getOnFailureCallback() {
56 return builder.onFailureCallback;
60 * Returns a new Builder for creating MessageSlicer instances.
62 * @return a Builder instance
64 public static Builder builder() {
68 public static class Builder {
69 private Identifier identifier;
70 private FileBackedOutputStream fileBackedStream;
71 private Serializable message;
72 private ActorRef sendToRef;
73 private ActorSelection sendToSelection;
74 private ActorRef replyTo;
75 private Consumer<Throwable> onFailureCallback;
76 private boolean sealed;
79 * Sets the identifier of the component to slice.
81 * @param newIdentifier the identifier
82 * @return this Builder
84 public Builder identifier(final Identifier newIdentifier) {
86 identifier = newIdentifier;
91 * Sets the {@link FileBackedOutputStream} containing the message data to slice.
93 * @param newFileBackedStream the {@link FileBackedOutputStream}
94 * @return this Builder
96 public Builder fileBackedOutputStream(final FileBackedOutputStream newFileBackedStream) {
98 fileBackedStream = newFileBackedStream;
103 * Sets the message to slice. The message is first serialized to a {@link FileBackedOutputStream}. If the
104 * message doesn't need to be sliced, ie its serialized size is less than the maximum message slice size, then
105 * the original message is sent. Otherwise the first message slice is sent.
108 * <b>Note:</b> a {@link FileBackedOutputStreamFactory} must be set in the {@link MessageSlicer}.
110 * @param newMessage the message
111 * @param <T> the Serializable message type
112 * @return this Builder
114 public <T extends Serializable> Builder message(final T newMessage) {
116 message = newMessage;
121 * Sets the reference of the actor to which to send the message slices.
123 * @param sendTo the ActorRef
124 * @return this Builder
126 public Builder sendTo(final ActorRef sendTo) {
133 * Sets the ActorSelection to which to send the message slices.
135 * @param sendTo the ActorSelection
136 * @return this Builder
138 public Builder sendTo(final ActorSelection sendTo) {
140 sendToSelection = sendTo;
145 * Sets the reference of the actor to which message slice replies should be sent. The actor should
146 * forward the replies to the {@link MessageSlicer#handleMessage(Object)} method.
148 * @param newReplyTo the ActorRef
149 * @return this Builder
151 public Builder replyTo(final ActorRef newReplyTo) {
153 replyTo = newReplyTo;
158 * Sets the callback to be notified of failure.
160 * @param newOnFailureCallback the callback
161 * @return this Builder
163 public Builder onFailureCallback(final Consumer<Throwable> newOnFailureCallback) {
165 onFailureCallback = newOnFailureCallback;
170 * Builds a new SliceOptions instance.
172 * @return a new SliceOptions
174 public SliceOptions build() {
177 Preconditions.checkNotNull(identifier, "identifier must be set");
178 Preconditions.checkNotNull(replyTo, "replyTo must be set");
179 Preconditions.checkNotNull(onFailureCallback, "onFailureCallback must be set");
180 Preconditions.checkState(fileBackedStream == null || message == null,
181 "Only one of message and fileBackedStream can be set");
182 Preconditions.checkState(!(fileBackedStream == null && message == null),
183 "One of message and fileBackedStream must be set");
184 Preconditions.checkState(sendToRef == null || sendToSelection == null,
185 "Only one of sendToRef and sendToSelection can be set");
186 Preconditions.checkState(!(sendToRef == null && sendToSelection == null),
187 "One of sendToRef and sendToSelection must be set");
189 return new SliceOptions(this);
192 protected void checkSealed() {
193 Preconditions.checkState(!sealed, "Builder is already sealed - further modifications are not allowed");