Yuta HIGUCHI | d1c413b | 2018-02-20 14:52:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.netconf; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.EnumSet; |
| 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.slf4j.Logger; |
| 27 | import com.google.common.annotations.Beta; |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | import com.google.common.collect.ImmutableList; |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | |
| 32 | /** |
| 33 | * Representation of rpc-reply. |
| 34 | * |
| 35 | * @see NetconfRpcParserUtil |
| 36 | */ |
| 37 | @Beta |
| 38 | public class NetconfRpcReply { |
| 39 | |
| 40 | private static final Logger log = getLogger(NetconfRpcReply.class); |
| 41 | |
| 42 | public enum Type { |
| 43 | OK, |
| 44 | ERROR, |
| 45 | RESPONSE |
| 46 | } |
| 47 | |
| 48 | private final Set<NetconfRpcReply.Type> replies; |
| 49 | |
| 50 | private final String messageId; |
| 51 | |
| 52 | // rpc-reply is ok or (0+ rpc-error and/or 0+ rpc-response) |
| 53 | |
| 54 | private final List<NetconfRpcError> errors; |
| 55 | private final List<String> responses; |
| 56 | |
| 57 | protected NetconfRpcReply(NetconfRpcReply.Builder builder) { |
| 58 | this.messageId = checkNotNull(builder.messageId); |
| 59 | this.replies = ImmutableSet.copyOf(builder.replies); |
| 60 | this.errors = ImmutableList.copyOf(builder.errors); |
| 61 | this.responses = ImmutableList.copyOf(builder.responses); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns message-id of this message. |
| 66 | * |
| 67 | * @return message-id |
| 68 | */ |
| 69 | //@Nonnull |
| 70 | public String messageId() { |
| 71 | return messageId; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns true if ok reply. |
| 76 | * |
| 77 | * @return true if ok reply. |
| 78 | */ |
| 79 | public boolean isOk() { |
| 80 | return replies.contains(Type.OK); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns true if reply contains rpc-error. |
| 85 | * |
| 86 | * @return true if reply contains rpc-error |
| 87 | */ |
| 88 | public boolean hasError() { |
| 89 | return replies.contains(Type.ERROR); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns list of rpc-errors in rpc-reply. |
| 94 | * |
| 95 | * @return list of rpc-errors in rpc-reply. |
| 96 | */ |
| 97 | public List<NetconfRpcError> errors() { |
| 98 | return errors; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns list of rpc responses in rpc-reply. |
| 103 | * |
| 104 | * @return list of rpc responses in rpc-reply. |
| 105 | */ |
| 106 | public List<String> responses() { |
| 107 | return responses; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(this) |
| 113 | .add("messageId", messageId) |
| 114 | .add("replies", replies) |
| 115 | .add("errors", errors) |
| 116 | .add("responses", responses) |
| 117 | .toString(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates builder to build {@link NetconfRpcReply}. |
| 122 | * @return created builder |
| 123 | */ |
| 124 | public static NetconfRpcReply.Builder builder() { |
| 125 | return new Builder(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Builder to build {@link NetconfRpcReply}. |
| 130 | */ |
| 131 | public static final class Builder { |
| 132 | private String messageId; |
| 133 | private Set<NetconfRpcReply.Type> replies = EnumSet.noneOf(NetconfRpcReply.Type.class); |
| 134 | private List<NetconfRpcError> errors = new ArrayList<>(); |
| 135 | private List<String> responses = new ArrayList<>(); |
| 136 | |
| 137 | private Builder() {} |
| 138 | |
| 139 | /** |
| 140 | * Builder method to set message-id. |
| 141 | * |
| 142 | * @param messageId field to set |
| 143 | * @return builder |
| 144 | */ |
| 145 | public NetconfRpcReply.Builder withMessageId(String messageId) { |
| 146 | this.messageId = messageId; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Builder method to adding error parameter. |
| 152 | * |
| 153 | * @param error field to add |
| 154 | * @return builder |
| 155 | */ |
| 156 | public NetconfRpcReply.Builder addError(NetconfRpcError error) { |
| 157 | this.replies.add(Type.ERROR); |
| 158 | this.errors.add(error); |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Builder method for adding response parameter. |
| 164 | * |
| 165 | * @param response field to add |
| 166 | * @return builder |
| 167 | */ |
| 168 | public NetconfRpcReply.Builder addResponses(String response) { |
| 169 | this.replies.add(Type.RESPONSE); |
| 170 | this.responses.add(response); |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Builds ok reply message. |
| 176 | * @return ok |
| 177 | */ |
| 178 | public NetconfRpcReply buildOk() { |
| 179 | if (!replies.isEmpty()) { |
| 180 | log.warn("Unexpected item in replies area: {}", replies); |
| 181 | } |
| 182 | this.replies.add(Type.OK); |
| 183 | return build(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Builder method of the builder. |
| 188 | * |
| 189 | * @return built class |
| 190 | */ |
| 191 | public NetconfRpcReply build() { |
| 192 | if (replies.isEmpty()) { |
| 193 | log.warn("Empty rpc-reply?"); |
| 194 | } |
| 195 | return new NetconfRpcReply(this); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | } |