jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.workflow.impl.example; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.JsonNodeType; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.workflow.api.AbstractWorklet; |
| 23 | import org.onosproject.workflow.api.DataModelTree; |
| 24 | import org.onosproject.workflow.api.ImmutableListWorkflow; |
| 25 | import org.onosproject.workflow.api.JsonDataModelTree; |
| 26 | import org.onosproject.workflow.api.Workflow; |
| 27 | import org.onosproject.workflow.api.WorkflowContext; |
| 28 | import org.onosproject.workflow.api.WorkflowException; |
| 29 | import org.onosproject.workflow.api.WorkflowExecutionService; |
| 30 | import org.onosproject.workflow.api.WorkflowStore; |
| 31 | import org.onosproject.workflow.api.WorkplaceStore; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.net.URI; |
| 41 | |
| 42 | /** |
| 43 | * Class for sample workflow. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | public class SampleWorkflow { |
| 47 | |
| 48 | private static final Logger log = LoggerFactory.getLogger(SampleWorkflow.class); |
| 49 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 51 | protected WorkflowStore workflowStore; |
| 52 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 54 | protected WorkplaceStore workplaceStore; |
| 55 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 57 | protected WorkflowExecutionService workflowExecutionService; |
| 58 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 60 | protected DeviceService deviceService; |
| 61 | |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | log.info("Activated"); |
| 66 | |
| 67 | registerWorkflows(); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | log.info("Deactivated"); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Registers example workflows. |
| 78 | */ |
| 79 | private void registerWorkflows() { |
| 80 | // registering class-loader |
| 81 | workflowStore.registerLocal(this.getClass().getClassLoader()); |
| 82 | |
| 83 | // registering new workflow definition |
| 84 | URI uri = URI.create("sample.workflow-0"); |
| 85 | Workflow workflow = ImmutableListWorkflow.builder() |
| 86 | .id(uri) |
| 87 | .chain(SampleWorklet1.class.getName()) |
| 88 | .chain(SampleWorklet2.class.getName()) |
| 89 | .chain(SampleWorklet3.class.getName()) |
| 90 | .chain(SampleWorklet4.class.getName()) |
| 91 | .chain(SampleWorklet5.class.getName()) |
| 92 | .build(); |
| 93 | workflowStore.register(workflow); |
| 94 | |
| 95 | // registering new workflow definition |
| 96 | uri = URI.create("sample.workflow-1"); |
| 97 | workflow = ImmutableListWorkflow.builder() |
| 98 | .id(uri) |
| 99 | .chain(SampleWorklet3.class.getName()) |
| 100 | .chain(SampleWorklet2.class.getName()) |
| 101 | .chain(SampleWorklet1.class.getName()) |
| 102 | .chain(SampleWorklet4.class.getName()) |
| 103 | .chain(SampleWorklet5.class.getName()) |
| 104 | .build(); |
| 105 | workflowStore.register(workflow); |
| 106 | |
| 107 | // registering new workflow definition |
| 108 | uri = URI.create("sample.workflow-2"); |
| 109 | workflow = ImmutableListWorkflow.builder() |
| 110 | .id(uri) |
| 111 | .chain(SampleWorklet1.class.getName()) |
| 112 | .chain(SampleWorklet3.class.getName()) |
| 113 | .chain(SampleWorklet2.class.getName()) |
| 114 | .chain(SampleWorklet4.class.getName()) |
| 115 | .chain(SampleWorklet5.class.getName()) |
| 116 | .build(); |
| 117 | workflowStore.register(workflow); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Abstract class for sample worklet. |
| 122 | */ |
| 123 | public abstract static class AbsSampleWorklet extends AbstractWorklet { |
| 124 | |
| 125 | protected static final String SAMPLE_DATAMODEL_PTR = "/sample/job"; |
| 126 | |
| 127 | /** |
| 128 | * Constructor for sample worklet. |
| 129 | */ |
| 130 | protected AbsSampleWorklet() { |
| 131 | |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Allocates or gets data model. |
| 136 | * @param context workflow context |
| 137 | * @return json object node |
| 138 | * @throws WorkflowException workflow exception |
| 139 | */ |
| 140 | protected ObjectNode allocOrGetModel(WorkflowContext context) throws WorkflowException { |
| 141 | |
| 142 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 143 | JsonNode params = tree.root(); |
| 144 | |
| 145 | if (params.at(SAMPLE_DATAMODEL_PTR).getNodeType() == JsonNodeType.MISSING) { |
| 146 | tree.alloc(SAMPLE_DATAMODEL_PTR, DataModelTree.Nodetype.MAP); |
| 147 | } |
| 148 | return (ObjectNode) params.at(SAMPLE_DATAMODEL_PTR); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Gets data model. |
| 153 | * @param context workflow context |
| 154 | * @return json object node |
| 155 | * @throws WorkflowException workflow exception |
| 156 | */ |
| 157 | protected ObjectNode getDataModel(WorkflowContext context) throws WorkflowException { |
| 158 | DataModelTree tree = context.data(); |
| 159 | return ((JsonDataModelTree) tree.subtree(SAMPLE_DATAMODEL_PTR)).rootObject(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sleeps for 'ms' milli seconds. |
| 164 | * @param ms milli seconds to sleep |
| 165 | */ |
| 166 | protected void sleep(long ms) { |
| 167 | try { |
| 168 | Thread.sleep(ms); |
| 169 | } catch (InterruptedException e) { |
Ray Milkey | fe6afd8 | 2018-11-26 14:03:20 -0800 | [diff] [blame] | 170 | Thread.currentThread().interrupt(); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Class for sample worklet-1. |
| 177 | */ |
| 178 | public static class SampleWorklet1 extends AbsSampleWorklet { |
| 179 | @Override |
| 180 | public void process(WorkflowContext context) throws WorkflowException { |
| 181 | ObjectNode node = getDataModel(context); |
| 182 | node.put("work1", "done"); |
| 183 | log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 184 | sleep(30); |
| 185 | |
| 186 | context.completed(); //Complete the job of worklet in the process |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 191 | ObjectNode node = allocOrGetModel(context); |
| 192 | log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 193 | sleep(30); |
| 194 | return !node.has("work1"); |
| 195 | |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Class for sample worklet-2 (using timeout). |
| 201 | */ |
| 202 | public static class SampleWorklet2 extends AbsSampleWorklet { |
| 203 | @Override |
| 204 | public void process(WorkflowContext context) throws WorkflowException { |
| 205 | ObjectNode node = getDataModel(context); |
| 206 | node.put("work2", "done"); |
| 207 | log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 208 | sleep(50); |
| 209 | |
| 210 | context.waitFor(50L); //Timeout will happen after 50 milli seconds. |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void timeout(WorkflowContext context) throws WorkflowException { |
| 215 | context.completed(); //Complete the job of worklet by timeout |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 220 | ObjectNode node = allocOrGetModel(context); |
| 221 | log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 222 | sleep(50); |
| 223 | return !node.has("work2"); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | public static class SampleWorklet3 extends AbsSampleWorklet { |
| 228 | @Override |
| 229 | public void process(WorkflowContext context) throws WorkflowException { |
| 230 | ObjectNode node = getDataModel(context); |
| 231 | node.put("work3", "done"); |
| 232 | log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 233 | sleep(10); |
| 234 | |
| 235 | context.completed(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 240 | ObjectNode node = allocOrGetModel(context); |
| 241 | log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 242 | sleep(10); |
| 243 | return !node.has("work3"); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public static class SampleWorklet4 extends AbsSampleWorklet { |
| 248 | @Override |
| 249 | public void process(WorkflowContext context) throws WorkflowException { |
| 250 | ObjectNode node = getDataModel(context); |
| 251 | node.put("work4", "done"); |
| 252 | log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 253 | sleep(10); |
| 254 | |
| 255 | context.completed(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 260 | ObjectNode node = allocOrGetModel(context); |
| 261 | log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 262 | sleep(10); |
| 263 | return !node.has("work4"); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | public static class SampleWorklet5 extends AbsSampleWorklet { |
| 268 | @Override |
| 269 | public void process(WorkflowContext context) throws WorkflowException { |
| 270 | ObjectNode node = getDataModel(context); |
| 271 | node.put("work5", "done"); |
| 272 | log.info("workflow-process {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 273 | sleep(10); |
| 274 | |
| 275 | context.completed(); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 280 | ObjectNode node = allocOrGetModel(context); |
| 281 | log.info("workflow-isNext {}-{}", context.workplaceName(), this.getClass().getSimpleName()); |
| 282 | sleep(10); |
| 283 | return !node.has("work5"); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | } |