blob: 0bc9502293d2fbd21bcdd4b7004bdcef91b52148 [file] [log] [blame]
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -07001/*
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.d.config.sync.operation;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Objects;
22
23import org.apache.commons.lang3.tuple.Pair;
24import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation;
25import org.onosproject.yang.model.ResourceId;
26
27import com.google.common.annotations.Beta;
28import com.google.common.base.MoreObjects;
29import com.google.common.collect.ImmutableList;
30
31
32@Beta
33public final class SetResponse {
34
35 // partially borrowed from io.grpc.Status.Code,
36 // might want to borrow all of them
37 public enum Code {
38 OK,
39 CANCELLED,
40
41 UNKNOWN,
42
43 INVALID_ARGUMENT,
44
45 NOT_FOUND,
46 ALREADY_EXISTS,
47
48 FAILED_PRECONDITION,
49 ABORTED,
50 UNAVAILABLE,
51 }
52
53 private final Collection<Pair<Operation, ResourceId>> subjects;
54
55 private final SetResponse.Code code;
56
57 // human readable error message for logging purpose
58 private final String message;
59
60 SetResponse(Collection<Pair<Operation, ResourceId>> subjects,
61 SetResponse.Code code,
62 String message) {
63 this.subjects = ImmutableList.copyOf(subjects);
64 this.code = checkNotNull(code);
65 this.message = checkNotNull(message);
66 }
67
68 public Collection<Pair<Operation, ResourceId>> subjects() {
69 return subjects;
70 }
71
72 public Code code() {
73 return code;
74 }
75
76 public String message() {
77 return message;
78 }
79
80
81 /**
82 * Creates SetResponse instance from request.
83 *
84 * @param request original request this response corresponds to
85 * @param code response status code
86 * @param message human readable error message for logging purpose.
87 * can be left empty string on OK response.
88 * @return SetResponse instance
89 */
90 public static SetResponse response(SetRequest request,
91 Code code,
92 String message) {
93 return new SetResponse(request.subjects(), code, checkNotNull(message));
94 }
95
96 /**
97 * Creates successful SetResponce instance from request.
98 *
99 * @param request original request this response corresponds to
100 * @return SetResponse instance
101 */
102 public static SetResponse ok(SetRequest request) {
103 return new SetResponse(request.subjects(), Code.OK, "");
104 }
105
106 @Override
107 public int hashCode() {
108 return Objects.hash(subjects, code, message);
109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (this == obj) {
114 return true;
115 }
116 if (obj instanceof SetResponse) {
117 SetResponse that = (SetResponse) obj;
118 return Objects.equals(this.subjects, that.subjects) &&
119 Objects.equals(this.code, that.code) &&
120 Objects.equals(this.message, that.message);
121 }
122 return false;
123 }
124
125 @Override
126 public String toString() {
127 return MoreObjects.toStringHelper(getClass())
128 .add("code", code)
129 .add("subjects", subjects)
130 .add("message", message)
131 .toString();
132 }
133
134
135
136}