blob: 4a4715ed062d935c6c9925cf45172d1f917a9afb [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
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 */
16package org.onosproject.workflow.api;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.MissingNode;
20import com.fasterxml.jackson.databind.node.TextNode;
21import com.google.common.base.MoreObjects;
22
23/**
24 * Class of workflow RPC description.
25 */
26public final class DefaultRpcDescription implements RpcDescription {
27
28 /**
29 * Workflow RPC operation.
30 */
31 private final String op;
32
33 /**
34 * Parameters.
35 */
36 private final JsonNode params;
37
38 /**
39 * Invocation ID.
40 */
41 private final String id;
42
43 /**
44 * Constructor of workplace description.
45 * @param builder workplace builder
46 */
47 private DefaultRpcDescription(Builder builder) {
48 this.op = builder.op;
49 this.params = builder.params;
50 this.id = builder.id;
51 }
52
53 @Override
54 public String op() {
55 return this.op;
56 }
57
58 @Override
59 public JsonNode params() {
60 return this.params;
61 }
62
63 @Override
64 public String id() {
65 return this.id;
66 }
67
68 /**
69 * Creating workflow RPC description from json tree.
70 * @param root root node for workflow RPC description
71 * @return workflow RPC description
72 * @throws WorkflowException workflow exception
73 */
74 public static DefaultRpcDescription valueOf(JsonNode root) throws WorkflowException {
75
76 JsonNode node = root.at(RPC_OP_PTR);
77 if (!(node instanceof TextNode)) {
78 throw new WorkflowException("invalid RPC operation for " + root);
79 }
80 String rpcOp = node.asText();
81
82 node = root.at(RPC_PARAMS_PTR);
83 if (node instanceof MissingNode) {
84 throw new WorkflowException("invalid RPC parameters for " + root);
85 }
86 JsonNode rpcParams = node;
87
88 node = root.at(RPC_ID_PTR);
89 if (!(node instanceof TextNode)) {
90 throw new WorkflowException("invalid RPC invocation ID for " + root);
91 }
92 String rpcId = node.asText();
93
94
95 return builder()
96 .setOp(rpcOp)
97 .setParams(rpcParams)
98 .setId(rpcId)
99 .build();
100 }
101
102 @Override
103 public String toString() {
104 return MoreObjects.toStringHelper(getClass())
105 .add("op", op())
106 .add("params", params())
107 .add("id", id())
108 .toString();
109 }
110
111 /**
112 * Gets builder instance.
113 * @return builder instance
114 */
115 public static Builder builder() {
116 return new Builder();
117 }
118
119 /**
120 * Builder for workplace RPC description.
121 */
122 public static class Builder {
123
124 /**
125 * Workflow RPC operation.
126 */
127 private String op;
128
129 /**
130 * Parameters.
131 */
132 private JsonNode params;
133
134 /**
135 * Invocation ID.
136 */
137 private String id;
138
139 /**
140 * Sets workflow RPC operation.
141 * @param op workflow RPC operation
142 * @return builder
143 */
144 public Builder setOp(String op) {
145 this.op = op;
146 return this;
147 }
148
149 /**
150 * Sets workflow RPC parameters.
151 * @param params workflow RPC parameters
152 * @return builder
153 */
154 public Builder setParams(JsonNode params) {
155 this.params = params;
156 return this;
157 }
158
159 /**
160 * Sets workflow RPC invocation ID.
161 * @param id workflow invocation ID
162 * @return builder
163 */
164 public Builder setId(String id) {
165 this.id = id;
166 return this;
167 }
168
169 /**
170 * Builds workplace RPC description from builder.
171 * @return instance of workflow RPC description
172 */
173 public DefaultRpcDescription build() {
174 return new DefaultRpcDescription(this);
175 }
176 }
177}