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