blob: 02eda21253f4936640cececc0669ff3789e36b7e [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
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053023import org.apache.felix.scr.annotations.Service;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053024import org.onlab.util.ItemNotFoundException;
Jonathan Hart51539b82015-10-29 09:53:04 -070025import org.onlab.util.KryoNamespace;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053028import org.onosproject.net.NshServicePathId;
29import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService;
30import org.onosproject.sfc.forwarder.impl.ServiceFunctionForwarderImpl;
31import org.onosproject.sfc.installer.FlowClassifierInstallerService;
32import org.onosproject.sfc.installer.impl.FlowClassifierInstallerImpl;
33import org.onosproject.sfc.manager.NshSpiIdGenerators;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053034import org.onosproject.sfc.manager.SfcService;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053035import org.onosproject.vtnrsc.FlowClassifier;
36import org.onosproject.vtnrsc.FlowClassifierId;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053037import org.onosproject.vtnrsc.PortChain;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053038import org.onosproject.vtnrsc.PortChainId;
Jonathan Hart51539b82015-10-29 09:53:04 -070039import org.onosproject.vtnrsc.PortPair;
40import org.onosproject.vtnrsc.PortPairGroup;
41import org.onosproject.vtnrsc.PortPairGroupId;
42import org.onosproject.vtnrsc.PortPairId;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053043import org.onosproject.vtnrsc.TenantId;
44import org.onosproject.vtnrsc.event.VtnRscEvent;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053045import org.onosproject.vtnrsc.event.VtnRscEventFeedback;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053046import org.onosproject.vtnrsc.event.VtnRscListener;
47import org.onosproject.vtnrsc.service.VtnRscService;
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053048import org.slf4j.Logger;
49
Jonathan Hart51539b82015-10-29 09:53:04 -070050import java.util.concurrent.ConcurrentHashMap;
51import java.util.concurrent.ConcurrentMap;
52
53import static org.slf4j.LoggerFactory.getLogger;
54
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053055/**
56 * Provides implementation of SFC Service.
57 */
58@Component(immediate = true)
59@Service
60public class SfcManager implements SfcService {
61
62 private final Logger log = getLogger(getClass());
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053063 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 Huawei15092f62015-12-10 11:09:02 +053072 private ServiceFunctionForwarderService serviceFunctionForwarderService;
73 private FlowClassifierInstallerService flowClassifierInstallerService;
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053074
75 private final VtnRscListener vtnRscListener = new InnerVtnRscListener();
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053076
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053077 private ConcurrentMap<PortChainId, NshServicePathId> nshSpiPortChainMap = new ConcurrentHashMap<>();
78
Mahesh Poojary S335e7c32015-10-29 10:16:51 +053079 @Activate
80 public void activate() {
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053081 appId = coreService.registerApplication(APP_ID);
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +053082 serviceFunctionForwarderService = new ServiceFunctionForwarderImpl(appId);
83 flowClassifierInstallerService = new FlowClassifierInstallerImpl(appId);
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053084
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 S335e7c32015-10-29 10:16:51 +053094 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +053099 vtnRscService.removeListener(vtnRscListener);
100
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530101 log.info("Stopped");
102 }
103
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530104 /*
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 Huawei15092f62015-12-10 11:09:02 +0530112 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530113 onPortPairCreated(portPair);
114 } else if (VtnRscEvent.Type.PORT_PAIR_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530115 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530116 onPortPairDeleted(portPair);
117 } else if (VtnRscEvent.Type.PORT_PAIR_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530118 PortPair portPair = ((VtnRscEventFeedback) event.subject()).portPair();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530119 onPortPairDeleted(portPair);
120 onPortPairCreated(portPair);
121 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530122 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530123 onPortPairGroupCreated(portPairGroup);
124 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530125 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530126 onPortPairGroupDeleted(portPairGroup);
127 } else if (VtnRscEvent.Type.PORT_PAIR_GROUP_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530128 PortPairGroup portPairGroup = ((VtnRscEventFeedback) event.subject()).portPairGroup();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530129 onPortPairGroupDeleted(portPairGroup);
130 onPortPairGroupCreated(portPairGroup);
131 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530132 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530133 onFlowClassifierCreated(flowClassifier);
134 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530135 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530136 onFlowClassifierDeleted(flowClassifier);
137 } else if (VtnRscEvent.Type.FLOW_CLASSIFIER_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530138 FlowClassifier flowClassifier = ((VtnRscEventFeedback) event.subject()).flowClassifier();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530139 onFlowClassifierDeleted(flowClassifier);
140 onFlowClassifierCreated(flowClassifier);
141 } else if (VtnRscEvent.Type.PORT_CHAIN_PUT == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530142 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530143 onPortChainCreated(portChain);
144 } else if (VtnRscEvent.Type.PORT_CHAIN_DELETE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530145 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530146 onPortChainDeleted(portChain);
147 } else if (VtnRscEvent.Type.PORT_CHAIN_UPDATE == event.type()) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530148 PortChain portChain = (PortChain) ((VtnRscEventFeedback) event.subject()).portChain();
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530149 onPortChainDeleted(portChain);
150 onPortChainCreated(portChain);
151 }
152 }
153 }
154
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530155 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530156 public void onPortPairCreated(PortPair portPair) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530157 log.debug("onPortPairCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530158 // TODO: Modify forwarding rule on port-pair creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530159 }
160
161 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530162 public void onPortPairDeleted(PortPair portPair) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530163 log.debug("onPortPairDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530164 // TODO: Modify forwarding rule on port-pair deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530165 }
166
167 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530168 public void onPortPairGroupCreated(PortPairGroup portPairGroup) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530169 log.debug("onPortPairGroupCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530170 // TODO: Modify forwarding rule on port-pair-group creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530171 }
172
173 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530174 public void onPortPairGroupDeleted(PortPairGroup portPairGroup) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530175 log.debug("onPortPairGroupDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530176 // TODO: Modify forwarding rule on port-pair-group deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530177 }
178
179 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530180 public void onFlowClassifierCreated(FlowClassifier flowClassifier) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530181 log.debug("onFlowClassifierCreated");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530182 // TODO: Modify forwarding rule on flow-classifier creation.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530183 }
184
185 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530186 public void onFlowClassifierDeleted(FlowClassifier flowClassifier) {
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530187 log.debug("onFlowClassifierDeleted");
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530188 // TODO: Modify forwarding rule on flow-classifier deletion.
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530189 }
190
191 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530192 public void onPortChainCreated(PortChain portChain) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700193 NshServicePathId nshSpi;
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530194 log.info("onPortChainCreated");
195 if (nshSpiPortChainMap.containsKey(portChain.portChainId())) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700196 nshSpi = nshSpiPortChainMap.get(portChain.portChainId());
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530197 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700198 nshSpi = NshServicePathId.of(NshSpiIdGenerators.create());
199 nshSpiPortChainMap.put(portChain.portChainId(), nshSpi);
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530200 }
201
202 // install in OVS.
Jonathan Hart51539b82015-10-29 09:53:04 -0700203 flowClassifierInstallerService.installFlowClassifier(portChain, nshSpi);
204 serviceFunctionForwarderService.installForwardingRule(portChain, nshSpi);
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530205 }
206
207 @Override
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530208 public void onPortChainDeleted(PortChain portChain) {
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530209 log.info("onPortChainDeleted");
210 if (!nshSpiPortChainMap.containsKey(portChain.portChainId())) {
211 throw new ItemNotFoundException("Unable to find NSH SPI");
212 }
213
Jonathan Hart51539b82015-10-29 09:53:04 -0700214 NshServicePathId nshSpi = nshSpiPortChainMap.get(portChain.portChainId());
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530215 // uninstall from OVS.
Jonathan Hart51539b82015-10-29 09:53:04 -0700216 flowClassifierInstallerService.unInstallFlowClassifier(portChain, nshSpi);
217 serviceFunctionForwarderService.unInstallForwardingRule(portChain, nshSpi);
Mahesh Poojary Huawei15092f62015-12-10 11:09:02 +0530218
219 // remove SPI. No longer it will be used.
Jonathan Hart51539b82015-10-29 09:53:04 -0700220 nshSpiPortChainMap.remove(nshSpi);
Mahesh Poojary S335e7c32015-10-29 10:16:51 +0530221 }
Mahesh Poojary Sc9c10f92015-11-30 17:18:05 +0530222}