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