blob: e8125f7eb8456cae6f7b0f971ac9b1217be1400a [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.ArrayNode;
20import com.fasterxml.jackson.databind.node.JsonNodeFactory;
21import com.fasterxml.jackson.databind.node.MissingNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import com.fasterxml.jackson.databind.node.TextNode;
24import com.google.common.base.MoreObjects;
25
26import java.util.ArrayList;
27import java.util.List;
28import java.util.Optional;
29
jaegonkime0f45b52018-10-09 20:23:26 +090030import static org.onosproject.workflow.api.CheckCondition.check;
31
jaegonkim6a7b5242018-09-12 23:09:42 +090032/**
33 * Class for default workplace description.
34 */
35public final class DefaultWorkplaceDescription implements WorkplaceDescription {
36
37 /**
38 * Name of workplace.
39 */
40 private final String name;
41
42 /**
43 * Data model of workplace(Optional).
44 */
45 private final Optional<JsonNode> optData;
46
47 /**
48 * Constructor of workplace description.
49 * @param builder workplace builder
50 */
51 private DefaultWorkplaceDescription(Builder builder) {
52 this.name = builder.name;
53 this.optData = builder.optData;
54 }
55
56 @Override
57 public String name() {
58 return this.name;
59 }
60
61 @Override
62 public Optional<JsonNode> data() {
63 return this.optData;
64 }
65
66 /**
67 * Creating workplace description from json tree.
68 * @param root root node for workplace description
69 * @return workplace description
70 * @throws WorkflowException workflow exception
71 */
72 public static DefaultWorkplaceDescription valueOf(JsonNode root) throws WorkflowException {
73
74 JsonNode node = root.at(ptr(WP_NAME));
75 if (!(node instanceof TextNode)) {
76 throw new WorkflowException("invalid workplace name for " + root);
77 }
78
79 Builder builder = builder()
80 .name(node.asText());
81
82 node = root.at(ptr(WP_DATA));
83 if (node != null && !(node instanceof MissingNode)) {
84 if (!(node instanceof ObjectNode) && !(node instanceof ArrayNode)) {
85 throw new WorkflowException("invalid workplace data for " + root);
86 }
87 builder.data(node);
88 }
89
90 return builder.build();
91 }
92
93 private static String ptr(String field) {
94 return "/" + field;
95 }
96
97 @Override
98 public JsonNode toJson() {
99 ObjectNode root = JsonNodeFactory.instance.objectNode();
100 root.put(WP_NAME, name());
101 if (data().isPresent()) {
102 root.put(WP_DATA, data().get());
103 }
104 return root;
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .add("name", name())
111 .add("optData", data())
112 .toString();
113 }
114
115 /**
116 * Gets builder instance.
117 * @return builder instance
118 */
119 public static Builder builder() {
120 return new Builder();
121 }
122
123 /**
124 * Builder for workplace description.
125 */
126 public static class Builder {
127
128 /**
129 * Workplace name.
130 */
131 private String name;
132
133 /**
134 * Workplace optData model.
135 */
136 private Optional<JsonNode> optData = Optional.empty();
137
138 /**
139 * List of workflow.
140 */
141 private List<DefaultWorkflowDescription> workflowDescs = new ArrayList<DefaultWorkflowDescription>();
142
143 /**
144 * Sets workplace name.
145 * @param name workplace name
146 * @return builder
147 */
148 public Builder name(String name) {
149 this.name = name;
150 return this;
151 }
152
153 /**
154 * Sets optData model.
155 * @param data workplace optData model
156 * @return builder
157 */
158 public Builder data(JsonNode data) {
159 this.optData = Optional.of(data);
160 return this;
161 }
162
163 /**
164 * Builds workplace description from builder.
165 * @return instance of workflow description
jaegonkime0f45b52018-10-09 20:23:26 +0900166 * @throws WorkflowException workflow exception
jaegonkim6a7b5242018-09-12 23:09:42 +0900167 */
jaegonkime0f45b52018-10-09 20:23:26 +0900168 public DefaultWorkplaceDescription build() throws WorkflowException {
169 check(name != null, "name is invalid");
jaegonkim6a7b5242018-09-12 23:09:42 +0900170 return new DefaultWorkplaceDescription(this);
171 }
172 }
173}