blob: c5805d8678daf067b9b64ccb8e3c298ec4893ff7 [file] [log] [blame]
Yuta HIGUCHId1c413b2018-02-20 14:52:00 -08001/*
2 * Copyright 2017-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 */
16package org.onosproject.netconf;
17
18import java.util.List;
19
20import com.google.common.annotations.Beta;
21import com.google.common.collect.ImmutableList;
22
23/**
24 * Represents class of errors related to NETCONF rpc messaging.
25 */
26@Beta
27public class NetconfRpcException extends RuntimeException {
28
29 private static final long serialVersionUID = -5947975698207522820L;
30
31 private final List<NetconfRpcError> error;
32
33 /**
34 * @param errors RPC error message body
35 */
36 public NetconfRpcException(List<NetconfRpcError> errors) {
37 super(getErrorMsg(errors));
38 this.error = ImmutableList.copyOf(errors);
39 }
40
41 /**
42 * @param errors RPC error message body
43 * @param message describing the error
44 */
45 public NetconfRpcException(List<NetconfRpcError> errors, String message) {
46 super(message);
47 this.error = ImmutableList.copyOf(errors);
48 }
49
50 /**
51 * @param errors RPC error message body
52 * @param cause of this exception
53 */
54 public NetconfRpcException(List<NetconfRpcError> errors, Throwable cause) {
55 super(getErrorMsg(errors), cause);
56 this.error = ImmutableList.copyOf(errors);
57 }
58
59 /**
60 * @param errors RPC error message body
61 * @param message describing the error
62 * @param cause of this exception
63 */
64 public NetconfRpcException(List<NetconfRpcError> errors, String message, Throwable cause) {
65 super(message, cause);
66 this.error = ImmutableList.copyOf(errors);
67 }
68
69 /**
70 * Retrieves rpc-error details.
71 *
72 * @return the rpc-errors
73 */
74 public List<NetconfRpcError> rpcErrors() {
75 return error;
76 }
77
78
79 @Override
80 public String toString() {
81 return super.toString() + " " + error;
82 }
83
84 /**
85 * Gets the first non-empty error message or {@code ""}.
86 * @param errors to fetch message from
87 * @return first non-empty error message or {@code ""}
88 */
89 private static String getErrorMsg(List<NetconfRpcError> errors) {
90 return errors.stream()
91 .map(NetconfRpcError::message)
92 .filter(s -> !s.isEmpty())
93 .findFirst().orElse("");
94 }
95
96}