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 | |
| 20 | import java.io.Serializable; |
| 21 | import java.util.Collection; |
| 22 | import java.util.LinkedHashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Optional; |
| 26 | import java.util.stream.Collectors; |
| 27 | import java.util.stream.Stream; |
| 28 | |
| 29 | import javax.xml.bind.JAXBElement; |
| 30 | |
| 31 | import org.onosproject.netconf.rpc.ErrorInfoType; |
| 32 | import org.onosproject.netconf.rpc.ErrorSeverity; |
| 33 | import org.onosproject.netconf.rpc.ErrorTag; |
| 34 | import org.onosproject.netconf.rpc.ErrorType; |
| 35 | import org.onosproject.netconf.rpc.RpcErrorType; |
| 36 | import org.onosproject.netconf.rpc.RpcErrorType.ErrorMessage; |
| 37 | |
| 38 | import com.google.common.annotations.Beta; |
| 39 | import com.google.common.base.MoreObjects; |
| 40 | import com.google.common.base.Strings; |
| 41 | import com.google.common.collect.ImmutableList; |
| 42 | |
| 43 | /** |
| 44 | * Wrapper around {@link RpcErrorType} for ease of handling and logging. |
| 45 | * |
| 46 | * @see NetconfRpcParserUtil |
| 47 | */ |
| 48 | @Beta |
| 49 | public class NetconfRpcError { |
| 50 | |
| 51 | /** |
| 52 | * Protocol mandated error-info: {@value}. |
| 53 | */ |
| 54 | public static final String BAD_ATTRIBUTE = "bad-attribute"; |
| 55 | /** |
| 56 | * Protocol mandated error-info: {@value}. |
| 57 | */ |
| 58 | public static final String BAD_ELEMENT = "bad-element"; |
| 59 | /** |
| 60 | * Protocol mandated error-info: {@value}. |
| 61 | */ |
| 62 | public static final String OK_ELEMENT = "ok-element"; |
| 63 | /** |
| 64 | * Protocol mandated error-info: {@value}. |
| 65 | */ |
| 66 | public static final String ERR_ELEMENT = "err-element"; |
| 67 | /** |
| 68 | * Protocol mandated error-info: {@value}. |
| 69 | */ |
| 70 | public static final String NOOP_ELEMENT = "noop-element"; |
| 71 | /** |
| 72 | * Protocol mandated error-info: {@value}. |
| 73 | */ |
| 74 | public static final String BAD_NAMESPACE = "bad-namespace"; |
| 75 | |
| 76 | private final RpcErrorType msg; |
| 77 | |
| 78 | |
| 79 | public static final NetconfRpcError wrap(RpcErrorType msg) { |
| 80 | return new NetconfRpcError(msg); |
| 81 | } |
| 82 | |
| 83 | protected NetconfRpcError(RpcErrorType msg) { |
| 84 | this.msg = checkNotNull(msg); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns conceptual layer that the error occurred. |
| 89 | * |
| 90 | * @return the type |
| 91 | */ |
| 92 | public ErrorType type() { |
| 93 | return msg.getErrorType(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a tag identifying the error condition. |
| 98 | * |
| 99 | * @return the tag |
| 100 | */ |
| 101 | public ErrorTag tag() { |
| 102 | return msg.getErrorTag(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns error severity. |
| 107 | * |
| 108 | * @return severity |
| 109 | */ |
| 110 | public ErrorSeverity severity() { |
| 111 | return msg.getErrorSeverity(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns a string identifying the data-model-specific |
| 116 | * or implementation-specific error condition, if one exists. |
| 117 | * |
| 118 | * @return app tag |
| 119 | */ |
| 120 | public Optional<String> appTag() { |
| 121 | return Optional.ofNullable(msg.getErrorAppTag()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns absolute XPath expression identifying the element path |
| 126 | * to the node that is associated with the error being reported. |
| 127 | * |
| 128 | * @return XPath expression |
| 129 | */ |
| 130 | public Optional<String> path() { |
| 131 | return Optional.ofNullable(msg.getErrorPath()); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns human readable error message. |
| 136 | * |
| 137 | * @return message |
| 138 | */ |
| 139 | //@Nonnull |
| 140 | public String message() { |
| 141 | return Optional.ofNullable(msg.getErrorMessage()) |
| 142 | .map(ErrorMessage::getValue) |
| 143 | .orElse(""); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns session-id in error-info if any. |
| 148 | * |
| 149 | * @return session-id |
| 150 | */ |
| 151 | public Optional<Long> infoSessionId() { |
| 152 | return Optional.ofNullable(msg.getErrorInfo()) |
| 153 | .map(ErrorInfoType::getSessionId); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns protocol-mandated contents if any. |
| 158 | * |
| 159 | * @return Map containing protocol-mandated contents |
| 160 | * <p> |
| 161 | * Possible Map keys: |
| 162 | * {@link #BAD_ATTRIBUTE}, |
| 163 | * {@link #BAD_ELEMENT}, |
| 164 | * {@link #OK_ELEMENT}, |
| 165 | * {@link #ERR_ELEMENT}, |
| 166 | * {@link #NOOP_ELEMENT}, |
| 167 | * {@link #BAD_NAMESPACE} |
| 168 | */ |
| 169 | @Beta |
| 170 | public Map<String, String> info() { |
| 171 | return Optional.ofNullable(msg.getErrorInfo()) |
| 172 | .map(ErrorInfoType::getBadAttributeAndBadElementAndOkElement) |
| 173 | .map(Collection::stream) |
| 174 | .orElse(Stream.empty()) |
| 175 | .collect(Collectors.<JAXBElement<? extends Serializable>, |
| 176 | String, String, Map<String, String>> |
| 177 | toMap(elm -> elm.getName().getLocalPart(), |
| 178 | elm -> String.valueOf(elm.getValue()), |
| 179 | (v1, v2) -> v1, |
| 180 | LinkedHashMap::new)); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns any other elements if present. |
| 185 | * |
| 186 | * @return any other elements |
| 187 | */ |
| 188 | @Beta |
| 189 | public List<Object> infoAny() { |
| 190 | return Optional.ofNullable(msg.getErrorInfo()) |
| 191 | .map(ErrorInfoType::getAny) |
| 192 | .orElse(ImmutableList.of()); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns protocol- or data-model-specific error content. |
| 197 | * |
| 198 | * @return error info |
| 199 | */ |
| 200 | @Beta |
| 201 | public Optional<ErrorInfoType> rawInfo() { |
| 202 | return Optional.ofNullable(msg.getErrorInfo()); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public String toString() { |
| 207 | return MoreObjects.toStringHelper(this) |
| 208 | .add("type", type()) |
| 209 | .add("tag", tag()) |
| 210 | .add("severity", severity()) |
| 211 | .add("appTag", appTag().orElse(null)) |
| 212 | .add("path", path().orElse(null)) |
| 213 | .add("message", Strings.emptyToNull(message())) |
| 214 | .add("info-session-id", infoSessionId().orElse(null)) |
| 215 | .add("info", info()) |
| 216 | .omitNullValues() |
| 217 | .toString(); |
| 218 | } |
| 219 | } |