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