e134d09e7f728acc70e95a1ecd1c0aff5017e1db
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputOutput.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.controller.cluster.datastore.node.utils.stream;
9
10 import com.google.common.annotations.Beta;
11 import java.io.DataInput;
12 import java.io.DataOutput;
13 import java.io.IOException;
14 import org.eclipse.jdt.annotation.NonNull;
15
16 @Beta
17 public final class NormalizedNodeInputOutput {
18     private NormalizedNodeInputOutput() {
19         throw new UnsupportedOperationException();
20     }
21
22     /**
23      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
24      * and validates that the input contains a valid NormalizedNode stream.
25      *
26      * @param input the DataInput to read from
27      * @return a new {@link NormalizedNodeDataInput} instance
28      * @throws IOException if an error occurs reading from the input
29      */
30     public static NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
31         return new VersionedNormalizedNodeDataInput(input).delegate();
32     }
33
34     /**
35      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
36      * perform any initial validation of the input stream.
37      *
38      * @param input the DataInput to read from
39      * @return a new {@link NormalizedNodeDataInput} instance
40      */
41     public static NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
42         return new VersionedNormalizedNodeDataInput(input);
43     }
44
45     /**
46      * Creates a new {@link NormalizedNodeDataOutput} instance that writes to the given output and latest current
47      * stream version.
48      *
49      * @param output the DataOutput to write to
50      * @return a new {@link NormalizedNodeDataOutput} instance
51      */
52     public static NormalizedNodeDataOutput newDataOutput(final @NonNull DataOutput output) {
53         return new NeonSR2NormalizedNodeOutputStreamWriter(output);
54     }
55
56     /**
57      * Creates a new {@link NormalizedNodeDataOutput} instance that writes to the given output.
58      *
59      * @param output the DataOutput to write to
60      * @param version Streaming version to use
61      * @return a new {@link NormalizedNodeDataOutput} instance
62      */
63     public static NormalizedNodeDataOutput newDataOutput(final @NonNull DataOutput output,
64             final @NonNull NormalizedNodeStreamVersion version) {
65         switch (version) {
66             case LITHIUM:
67                 return new LithiumNormalizedNodeOutputStreamWriter(output);
68             case NEON_SR2:
69                 return new NeonSR2NormalizedNodeOutputStreamWriter(output);
70             case SODIUM_SR1:
71                 return new SodiumSR1DataOutput(output);
72             default:
73                 throw new IllegalStateException("Unhandled version " + version);
74         }
75     }
76
77 }