Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | e3813a9 | 2017-07-29 14:10:31 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.cluster.messaging.impl; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 20 | import org.onosproject.core.HybridLogicalTime; |
| 21 | |
| 22 | /** |
| 23 | * Internal reply message. |
| 24 | */ |
| 25 | public final class InternalReply extends InternalMessage { |
| 26 | |
| 27 | /** |
| 28 | * Message status. |
| 29 | */ |
| 30 | public enum Status { |
| 31 | |
| 32 | // NOTE: For backwards compatibility enum constant IDs should not be changed. |
| 33 | |
| 34 | /** |
| 35 | * All ok. |
| 36 | */ |
| 37 | OK(0), |
| 38 | |
| 39 | /** |
| 40 | * Response status signifying no registered handler. |
| 41 | */ |
| 42 | ERROR_NO_HANDLER(1), |
| 43 | |
| 44 | /** |
| 45 | * Response status signifying an exception handling the message. |
| 46 | */ |
| 47 | ERROR_HANDLER_EXCEPTION(2), |
| 48 | |
| 49 | /** |
| 50 | * Response status signifying invalid message structure. |
| 51 | */ |
| 52 | PROTOCOL_EXCEPTION(3); |
| 53 | |
| 54 | private final int id; |
| 55 | |
| 56 | Status(int id) { |
| 57 | this.id = id; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the unique status ID. |
| 62 | * |
| 63 | * @return the unique status ID. |
| 64 | */ |
| 65 | public int id() { |
| 66 | return id; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the status enum associated with the given ID. |
| 71 | * |
| 72 | * @param id the status ID. |
| 73 | * @return the status enum for the given ID. |
| 74 | */ |
| 75 | public static Status forId(int id) { |
| 76 | switch (id) { |
| 77 | case 0: |
| 78 | return OK; |
| 79 | case 1: |
| 80 | return ERROR_NO_HANDLER; |
| 81 | case 2: |
| 82 | return ERROR_HANDLER_EXCEPTION; |
| 83 | case 3: |
| 84 | return PROTOCOL_EXCEPTION; |
| 85 | default: |
| 86 | throw new IllegalArgumentException("Unknown status ID " + id); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private final Status status; |
| 92 | |
| 93 | public InternalReply(int preamble, |
| 94 | HybridLogicalTime time, |
| 95 | long id, |
| 96 | Status status) { |
| 97 | this(preamble, time, id, new byte[0], status); |
| 98 | } |
| 99 | |
| 100 | public InternalReply(int preamble, |
| 101 | HybridLogicalTime time, |
| 102 | long id, |
| 103 | byte[] payload, |
| 104 | Status status) { |
| 105 | super(preamble, time, id, payload); |
| 106 | this.status = status; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Type type() { |
| 111 | return Type.REPLY; |
| 112 | } |
| 113 | |
| 114 | public Status status() { |
| 115 | return status; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public String toString() { |
| 120 | return MoreObjects.toStringHelper(this) |
| 121 | .add("time", time()) |
| 122 | .add("id", id()) |
| 123 | .add("status", status()) |
| 124 | .add("payload", ByteArraySizeHashPrinter.of(payload())) |
| 125 | .toString(); |
| 126 | } |
| 127 | } |