Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.sfc.manager.impl; |
| 17 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 24 | import org.onlab.util.ItemNotFoundException; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 25 | import org.onlab.util.KryoNamespace; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 28 | import org.onosproject.net.NshServicePathId; |
| 29 | import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService; |
| 30 | import org.onosproject.sfc.forwarder.impl.ServiceFunctionForwarderImpl; |
| 31 | import org.onosproject.sfc.installer.FlowClassifierInstallerService; |
| 32 | import org.onosproject.sfc.installer.impl.FlowClassifierInstallerImpl; |
| 33 | import org.onosproject.sfc.manager.NshSpiIdGenerators; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 34 | import org.onosproject.sfc.manager.SfcService; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 35 | import org.onosproject.vtnrsc.FlowClassifier; |
| 36 | import org.onosproject.vtnrsc.FlowClassifierId; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 37 | import org.onosproject.vtnrsc.PortChain; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 38 | import org.onosproject.vtnrsc.PortChainId; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 39 | import org.onosproject.vtnrsc.PortPair; |
| 40 | import org.onosproject.vtnrsc.PortPairGroup; |
| 41 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 42 | import org.onosproject.vtnrsc.PortPairId; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 43 | import org.onosproject.vtnrsc.TenantId; |
| 44 | import org.onosproject.vtnrsc.event.VtnRscEvent; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 45 | import org.onosproject.vtnrsc.event.VtnRscEventFeedback; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 46 | import org.onosproject.vtnrsc.event.VtnRscListener; |
| 47 | import org.onosproject.vtnrsc.service.VtnRscService; |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 50 | import java.util.concurrent.ConcurrentHashMap; |
| 51 | import java.util.concurrent.ConcurrentMap; |
| 52 | |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 55 | /** |
| 56 | * Provides implementation of SFC Service. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | @Service |
| 60 | public class SfcManager implements SfcService { |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 63 | private static final String APP_ID = "org.onosproject.app.vtn"; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected VtnRscService vtnRscService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected CoreService coreService; |
| 70 | |
| 71 | protected ApplicationId appId; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 72 | private ServiceFunctionForwarderService serviceFunctionForwarderService; |
| 73 | private FlowClassifierInstallerService flowClassifierInstallerService; |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 74 | |
| 75 | private final VtnRscListener vtnRscListener = new InnerVtnRscListener(); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 76 | |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 77 | private ConcurrentMap<PortChainId, NshServicePathId> nshSpiPortChainMap = new ConcurrentHashMap<>(); |
| 78 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 79 | @Activate |
| 80 | public void activate() { |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 81 | appId = coreService.registerApplication(APP_ID); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 82 | serviceFunctionForwarderService = new ServiceFunctionForwarderImpl(appId); |
| 83 | flowClassifierInstallerService = new FlowClassifierInstallerImpl(appId); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 84 | |
| 85 | vtnRscService.addListener(vtnRscListener); |
| 86 | |
| 87 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 88 | .register(TenantId.class) |
| 89 | .register(PortPairId.class) |
| 90 | .register(PortPairGroupId.class) |
| 91 | .register(FlowClassifierId.class) |
| 92 | .register(PortChainId.class); |
| 93 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 99 | vtnRscService.removeListener(vtnRscListener); |
| 100 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 104 | /* |
| 105 | * Handle events. |
| 106 | */ |
| 107 | private class InnerVtnRscListener implements VtnRscListener { |
| 108 | @Override |
| 109 | public void event(VtnRscEvent event) { |
| 110 | |
| 111 | if (VtnRscEvent.Type.PORT_PAIR_PUT == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 112 | PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 113 | onPortPairCreated(portPair); |
| 114 | } else if (VtnRscEvent.Type.PORT_PAIR_DELETE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 115 | PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 116 | onPortPairDeleted(portPair); |
| 117 | } else if (VtnRscEvent.Type.PORT_PAIR_UPDATE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 118 | PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 119 | onPortPairDeleted(portPair); |
| 120 | onPortPairCreated(portPair); |
| 121 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 122 | PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 123 | onPortPairGroupCreated(portPairGroup); |
| 124 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 125 | PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 126 | onPortPairGroupDeleted(portPairGroup); |
| 127 | } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 128 | PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 129 | onPortPairGroupDeleted(portPairGroup); |
| 130 | onPortPairGroupCreated(portPairGroup); |
| 131 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 132 | FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 133 | onFlowClassifierCreated(flowClassifier); |
| 134 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 135 | FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 136 | onFlowClassifierDeleted(flowClassifier); |
| 137 | } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 138 | FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 139 | onFlowClassifierDeleted(flowClassifier); |
| 140 | onFlowClassifierCreated(flowClassifier); |
| 141 | } else if (VtnRscEvent.Type.PORT_CHAIN_PUT == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 142 | PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 143 | onPortChainCreated(portChain); |
| 144 | } else if (VtnRscEvent.Type.PORT_CHAIN_DELETE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 145 | PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 146 | onPortChainDeleted(portChain); |
| 147 | } else if (VtnRscEvent.Type.PORT_CHAIN_UPDATE == event.type()) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 148 | PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain(); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 149 | onPortChainDeleted(portChain); |
| 150 | onPortChainCreated(portChain); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 155 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 156 | public void onPortPairCreated(PortPair portPair) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 157 | log.debug("onPortPairCreated"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 158 | // TODO: Modify forwarding rule on port-pair creation. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 162 | public void onPortPairDeleted(PortPair portPair) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 163 | log.debug("onPortPairDeleted"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 164 | // TODO: Modify forwarding rule on port-pair deletion. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 168 | public void onPortPairGroupCreated(PortPairGroup portPairGroup) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 169 | log.debug("onPortPairGroupCreated"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 170 | // TODO: Modify forwarding rule on port-pair-group creation. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 174 | public void onPortPairGroupDeleted(PortPairGroup portPairGroup) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 175 | log.debug("onPortPairGroupDeleted"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 176 | // TODO: Modify forwarding rule on port-pair-group deletion. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 180 | public void onFlowClassifierCreated(FlowClassifier flowClassifier) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 181 | log.debug("onFlowClassifierCreated"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 182 | // TODO: Modify forwarding rule on flow-classifier creation. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 186 | public void onFlowClassifierDeleted(FlowClassifier flowClassifier) { |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 187 | log.debug("onFlowClassifierDeleted"); |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 188 | // TODO: Modify forwarding rule on flow-classifier deletion. |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 192 | public void onPortChainCreated(PortChain portChain) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 193 | NshServicePathId nshSpi; |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 194 | log.info("onPortChainCreated"); |
| 195 | if (nshSpiPortChainMap.containsKey(portChain.portChainId())) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 196 | nshSpi = nshSpiPortChainMap.get(portChain.portChainId()); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 197 | } else { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 198 | nshSpi = NshServicePathId.of(NshSpiIdGenerators.create()); |
| 199 | nshSpiPortChainMap.put(portChain.portChainId(), nshSpi); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // install in OVS. |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 203 | flowClassifierInstallerService.installFlowClassifier(portChain, nshSpi); |
| 204 | serviceFunctionForwarderService.installForwardingRule(portChain, nshSpi); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 208 | public void onPortChainDeleted(PortChain portChain) { |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 209 | log.info("onPortChainDeleted"); |
| 210 | if (!nshSpiPortChainMap.containsKey(portChain.portChainId())) { |
| 211 | throw new ItemNotFoundException("Unable to find NSH SPI"); |
| 212 | } |
| 213 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 214 | NshServicePathId nshSpi = nshSpiPortChainMap.get(portChain.portChainId()); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 215 | // uninstall from OVS. |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 216 | flowClassifierInstallerService.unInstallFlowClassifier(portChain, nshSpi); |
| 217 | serviceFunctionForwarderService.unInstallForwardingRule(portChain, nshSpi); |
Mahesh Poojary Huawei | 15092f6 | 2015-12-10 11:09:02 +0530 | [diff] [blame] | 218 | |
| 219 | // remove SPI. No longer it will be used. |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame^] | 220 | nshSpiPortChainMap.remove(nshSpi); |
Mahesh Poojary S | 335e7c3 | 2015-10-29 10:16:51 +0530 | [diff] [blame] | 221 | } |
Mahesh Poojary S | c9c10f9 | 2015-11-30 17:18:05 +0530 | [diff] [blame] | 222 | } |