jaegonkim | dcf7c82 | 2019-02-06 15:00:14 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-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.ofoverlay.impl; |
| 17 | |
| 18 | import org.onosproject.net.device.DeviceService; |
| 19 | import org.onosproject.workflow.api.ImmutableListWorkflow; |
| 20 | import org.onosproject.workflow.api.Workflow; |
| 21 | import org.onosproject.workflow.api.WorkflowExecutionService; |
| 22 | import org.onosproject.workflow.api.WorkflowStore; |
| 23 | import org.onosproject.workflow.api.WorkplaceStore; |
| 24 | import org.osgi.service.component.annotations.Activate; |
| 25 | import org.osgi.service.component.annotations.Component; |
| 26 | import org.osgi.service.component.annotations.Deactivate; |
| 27 | import org.osgi.service.component.annotations.Reference; |
| 28 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.net.URI; |
| 33 | import java.util.concurrent.ScheduledExecutorService; |
| 34 | |
| 35 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 36 | import static org.onlab.util.Tools.groupedThreads; |
| 37 | |
| 38 | /** |
| 39 | * Class for Open-flow overlay configuration workflow. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public class OfOverlayWorkflow { |
| 43 | |
| 44 | private static final Logger log = LoggerFactory.getLogger(OfOverlayWorkflow.class); |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 47 | protected WorkflowStore workflowStore; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 50 | protected WorkplaceStore workplaceStore; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 53 | protected WorkflowExecutionService workflowExecutionService; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 56 | protected DeviceService deviceService; |
| 57 | |
| 58 | private ScheduledExecutorService eventMapTriggerExecutor; |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | log.info("Activated"); |
| 63 | |
| 64 | eventMapTriggerExecutor = newSingleThreadScheduledExecutor( |
| 65 | groupedThreads("onos/of-overlay", "eventmap-trigger-executor")); |
| 66 | |
| 67 | registerWorkflows(); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | log.info("Deactivated"); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Registers 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("of-overlay.workflow-nova"); |
| 85 | Workflow workflow = ImmutableListWorkflow.builder() |
| 86 | .id(uri) |
| 87 | //.attribute(WorkflowAttribute.REMOVE_AFTER_COMPLETE) |
| 88 | .chain(Ovs.CreateOvsdbDevice.class.getName()) |
| 89 | .chain(Ovs.UpdateOvsVersion.class.getName()) |
| 90 | .chain(Ovs.UpdateOverlayBridgeId.class.getName()) |
| 91 | .chain(Ovs.CreateOverlayBridge.class.getName()) |
| 92 | .chain(Ovs.UpdateUnderlayBridgeId.class.getName()) |
| 93 | .chain(Ovs.CreateUnderlayBridge.class.getName()) |
| 94 | .chain(Ovs.CreateOverlayBridgeVxlanPort.class.getName()) |
| 95 | .chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()) |
| 96 | .chain(Ovs.ConfigureUnderlayBridgeLocalIp.class.getName()) |
| 97 | .build(); |
| 98 | workflowStore.register(workflow); |
| 99 | |
| 100 | uri = URI.create("of-overlay.clean-workflow-nova"); |
| 101 | workflow = ImmutableListWorkflow.builder() |
| 102 | .id(uri) |
| 103 | //.attribute(WorkflowAttribute.REMOVE_AFTER_COMPLETE) |
| 104 | .chain(Ovs.DeleteOverlayBridgeConfig.class.getName()) |
| 105 | .chain(Ovs.RemoveOverlayBridgeOfDevice.class.getName()) |
| 106 | .chain(Ovs.DeleteUnderlayBridgeConfig.class.getName()) |
| 107 | .chain(Ovs.RemoveUnderlayBridgeOfDevice.class.getName()) |
| 108 | .chain(Ovs.RemoveOvsdbDevice.class.getName()) |
| 109 | .build(); |
| 110 | workflowStore.register(workflow); |
| 111 | |
| 112 | uri = URI.create("of-overlay.workflow-ovs-leaf"); |
| 113 | workflow = ImmutableListWorkflow.builder() |
| 114 | .id(uri) |
| 115 | .chain(Ovs.CreateOvsdbDevice.class.getName()) |
| 116 | .chain(Ovs.UpdateOvsVersion.class.getName()) |
| 117 | .chain(Ovs.UpdateUnderlayBridgeId.class.getName()) |
| 118 | .chain(Ovs.CreateUnderlayBridge.class.getName()) |
| 119 | .chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()) |
| 120 | .build(); |
| 121 | workflowStore.register(workflow); |
| 122 | |
| 123 | uri = URI.create("of-overlay.workflow-ovs-spine"); |
| 124 | workflow = ImmutableListWorkflow.builder() |
| 125 | .id(uri) |
| 126 | .chain(Ovs.CreateOvsdbDevice.class.getName()) |
| 127 | .chain(Ovs.UpdateOvsVersion.class.getName()) |
| 128 | .chain(Ovs.UpdateUnderlayBridgeId.class.getName()) |
| 129 | .chain(Ovs.CreateUnderlayBridge.class.getName()) |
| 130 | .chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()) |
| 131 | .build(); |
| 132 | workflowStore.register(workflow); |
| 133 | |
| 134 | deviceService.addListener( |
| 135 | event -> { |
| 136 | // trigger EventTask for DeviceEvent |
| 137 | eventMapTriggerExecutor.submit( |
| 138 | () -> workflowExecutionService.eventMapTrigger( |
| 139 | event, |
| 140 | // event hint supplier |
| 141 | (ev) -> { |
| 142 | if (ev == null || ev.subject() == null) { |
| 143 | return null; |
| 144 | } |
| 145 | String hint = event.subject().id().toString(); |
| 146 | log.debug("hint: {}", hint); |
| 147 | return hint; |
| 148 | } |
| 149 | ) |
| 150 | ); |
| 151 | } |
| 152 | ); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } |