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; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 20 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 21 | import org.onosproject.net.config.NetworkConfigService; |
| 22 | import org.onosproject.workflow.api.DefaultWorkplace; |
| 23 | import org.onosproject.workflow.api.JsonDataModelTree; |
| 24 | import org.onosproject.workflow.api.Workflow; |
| 25 | import org.onosproject.workflow.api.WorkflowContext; |
| 26 | import org.onosproject.workflow.api.WorkflowDescription; |
| 27 | import org.onosproject.workflow.api.WorkflowException; |
| 28 | import org.onosproject.workflow.api.WorkflowService; |
| 29 | import org.onosproject.workflow.api.WorkflowExecutionService; |
| 30 | import org.onosproject.workflow.api.WorkflowStore; |
| 31 | import org.onosproject.workflow.api.Workplace; |
| 32 | import org.onosproject.workflow.api.WorkplaceDescription; |
| 33 | import org.onosproject.workflow.api.WorkplaceStore; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.net.URI; |
| 42 | import java.util.Objects; |
| 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 46 | @Component(immediate = true, service = WorkflowService.class) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 47 | public class WorkflowManager implements WorkflowService { |
| 48 | |
| 49 | protected static final Logger log = getLogger(WorkflowManager.class); |
| 50 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 52 | private WorkflowExecutionService workflowExecutionService; |
| 53 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 55 | protected WorkplaceStore workplaceStore; |
| 56 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 58 | protected WorkflowStore workflowStore; |
| 59 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 61 | private NetworkConfigService networkConfigService; |
| 62 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 64 | private NetworkConfigRegistry networkConfigRegistry; |
| 65 | |
| 66 | private WorkflowNetConfigListener netcfgListener; |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | netcfgListener = new WorkflowNetConfigListener(this); |
| 71 | networkConfigRegistry.registerConfigFactory(netcfgListener.getConfigFactory()); |
| 72 | networkConfigService.addListener(netcfgListener); |
| 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
| 78 | networkConfigService.removeListener(netcfgListener); |
| 79 | networkConfigRegistry.unregisterConfigFactory(netcfgListener.getConfigFactory()); |
| 80 | log.info("Stopped"); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void createWorkplace(WorkplaceDescription wpDesc) throws WorkflowException { |
| 85 | log.info("createWorkplace: {}", wpDesc); |
| 86 | |
| 87 | JsonNode root; |
| 88 | if (wpDesc.data().isPresent()) { |
| 89 | root = wpDesc.data().get(); |
| 90 | } else { |
| 91 | root = JsonNodeFactory.instance.objectNode(); |
| 92 | } |
| 93 | DefaultWorkplace workplace = |
| 94 | new DefaultWorkplace(wpDesc.name(), new JsonDataModelTree(root)); |
| 95 | workplaceStore.registerWorkplace(wpDesc.name(), workplace); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void removeWorkplace(WorkplaceDescription wpDesc) throws WorkflowException { |
| 100 | log.info("removeWorkplace: {}", wpDesc); |
| 101 | //TODO: Removing workflows belong to this workplace |
| 102 | workplaceStore.removeWorkplace(wpDesc.name()); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void clearWorkplace() throws WorkflowException { |
| 107 | log.info("clearWorkplace"); |
| 108 | workplaceStore.getWorkplaces().stream() |
| 109 | .filter(wp -> !Objects.equals(wp.name(), Workplace.SYSTEM_WORKPLACE)) |
| 110 | .forEach(wp -> workplaceStore.removeWorkplace(wp.name())); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void invokeWorkflow(WorkflowDescription wfDesc) throws WorkflowException { |
| 115 | invokeWorkflow(wfDesc.toJson()); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void invokeWorkflow(JsonNode worklowDescJson) throws WorkflowException { |
| 120 | log.info("invokeWorkflow: {}", worklowDescJson); |
| 121 | |
| 122 | Workplace workplace = workplaceStore.getWorkplace(Workplace.SYSTEM_WORKPLACE); |
| 123 | if (Objects.isNull(workplace)) { |
| 124 | throw new WorkflowException("Invalid system workplace"); |
| 125 | } |
| 126 | |
| 127 | Workflow workflow = workflowStore.get(URI.create(WorkplaceWorkflow.WF_CREATE_WORKFLOW)); |
| 128 | if (Objects.isNull(workflow)) { |
| 129 | throw new WorkflowException("Invalid workflow " + WorkplaceWorkflow.WF_CREATE_WORKFLOW); |
| 130 | } |
| 131 | |
| 132 | WorkflowContext context = workflow.buildSystemContext(workplace, new JsonDataModelTree(worklowDescJson)); |
| 133 | workflowExecutionService.execInitWorklet(context); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void terminateWorkflow(WorkflowDescription wfDesc) throws WorkflowException { |
| 138 | log.info("terminateWorkflow: {}", wfDesc); |
| 139 | if (Objects.nonNull(workplaceStore.getContext(wfDesc.workflowContextName()))) { |
| 140 | workplaceStore.removeContext(wfDesc.workflowContextName()); |
| 141 | } |
| 142 | } |
| 143 | } |