blob: 13a8f462f2c4b5620e9007ee547ad915ce90a8c8 [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.JsonNodeFactory;
20import com.fasterxml.jackson.databind.node.MissingNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.fasterxml.jackson.databind.node.TextNode;
23import com.google.common.base.MoreObjects;
24
25import java.net.URI;
26
jaegonkime0f45b52018-10-09 20:23:26 +090027import static org.onosproject.workflow.api.CheckCondition.check;
28
jaegonkim6a7b5242018-09-12 23:09:42 +090029
30/**
31 * Class for default workflow description.
32 */
33public final class DefaultWorkflowDescription implements WorkflowDescription {
34
35 /**
36 * Workplace Name.
37 */
38 private String workplaceName;
39
40 /**
41 * Workflow ID.
42 */
43 private URI id;
44
45 /**
46 * Workflow data model.
47 */
48 private JsonNode data;
49
50 /**
51 * Constructor of workflow description.
52 * @param builder workflow description builder
53 */
54 private DefaultWorkflowDescription(Builder builder) {
55 this.workplaceName = builder.workplaceName;
56 this.id = builder.id;
57 this.data = builder.data;
58 }
59
60 @Override
61 public String workplaceName() {
62 return this.workplaceName;
63 }
64
65 @Override
66 public URI id() {
67 return this.id;
68 }
69
70 @Override
71 public String workflowContextName() {
72 return DefaultWorkflowContext.nameBuilder(id(), workplaceName());
73 }
74
75 @Override
76 public JsonNode data() {
77 return this.data;
78 }
79
80 /**
81 * Creating workflow description from json tree.
82 * @param root root node for workflow description
83 * @return workflow description
84 * @throws WorkflowException workflow exception
85 */
86 public static DefaultWorkflowDescription valueOf(JsonNode root) throws WorkflowException {
87
88 JsonNode node = root.at(ptr(WF_WORKPLACE));
89 if (!(node instanceof TextNode)) {
90 throw new WorkflowException("invalid workflow workplace for " + root);
91 }
92 String wfWorkplaceName = node.asText();
93
94 node = root.at(ptr(WF_ID));
95 if (!(node instanceof TextNode)) {
96 throw new WorkflowException("invalid workflow id for " + root);
97 }
98 URI wfId = URI.create(node.asText());
99
100 node = root.at(ptr(WF_DATA));
101 if (node instanceof MissingNode) {
102 throw new WorkflowException("invalid workflow data for " + root);
103 }
104 JsonNode wfData = node;
105
106 return builder()
107 .workplaceName(wfWorkplaceName)
108 .id(wfId)
109 .data(wfData)
110 .build();
111 }
112
113 private static String ptr(String field) {
114 return "/" + field;
115 }
116
117 @Override
118 public JsonNode toJson() {
119 ObjectNode root = JsonNodeFactory.instance.objectNode();
120 root.put(WF_WORKPLACE, workplaceName());
121 root.put(WF_ID, id().toString());
122 root.put(WF_DATA, data());
123 return root;
124 }
125
126 @Override
127 public String toString() {
128 return MoreObjects.toStringHelper(getClass())
129 .add("workplace", workplaceName())
130 .add("id", id())
131 .add("data", data())
132 .toString();
133 }
134
135 /**
136 * Gets builder instance.
137 * @return builder instance
138 */
139 public static Builder builder() {
140 return new Builder();
141 }
142
143 /**
144 * Builder for workflow description.
145 */
146 public static class Builder {
147
148 /**
149 * Workplace name.
150 */
151 private String workplaceName;
152
153 /**
154 * Workflow ID.
155 */
156 private URI id;
157
158 /**
159 * Workflow data model.
160 */
161 private JsonNode data;
162
163 /**
164 * Sets workplace name.
165 * @param workplaceName workplace name
166 * @return builder
167 */
168 public Builder workplaceName(String workplaceName) {
169 this.workplaceName = workplaceName;
170 return this;
171 }
172
173 /**
174 * Sets workflow id.
175 * @param id workflow ID
176 * @return builder
177 */
178 public Builder id(URI id) {
179 this.id = id;
180 return this;
181 }
182
183 /**
184 * Sets workflow id.
185 * @param id workflow ID string
186 * @return builder
187 */
188 public Builder id(String id) {
189 this.id = URI.create(id);
190 return this;
191 }
192
193 /**
194 * Sets workflow data model.
195 * @param data workflow data model
196 * @return builder
197 */
198 public Builder data(JsonNode data) {
199 this.data = data;
200 return this;
201 }
202
203 /**
204 * Builds workflow description from builder.
205 * @return instance of workflow description
jaegonkime0f45b52018-10-09 20:23:26 +0900206 * @throws WorkflowException workflow exception
jaegonkim6a7b5242018-09-12 23:09:42 +0900207 */
jaegonkime0f45b52018-10-09 20:23:26 +0900208 public DefaultWorkflowDescription build() throws WorkflowException {
209 check(workplaceName != null, "workplaceName is invalid");
210 check(id != null, "id is invalid");
211 check(data != null, "data is invalid");
jaegonkim6a7b5242018-09-12 23:09:42 +0900212 return new DefaultWorkflowDescription(this);
213 }
214 }
215}