blob: 95da5508acfd1b8f4703e696d0e0f2ee89aa5f93 [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
30/**
31 * Class for default workplace description.
32 */
33public final class DefaultWorkplaceDescription implements WorkplaceDescription {
34
35 /**
36 * Name of workplace.
37 */
38 private final String name;
39
40 /**
41 * Data model of workplace(Optional).
42 */
43 private final Optional<JsonNode> optData;
44
45 /**
46 * Constructor of workplace description.
47 * @param builder workplace builder
48 */
49 private DefaultWorkplaceDescription(Builder builder) {
50 this.name = builder.name;
51 this.optData = builder.optData;
52 }
53
54 @Override
55 public String name() {
56 return this.name;
57 }
58
59 @Override
60 public Optional<JsonNode> data() {
61 return this.optData;
62 }
63
64 /**
65 * Creating workplace description from json tree.
66 * @param root root node for workplace description
67 * @return workplace description
68 * @throws WorkflowException workflow exception
69 */
70 public static DefaultWorkplaceDescription valueOf(JsonNode root) throws WorkflowException {
71
72 JsonNode node = root.at(ptr(WP_NAME));
73 if (!(node instanceof TextNode)) {
74 throw new WorkflowException("invalid workplace name for " + root);
75 }
76
77 Builder builder = builder()
78 .name(node.asText());
79
80 node = root.at(ptr(WP_DATA));
81 if (node != null && !(node instanceof MissingNode)) {
82 if (!(node instanceof ObjectNode) && !(node instanceof ArrayNode)) {
83 throw new WorkflowException("invalid workplace data for " + root);
84 }
85 builder.data(node);
86 }
87
88 return builder.build();
89 }
90
91 private static String ptr(String field) {
92 return "/" + field;
93 }
94
95 @Override
96 public JsonNode toJson() {
97 ObjectNode root = JsonNodeFactory.instance.objectNode();
98 root.put(WP_NAME, name());
99 if (data().isPresent()) {
100 root.put(WP_DATA, data().get());
101 }
102 return root;
103 }
104
105 @Override
106 public String toString() {
107 return MoreObjects.toStringHelper(getClass())
108 .add("name", name())
109 .add("optData", data())
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 description.
123 */
124 public static class Builder {
125
126 /**
127 * Workplace name.
128 */
129 private String name;
130
131 /**
132 * Workplace optData model.
133 */
134 private Optional<JsonNode> optData = Optional.empty();
135
136 /**
137 * List of workflow.
138 */
139 private List<DefaultWorkflowDescription> workflowDescs = new ArrayList<DefaultWorkflowDescription>();
140
141 /**
142 * Sets workplace name.
143 * @param name workplace name
144 * @return builder
145 */
146 public Builder name(String name) {
147 this.name = name;
148 return this;
149 }
150
151 /**
152 * Sets optData model.
153 * @param data workplace optData model
154 * @return builder
155 */
156 public Builder data(JsonNode data) {
157 this.optData = Optional.of(data);
158 return this;
159 }
160
161 /**
162 * Builds workplace description from builder.
163 * @return instance of workflow description
164 */
165 public DefaultWorkplaceDescription build() {
166 return new DefaultWorkplaceDescription(this);
167 }
168 }
169}