blob: 840d482322824a43052837e2457471d7a4d99231 [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
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.net.flowobjective.impl;
17
18import com.google.common.collect.Maps;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.osgi.DefaultServiceDirectory;
26import org.onlab.osgi.ServiceDirectory;
27import org.onosproject.cluster.ClusterService;
28import org.onosproject.mastership.MastershipEvent;
29import org.onosproject.mastership.MastershipListener;
30import org.onosproject.mastership.MastershipService;
31import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.behaviour.Pipeliner;
alshabibaebe7752015-04-07 17:45:42 -070034import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
alshabib77b88482015-04-07 15:47:50 -070036import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.driver.Driver;
38import org.onosproject.net.driver.DriverHandler;
39import org.onosproject.net.driver.DriverService;
40import org.onosproject.net.flowobjective.FilteringObjective;
41import org.onosproject.net.flowobjective.FlowObjectiveService;
42import org.onosproject.net.flowobjective.ForwardingObjective;
43import org.onosproject.net.flowobjective.NextObjective;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
47import java.util.Collection;
48import java.util.Map;
49import java.util.concurrent.Future;
50
51import static com.google.common.base.Preconditions.checkState;
52
53/**
54 * Created by ash on 07/04/15.
55 */
56@Component(immediate = true)
57@Service
58public class FlowObjectiveManager implements FlowObjectiveService {
59
60 private final Logger log = LoggerFactory.getLogger(getClass());
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected DriverService driverService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DeviceService deviceService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected MastershipService mastershipService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected ClusterService clusterService;
73
74 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
75
76 private MastershipListener mastershipListener = new InnerMastershipListener();
77
alshabibaebe7752015-04-07 17:45:42 -070078 private DeviceListener deviceListener = new InnerDeviceListener();
79
alshabib77b88482015-04-07 15:47:50 -070080 private Map<DeviceId, DriverHandler> driverHandlers =
81 Maps.newConcurrentMap();
82
83 @Activate
84 protected void activate() {
85 mastershipService.addListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -070086 deviceService.addListener(deviceListener);
87 deviceService.getDevices().forEach(device -> setupDriver(device.id()));
alshabib77b88482015-04-07 15:47:50 -070088 log.info("Started");
89 }
90
91 @Deactivate
92 protected void deactivate() {
93 mastershipService.removeListener(mastershipListener);
alshabibaebe7752015-04-07 17:45:42 -070094 deviceService.removeListener(deviceListener);
alshabib77b88482015-04-07 15:47:50 -070095 log.info("Stopped");
96 }
97
98 @Override
99 public Future<Boolean> filter(DeviceId deviceId,
100 Collection<FilteringObjective> filterObjectives) {
101 DriverHandler handler = driverHandlers.get(deviceId);
102 checkState(handler != null, "Driver not initialized");
103
104 Pipeliner pipe = handler.behaviour(Pipeliner.class);
105
106 return pipe.filter(filterObjectives);
107 }
108
109 @Override
110 public Future<Boolean> forward(DeviceId deviceId,
111 Collection<ForwardingObjective> forwardingObjectives) {
112 DriverHandler handler = driverHandlers.get(deviceId);
113 checkState(handler != null, "Driver not initialized");
114
115 Pipeliner pipe = handler.behaviour(Pipeliner.class);
116
117 return pipe.forward(forwardingObjectives);
118 }
119
120 @Override
121 public Future<Boolean> next(DeviceId deviceId,
122 Collection<NextObjective> nextObjectives) {
123 DriverHandler handler = driverHandlers.get(deviceId);
124 checkState(handler != null, "Driver not initialized");
125
126 Pipeliner pipe = handler.behaviour(Pipeliner.class);
127
128 return pipe.next(nextObjectives);
129 }
130
alshabibaebe7752015-04-07 17:45:42 -0700131
132
alshabib77b88482015-04-07 15:47:50 -0700133 private class InnerMastershipListener implements MastershipListener {
134 @Override
135 public void event(MastershipEvent event) {
136 switch (event.type()) {
137
138 case MASTER_CHANGED:
alshabibaebe7752015-04-07 17:45:42 -0700139 setupDriver(event.subject());
alshabib77b88482015-04-07 15:47:50 -0700140
141 break;
142 case BACKUPS_CHANGED:
143 break;
144 default:
145 log.warn("Unknown mastership type {}", event.type());
146 }
147 }
148
alshabib77b88482015-04-07 15:47:50 -0700149
alshabibaebe7752015-04-07 17:45:42 -0700150 }
alshabib77b88482015-04-07 15:47:50 -0700151
alshabibaebe7752015-04-07 17:45:42 -0700152 private class InnerDeviceListener implements DeviceListener {
153 @Override
154 public void event(DeviceEvent event) {
155 switch (event.type()) {
156 case DEVICE_ADDED:
157 case DEVICE_AVAILABILITY_CHANGED:
158 setupDriver(event.subject().id());
159 break;
160 case DEVICE_UPDATED:
161 break;
162 case DEVICE_REMOVED:
163 break;
164 case DEVICE_SUSPENDED:
165 break;
166 case PORT_ADDED:
167 break;
168 case PORT_UPDATED:
169 break;
170 case PORT_REMOVED:
171 break;
172 default:
173 log.warn("Unknown event type {}", event.type());
174 }
alshabib77b88482015-04-07 15:47:50 -0700175 }
176 }
alshabibaebe7752015-04-07 17:45:42 -0700177
178 private void setupDriver(DeviceId deviceId) {
179 //TODO: Refactor this to make it nicer and use a cache.
180 if (mastershipService.getMasterFor(
181 deviceId).equals(clusterService.getLocalNode().id())) {
182
183 DriverHandler handler = lookupDriver(deviceId);
184 if (handler != null) {
185 Pipeliner pipe = handler.behaviour(Pipeliner.class);
186 pipe.init(deviceId, serviceDirectory);
187 driverHandlers.put(deviceId, handler);
188 log.info("Driver {} bound to device {}",
189 handler.data().type().name(), deviceId);
190 } else {
191 log.error("No driver for device {}", deviceId);
192 }
193 }
194 }
195
196
197 private DriverHandler lookupDriver(DeviceId deviceId) {
198 Device device = deviceService.getDevice(deviceId);
199 if (device == null) {
200 log.warn("Device is null!");
201 return null;
202 }
203 Driver driver = driverService.getDriver(device.manufacturer(),
204 device.hwVersion(), device.swVersion());
205
206 return driverService.createHandler(driver.name(), deviceId);
207 }
208
alshabib77b88482015-04-07 15:47:50 -0700209}