blob: c841266c06b3b916af764bf9e8f1b255c11f49d8 [file] [log] [blame]
Mahesh Poojary S335e7c32015-10-29 10:16:51 +05301/*
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 */
16package org.onosproject.sfc.manager.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053025import org.apache.felix.scr.annotations.Service;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053026
27import java.util.concurrent.ConcurrentMap;
28import java.util.concurrent.ConcurrentHashMap;
29
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053030import org.onlab.util.KryoNamespace;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053031import org.onlab.util.ItemNotFoundException;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053034import org.onosproject.net.NshServicePathId;
35import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService;
36import org.onosproject.sfc.forwarder.impl.ServiceFunctionForwarderImpl;
37import org.onosproject.sfc.installer.FlowClassifierInstallerService;
38import org.onosproject.sfc.installer.impl.FlowClassifierInstallerImpl;
39import org.onosproject.sfc.manager.NshSpiIdGenerators;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053040import org.onosproject.sfc.manager.SfcService;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053041import org.onosproject.vtnrsc.PortPair;
42import org.onosproject.vtnrsc.PortPairId;
43import org.onosproject.vtnrsc.PortPairGroup;
44import org.onosproject.vtnrsc.PortPairGroupId;
45import org.onosproject.vtnrsc.FlowClassifier;
46import org.onosproject.vtnrsc.FlowClassifierId;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053047import org.onosproject.vtnrsc.PortChain;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053048import org.onosproject.vtnrsc.PortChainId;
49import org.onosproject.vtnrsc.TenantId;
50import org.onosproject.vtnrsc.event.VtnRscEvent;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053051import org.onosproject.vtnrsc.event.VtnRscEventFeedback;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053052import org.onosproject.vtnrsc.event.VtnRscListener;
53import org.onosproject.vtnrsc.service.VtnRscService;
54
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053055import org.slf4j.Logger;
56
57/**
58 * Provides implementation of SFC Service.
59 */
60@Component(immediate = true)
61@Service
62public class SfcManager implements SfcService {
63
64 private final Logger log = getLogger(getClass());
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053065 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 Huawei15092f62015-12-10 11:09:02 +053074 private ServiceFunctionForwarderService serviceFunctionForwarderService;
75 private FlowClassifierInstallerService flowClassifierInstallerService;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053076
77 private final VtnRscListener vtnRscListener = new InnerVtnRscListener();
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053078
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053079 private ConcurrentMap<PortChainId, NshServicePathId> nshSpiPortChainMap = new ConcurrentHashMap<>();
80
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053081 @Activate
82 public void activate() {
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053083 appId = coreService.registerApplication(APP_ID);
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053084 serviceFunctionForwarderService = new ServiceFunctionForwarderImpl(appId);
85 flowClassifierInstallerService = new FlowClassifierInstallerImpl(appId);
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053086
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 S335e7c32015-10-29 10:16:51 +053096 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530101 vtnRscService.removeListener(vtnRscListener);
102
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530103 log.info("Stopped");
104 }
105
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530106 /*
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 Huawei15092f62015-12-10 11:09:02 +0530114 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530115 onPortPairCreated(portPair);
116 } else if (VtnRscEvent.Type.PORT_PAIR_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530117 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530118 onPortPairDeleted(portPair);
119 } else if (VtnRscEvent.Type.PORT_PAIR_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530120 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530121 onPortPairDeleted(portPair);
122 onPortPairCreated(portPair);
123 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530124 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530125 onPortPairGroupCreated(portPairGroup);
126 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530127 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530128 onPortPairGroupDeleted(portPairGroup);
129 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530130 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530131 onPortPairGroupDeleted(portPairGroup);
132 onPortPairGroupCreated(portPairGroup);
133 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530134 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530135 onFlowClassifierCreated(flowClassifier);
136 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530137 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530138 onFlowClassifierDeleted(flowClassifier);
139 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530140 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530141 onFlowClassifierDeleted(flowClassifier);
142 onFlowClassifierCreated(flowClassifier);
143 } else if (VtnRscEvent.Type.PORT_CHAIN_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530144 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530145 onPortChainCreated(portChain);
146 } else if (VtnRscEvent.Type.PORT_CHAIN_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530147 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530148 onPortChainDeleted(portChain);
149 } else if (VtnRscEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530150 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530151 onPortChainDeleted(portChain);
152 onPortChainCreated(portChain);
153 }
154 }
155 }
156
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530157 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530158 public void onPortPairCreated(PortPair portPair) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530159 log.debug("onPortPairCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530160 // TODO: Modify forwarding rule on port-pair creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530161 }
162
163 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530164 public void onPortPairDeleted(PortPair portPair) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530165 log.debug("onPortPairDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530166 // TODO: Modify forwarding rule on port-pair deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530167 }
168
169 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530170 public void onPortPairGroupCreated(PortPairGroup portPairGroup) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530171 log.debug("onPortPairGroupCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530172 // TODO: Modify forwarding rule on port-pair-group creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530173 }
174
175 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530176 public void onPortPairGroupDeleted(PortPairGroup portPairGroup) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530177 log.debug("onPortPairGroupDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530178 // TODO: Modify forwarding rule on port-pair-group deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530179 }
180
181 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530182 public void onFlowClassifierCreated(FlowClassifier flowClassifier) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530183 log.debug("onFlowClassifierCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530184 // TODO: Modify forwarding rule on flow-classifier creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530185 }
186
187 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530188 public void onFlowClassifierDeleted(FlowClassifier flowClassifier) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530189 log.debug("onFlowClassifierDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530190 // TODO: Modify forwarding rule on flow-classifier deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530191 }
192
193 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530194 public void onPortChainCreated(PortChain portChain) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530195 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 S335e7c32015-10-29 10:16:51 +0530207 }
208
209 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530210 public void onPortChainDeleted(PortChain portChain) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530211 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 S335e7c32015-10-29 10:16:51 +0530223 }
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530224}