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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSelection;
15 import java.io.Serializable;
16 import java.util.function.Consumer;
17 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
18 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
19 import org.opendaylight.yangtools.concepts.Identifier;
22 * Options for slicing a message with {@link MessageSlicer#slice(SliceOptions)}.
24 * @author Thomas Pantelis
26 public final class SliceOptions {
27 private final Builder builder;
29 private SliceOptions(final Builder builder) {
30 this.builder = builder;
33 public Identifier getIdentifier() {
34 return builder.identifier;
37 public FileBackedOutputStream getFileBackedStream() {
38 return builder.fileBackedStream;
41 public Serializable getMessage() {
42 return builder.message;
45 public ActorRef getSendToRef() {
46 return builder.sendToRef;
49 public ActorSelection getSendToSelection() {
50 return builder.sendToSelection;
53 public ActorRef getReplyTo() {
54 return builder.replyTo;
57 public Consumer<Throwable> getOnFailureCallback() {
58 return builder.onFailureCallback;
62 * Returns a new Builder for creating MessageSlicer instances.
64 * @return a Builder instance
66 public static Builder builder() {
70 public static class Builder {
71 private Identifier identifier;
72 private FileBackedOutputStream fileBackedStream;
73 private Serializable message;
74 private ActorRef sendToRef;
75 private ActorSelection sendToSelection;
76 private ActorRef replyTo;
77 private Consumer<Throwable> onFailureCallback;
78 private boolean sealed;
81 * Sets the identifier of the component to slice.
83 * @param newIdentifier the identifier
84 * @return this Builder
86 public Builder identifier(final Identifier newIdentifier) {
88 identifier = newIdentifier;
93 * Sets the {@link FileBackedOutputStream} containing the message data to slice.
95 * @param newFileBackedStream the {@link FileBackedOutputStream}
96 * @return this Builder
98 public Builder fileBackedOutputStream(final FileBackedOutputStream newFileBackedStream) {
100 fileBackedStream = newFileBackedStream;
105 * Sets the message to slice. The message is first serialized to a {@link FileBackedOutputStream}. If the
106 * message doesn't need to be sliced, ie its serialized size is less than the maximum message slice size, then
107 * the original message is sent. Otherwise the first message slice is sent.
110 * <b>Note:</b> a {@link FileBackedOutputStreamFactory} must be set in the {@link MessageSlicer}.
112 * @param newMessage the message
113 * @param <T> the Serializable message type
114 * @return this Builder
116 public <T extends Serializable> Builder message(final T newMessage) {
118 message = newMessage;
123 * Sets the reference of the actor to which to send the message slices.
125 * @param sendTo the ActorRef
126 * @return this Builder
128 public Builder sendTo(final ActorRef sendTo) {
135 * Sets the ActorSelection to which to send the message slices.
137 * @param sendTo the ActorSelection
138 * @return this Builder
140 public Builder sendTo(final ActorSelection sendTo) {
142 sendToSelection = sendTo;
147 * Sets the reference of the actor to which message slice replies should be sent. The actor should
148 * forward the replies to the {@link MessageSlicer#handleMessage(Object)} method.
150 * @param newReplyTo the ActorRef
151 * @return this Builder
153 public Builder replyTo(final ActorRef newReplyTo) {
155 replyTo = newReplyTo;
160 * Sets the callback to be notified of failure.
162 * @param newOnFailureCallback the callback
163 * @return this Builder
165 public Builder onFailureCallback(final Consumer<Throwable> newOnFailureCallback) {
167 onFailureCallback = newOnFailureCallback;
172 * Builds a new SliceOptions instance.
174 * @return a new SliceOptions
176 public SliceOptions build() {
179 requireNonNull(identifier, "identifier must be set");
180 requireNonNull(replyTo, "replyTo must be set");
181 requireNonNull(onFailureCallback, "onFailureCallback must be set");
182 checkState(fileBackedStream == null || message == null,
183 "Only one of message and fileBackedStream can be set");
184 checkState(!(fileBackedStream == null && message == null),
185 "One of message and fileBackedStream must be set");
186 checkState(sendToRef == null || sendToSelection == null,
187 "Only one of sendToRef and sendToSelection can be set");
188 checkState(!(sendToRef == null && sendToSelection == null),
189 "One of sendToRef and sendToSelection must be set");
191 return new SliceOptions(this);
194 protected void checkSealed() {
195 checkState(!sealed, "Builder is already sealed - further modifications are not allowed");