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