blob: 6f87411468053399f0ef25ecda3c4ca8d7ddd1a5 [file] [log] [blame]
jaegonkime0f45b52018-10-09 20:23:26 +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.BooleanNode;
21import com.fasterxml.jackson.databind.node.IntNode;
22import com.fasterxml.jackson.databind.node.MissingNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import com.fasterxml.jackson.databind.node.TextNode;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.lang.annotation.Annotation;
29import java.lang.reflect.Field;
30import java.util.ArrayList;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Map;
34import java.util.Objects;
35
36/**
37 * Class for injecting json data model on the work-let execution context.
38 */
39public class JsonDataModelInjector {
40
41 private static final Logger log = LoggerFactory.getLogger(JsonDataModelInjector.class);
42
43 /**
44 * Injects data model to work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +053045 *
jaegonkime0f45b52018-10-09 20:23:26 +090046 * @param worklet work-let to be injected
47 * @param context workflow context
48 * @throws WorkflowException workflow exception
49 */
50 public void inject(Worklet worklet, WorkflowContext context) throws WorkflowException {
51
52 handle(worklet, context, this::injectModel);
53 }
54
55 /**
56 * Inhales data model from work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +053057 *
jaegonkime0f45b52018-10-09 20:23:26 +090058 * @param worklet work-let to be inhaled
59 * @param context workflow context
60 * @throws WorkflowException workflow exception
61 */
62 public void inhale(Worklet worklet, WorkflowContext context) throws WorkflowException {
63
64 handle(worklet, context, this::inhaleModel);
65 }
66
67 private void handle(Worklet worklet, WorkflowContext context, DataModelFieldBehavior func)
68 throws WorkflowException {
69 Class cl = worklet.getClass();
70 List<Field> fields = getInheritedFields(cl);
71 if (Objects.isNull(fields)) {
72 log.error("Invalid fields on {}", cl);
73 return;
74 }
75
mohamedrahilra4545fb2019-02-07 18:43:01 +053076 for (Field field : fields) {
jaegonkime0f45b52018-10-09 20:23:26 +090077 Annotation[] annotations = field.getAnnotations();
78 if (Objects.isNull(annotations)) {
79 continue;
80 }
mohamedrahilra4545fb2019-02-07 18:43:01 +053081 for (Annotation annotation : annotations) {
jaegonkime0f45b52018-10-09 20:23:26 +090082 if (!(annotation instanceof JsonDataModel)) {
83 continue;
84 }
85 JsonDataModel model = (JsonDataModel) annotation;
86 func.apply(worklet, context, field, model);
87 }
88 }
89 }
90
91 private static List<Field> getInheritedFields(Class<?> type) {
92 List<Field> fields = new ArrayList<Field>();
93
94 Class<?> cl = type;
95 while (cl != null && cl != Object.class) {
96 for (Field field : cl.getDeclaredFields()) {
97 if (!field.isSynthetic()) {
98 fields.add(field);
99 }
100 }
101 cl = cl.getSuperclass();
102 }
103 return fields;
104 }
105
106 /**
107 * Functional interface for json data model annotated field behavior.
108 */
109 @FunctionalInterface
110 public interface DataModelFieldBehavior {
111 void apply(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
112 throws WorkflowException;
113 }
114
115 private static Map<Class, DataModelFieldBehavior> injectTypeMap = new HashMap<>();
mohamedrahilra4545fb2019-02-07 18:43:01 +0530116
jaegonkime0f45b52018-10-09 20:23:26 +0900117 static {
118 injectTypeMap.put(String.class, JsonDataModelInjector::injectText);
119 injectTypeMap.put(Integer.class, JsonDataModelInjector::injectInteger);
120 injectTypeMap.put(Boolean.class, JsonDataModelInjector::injectBoolean);
121 injectTypeMap.put(JsonNode.class, JsonDataModelInjector::injectJsonNode);
122 injectTypeMap.put(ArrayNode.class, JsonDataModelInjector::injectArrayNode);
123 injectTypeMap.put(ObjectNode.class, JsonDataModelInjector::injectObjectNode);
124 }
125
126 /**
127 * Injects data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530128 *
jaegonkime0f45b52018-10-09 20:23:26 +0900129 * @param worklet work-let
130 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530131 * @param field the field of work-let
132 * @param model data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900133 * @throws WorkflowException workflow exception
134 */
135 private void injectModel(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
136 throws WorkflowException {
137
mohamedrahilra4545fb2019-02-07 18:43:01 +0530138 DataModelFieldBehavior behavior = injectTypeMap.get(field.getType());
jaegonkime0f45b52018-10-09 20:23:26 +0900139 if (Objects.isNull(behavior)) {
mohamedrahilra4545fb2019-02-07 18:43:01 +0530140 throw new WorkflowException("Not supported type(" + field.getType() + ")");
jaegonkime0f45b52018-10-09 20:23:26 +0900141 }
142 behavior.apply(worklet, context, field, model);
143 }
144
145 /**
146 * Injects text data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530147 *
jaegonkime0f45b52018-10-09 20:23:26 +0900148 * @param worklet work-let
149 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530150 * @param field the field of work-let
151 * @param model text data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900152 * @throws WorkflowException workflow exception
153 */
154 private static void injectText(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
155 throws WorkflowException {
156
157 String text = ((JsonDataModelTree) context.data()).textAt(model.path());
158 if (Objects.isNull(text)) {
159 if (model.optional()) {
160 return;
161 }
162 throw new WorkflowException("Invalid text data model on (" + model.path() + ")");
163 }
164
165 if (!(Objects.equals(field.getType(), String.class))) {
166 throw new WorkflowException("Target field (" + field + ") is not String");
167 }
168
169 try {
170 field.setAccessible(true);
171 field.set(worklet, text);
172 } catch (IllegalAccessException e) {
173 throw new WorkflowException(e);
174 }
175 }
176
177 /**
178 * Injects integer data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530179 *
jaegonkime0f45b52018-10-09 20:23:26 +0900180 * @param worklet work-let
181 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530182 * @param field the field of work-let
183 * @param model integer data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900184 * @throws WorkflowException workflow exception
185 */
186 private static void injectInteger(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
187 throws WorkflowException {
jaegonkime0f45b52018-10-09 20:23:26 +0900188 Integer number = ((JsonDataModelTree) context.data()).intAt(model.path());
189 if (Objects.isNull(number)) {
190 if (model.optional()) {
191 return;
192 }
193 throw new WorkflowException("Invalid number data model on (" + model.path() + ")");
194 }
195
196 if (!(Objects.equals(field.getType(), Integer.class))) {
197 throw new WorkflowException("Target field (" + field + ") is not Integer");
198 }
199
200 try {
201 field.setAccessible(true);
202 field.set(worklet, number);
203 } catch (IllegalAccessException e) {
204 throw new WorkflowException(e);
205 }
206 }
207
208 /**
209 * Injects boolean data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530210 *
jaegonkime0f45b52018-10-09 20:23:26 +0900211 * @param worklet work-let
212 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530213 * @param field the field of work-let
214 * @param model boolean data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900215 * @throws WorkflowException workflow exception
216 */
217 private static void injectBoolean(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
218 throws WorkflowException {
219
220 Boolean bool = ((JsonDataModelTree) context.data()).booleanAt(model.path());
221 if (Objects.isNull(bool)) {
222 if (model.optional()) {
223 return;
224 }
225 throw new WorkflowException("Invalid boolean data model on (" + model.path() + ")");
226 }
227
228 if (!(Objects.equals(field.getType(), Boolean.class))) {
229 throw new WorkflowException("Target field (" + field + ") is not Boolean");
230 }
231
232 try {
233 field.setAccessible(true);
234 field.set(worklet, bool);
235 } catch (IllegalAccessException e) {
236 throw new WorkflowException(e);
237 }
238 }
239
240 /**
241 * Injects json node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530242 *
jaegonkime0f45b52018-10-09 20:23:26 +0900243 * @param worklet work-let
244 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530245 * @param field the field of work-let
246 * @param model json node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900247 * @throws WorkflowException workflow exception
248 */
249 private static void injectJsonNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
250 throws WorkflowException {
251
252 JsonNode jsonNode = ((JsonDataModelTree) context.data()).nodeAt(model.path());
253 if (Objects.isNull(jsonNode)) {
254 if (model.optional()) {
255 return;
256 }
257 throw new WorkflowException("Invalid json node data model on (" + model.path() + ")");
258 }
259
260 if (!(Objects.equals(field.getType(), JsonNode.class))) {
261 throw new WorkflowException("Target field (" + field + ") is not JsonNode");
262 }
263
264 try {
265 field.setAccessible(true);
266 field.set(worklet, jsonNode);
267 } catch (IllegalAccessException e) {
268 throw new WorkflowException(e);
269 }
270 }
271
272 /**
273 * Injects json array node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530274 *
jaegonkime0f45b52018-10-09 20:23:26 +0900275 * @param worklet work-let
276 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530277 * @param field the field of work-let
278 * @param model json array node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900279 * @throws WorkflowException workflow exception
280 */
281 private static void injectArrayNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
282 throws WorkflowException {
283
284 ArrayNode arrayNode = ((JsonDataModelTree) context.data()).arrayAt(model.path());
285 if (Objects.isNull(arrayNode)) {
286 if (model.optional()) {
287 return;
288 }
289 throw new WorkflowException("Invalid array node data model on (" + model.path() + ")");
290 }
291
292 if (!(Objects.equals(field.getType(), ArrayNode.class))) {
293 throw new WorkflowException("Target field (" + field + ") is not ArrayNode");
294 }
295
296 try {
297 field.setAccessible(true);
298 field.set(worklet, arrayNode);
299 } catch (IllegalAccessException e) {
300 throw new WorkflowException(e);
301 }
302 }
303
304 /**
305 * Injects json object node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530306 *
jaegonkime0f45b52018-10-09 20:23:26 +0900307 * @param worklet work-let
308 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530309 * @param field the field of work-let
310 * @param model json object node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900311 * @throws WorkflowException workflow exception
312 */
313 private static void injectObjectNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
314 throws WorkflowException {
315
316 ObjectNode objNode = ((JsonDataModelTree) context.data()).objectAt(model.path());
317 if (Objects.isNull(objNode)) {
318 if (model.optional()) {
319 return;
320 }
321 throw new WorkflowException("Invalid object node data model on (" + model.path() + ")");
322 }
323
324 if (!(Objects.equals(field.getType(), ObjectNode.class))) {
325 throw new WorkflowException("Target field (" + field + ") is not ObjectNode");
326 }
327
328 try {
329 field.setAccessible(true);
330 field.set(worklet, objNode);
331 } catch (IllegalAccessException e) {
332 throw new WorkflowException(e);
333 }
334 }
335
336 private static Map<Class, DataModelFieldBehavior> inhaleTypeMap = new HashMap<>();
mohamedrahilra4545fb2019-02-07 18:43:01 +0530337
jaegonkime0f45b52018-10-09 20:23:26 +0900338 static {
339 inhaleTypeMap.put(String.class, JsonDataModelInjector::inhaleText);
340 inhaleTypeMap.put(Integer.class, JsonDataModelInjector::inhaleInteger);
341 inhaleTypeMap.put(Boolean.class, JsonDataModelInjector::inhaleBoolean);
342 inhaleTypeMap.put(JsonNode.class, JsonDataModelInjector::inhaleJsonNode);
343 inhaleTypeMap.put(ArrayNode.class, JsonDataModelInjector::inhaleArrayNode);
344 inhaleTypeMap.put(ObjectNode.class, JsonDataModelInjector::inhaleObjectNode);
345 }
346
347 /**
348 * Inhales data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530349 *
jaegonkime0f45b52018-10-09 20:23:26 +0900350 * @param worklet work-let
351 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530352 * @param field the field of work-let
353 * @param model data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900354 * @throws WorkflowException workflow exception
355 */
356 private void inhaleModel(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
357 throws WorkflowException {
358
mohamedrahilra4545fb2019-02-07 18:43:01 +0530359 DataModelFieldBehavior behavior = inhaleTypeMap.get(field.getType());
jaegonkime0f45b52018-10-09 20:23:26 +0900360 if (Objects.isNull(behavior)) {
mohamedrahilra4545fb2019-02-07 18:43:01 +0530361 throw new WorkflowException("Not supported type(" + field.getType() + ")");
jaegonkime0f45b52018-10-09 20:23:26 +0900362 }
363 behavior.apply(worklet, context, field, model);
364 }
365
366 /**
367 * Inhales text data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530368 *
jaegonkime0f45b52018-10-09 20:23:26 +0900369 * @param worklet work-let
370 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530371 * @param field the field of work-let
372 * @param model text data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900373 * @throws WorkflowException workflow exception
374 */
375 private static void inhaleText(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
376 throws WorkflowException {
377
378 if (!(Objects.equals(field.getType(), String.class))) {
379 throw new WorkflowException("Target field (" + field + ") is not String");
380 }
381
382 String text;
383 try {
384 field.setAccessible(true);
385 text = (String) field.get(worklet);
386 } catch (IllegalAccessException e) {
387 throw new WorkflowException(e);
388 }
389
390 if (Objects.isNull(text)) {
391 return;
392 }
393
394 JsonDataModelTree tree = (JsonDataModelTree) context.data();
395 JsonNode jsonNode = tree.nodeAt(model.path());
396
397 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
398 tree.setAt(model.path(), text);
399 } else if (!(jsonNode instanceof TextNode)) {
400 throw new WorkflowException("Invalid text data model on (" + model.path() + ")");
401 } else {
402 tree.remove(model.path());
403 tree.setAt(model.path(), text);
404 }
405 }
406
407 /**
408 * Inhales integer data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530409 *
jaegonkime0f45b52018-10-09 20:23:26 +0900410 * @param worklet work-let
411 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530412 * @param field the field of work-let
413 * @param model integer data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900414 * @throws WorkflowException workflow exception
415 */
416 private static void inhaleInteger(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
417 throws WorkflowException {
jaegonkime0f45b52018-10-09 20:23:26 +0900418 if (!(Objects.equals(field.getType(), Integer.class))) {
419 throw new WorkflowException("Target field (" + field + ") is not Integer");
420 }
421
422 Integer number;
423 try {
424 field.setAccessible(true);
425 number = (Integer) field.get(worklet);
426 } catch (IllegalAccessException e) {
427 throw new WorkflowException(e);
428 }
429
430 if (Objects.isNull(number)) {
431 return;
432 }
433
434 JsonDataModelTree tree = (JsonDataModelTree) context.data();
435 JsonNode jsonNode = tree.nodeAt(model.path());
436
437 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
438 tree.setAt(model.path(), number);
439 } else if (!(jsonNode instanceof IntNode)) {
440 throw new WorkflowException("Invalid integer data model on (" + model.path() + ")");
441 } else {
442 tree.remove(model.path());
443 tree.setAt(model.path(), number);
444 }
445 }
446
447 /**
448 * Inhales boolean data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530449 *
jaegonkime0f45b52018-10-09 20:23:26 +0900450 * @param worklet work-let
451 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530452 * @param field the field of work-let
453 * @param model boolean data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900454 * @throws WorkflowException workflow exception
455 */
456 private static void inhaleBoolean(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
457 throws WorkflowException {
458
459 if (!(Objects.equals(field.getType(), Boolean.class))) {
460 throw new WorkflowException("Target field (" + field + ") is not Boolean");
461 }
462
463 Boolean bool;
464 try {
465 field.setAccessible(true);
466 bool = (Boolean) field.get(worklet);
467 } catch (IllegalAccessException e) {
468 throw new WorkflowException(e);
469 }
470
471 if (Objects.isNull(bool)) {
472 return;
473 }
474
475 JsonDataModelTree tree = (JsonDataModelTree) context.data();
476 JsonNode jsonNode = tree.nodeAt(model.path());
477
478 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
479 tree.setAt(model.path(), bool);
480 } else if (!(jsonNode instanceof BooleanNode)) {
481 throw new WorkflowException("Invalid boolean data model on (" + model.path() + ")");
482 } else {
483 tree.remove(model.path());
484 tree.setAt(model.path(), bool);
485 }
486 }
487
488 /**
489 * Inhales json node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530490 *
jaegonkime0f45b52018-10-09 20:23:26 +0900491 * @param worklet work-let
492 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530493 * @param field the field of work-let
494 * @param model json node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900495 * @throws WorkflowException workflow exception
496 */
497 private static void inhaleJsonNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
498 throws WorkflowException {
499
500 if (!(Objects.equals(field.getType(), JsonNode.class))) {
501 throw new WorkflowException("Target field (" + field + ") is not JsonNode");
502 }
503
504 JsonNode tgtJsonNode;
505 try {
506 field.setAccessible(true);
507 tgtJsonNode = (JsonNode) field.get(worklet);
508 } catch (IllegalAccessException e) {
509 throw new WorkflowException(e);
510 }
511
512 if (Objects.isNull(tgtJsonNode)) {
513 return;
514 }
515
516 JsonDataModelTree tree = (JsonDataModelTree) context.data();
517 JsonNode jsonNode = tree.nodeAt(model.path());
518
519 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
520 tree.attach(model.path(), new JsonDataModelTree(tgtJsonNode));
521 } else if (!(jsonNode instanceof JsonNode)) {
522 throw new WorkflowException("Invalid json node data model on (" + model.path() + ")");
523 } else {
524 // do nothing
525 }
526 }
527
528 /**
529 * Inhales json array node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530530 *
jaegonkime0f45b52018-10-09 20:23:26 +0900531 * @param worklet work-let
532 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530533 * @param field the field of work-let
534 * @param model json array node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900535 * @throws WorkflowException workflow exception
536 */
537 private static void inhaleArrayNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
538 throws WorkflowException {
539 if (!(Objects.equals(field.getType(), ArrayNode.class))) {
540 throw new WorkflowException("Target field (" + field + ") is not ArrayNode");
541 }
542
543 ArrayNode tgtArrayNode;
544 try {
545 field.setAccessible(true);
546 tgtArrayNode = (ArrayNode) field.get(worklet);
547 } catch (IllegalAccessException e) {
548 throw new WorkflowException(e);
549 }
550
551 if (Objects.isNull(tgtArrayNode)) {
552 return;
553 }
554
555 JsonDataModelTree tree = (JsonDataModelTree) context.data();
556 JsonNode jsonNode = tree.nodeAt(model.path());
557
558 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
559 tree.attach(model.path(), new JsonDataModelTree(tgtArrayNode));
560 } else if (!(jsonNode instanceof ArrayNode)) {
561 throw new WorkflowException("Invalid array node data model on (" + model.path() + ")");
562 } else {
563 // do nothing
564 }
565 }
566
567 /**
568 * Inhales json object node data model on the filed of work-let.
mohamedrahilra4545fb2019-02-07 18:43:01 +0530569 *
jaegonkime0f45b52018-10-09 20:23:26 +0900570 * @param worklet work-let
571 * @param context workflow context
mohamedrahilra4545fb2019-02-07 18:43:01 +0530572 * @param field the field of work-let
573 * @param model json object node data model for the field
jaegonkime0f45b52018-10-09 20:23:26 +0900574 * @throws WorkflowException workflow exception
575 */
576 private static void inhaleObjectNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model)
577 throws WorkflowException {
578 if (!(Objects.equals(field.getType(), ObjectNode.class))) {
579 throw new WorkflowException("Target field (" + field + ") is not ObjectNode");
580 }
581
582 ObjectNode tgtObjNode;
583 try {
584 field.setAccessible(true);
585 tgtObjNode = (ObjectNode) field.get(worklet);
586 } catch (IllegalAccessException e) {
587 throw new WorkflowException(e);
588 }
589
590 if (Objects.isNull(tgtObjNode)) {
591 return;
592 }
593
594 JsonDataModelTree tree = (JsonDataModelTree) context.data();
595 JsonNode jsonNode = tree.nodeAt(model.path());
596
597 if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) {
598 tree.attach(model.path(), new JsonDataModelTree(tgtObjNode));
599 } else if (!(jsonNode instanceof ObjectNode)) {
600 throw new WorkflowException("Invalid object node data model on (" + model.path() + ")");
601 } else {
602 // do nothing
603 }
604 }
605}