blob: 49d96966e7fc711758773bb8938a8a5a897219df [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;
34import org.onosproject.net.device.DeviceService;
35import org.onosproject.net.driver.Driver;
36import org.onosproject.net.driver.DriverHandler;
37import org.onosproject.net.driver.DriverService;
38import org.onosproject.net.flowobjective.FilteringObjective;
39import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.ForwardingObjective;
41import org.onosproject.net.flowobjective.NextObjective;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45import java.util.Collection;
46import java.util.Map;
47import java.util.concurrent.Future;
48
49import static com.google.common.base.Preconditions.checkState;
50
51/**
52 * Created by ash on 07/04/15.
53 */
54@Component(immediate = true)
55@Service
56public class FlowObjectiveManager implements FlowObjectiveService {
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected DriverService driverService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected DeviceService deviceService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected MastershipService mastershipService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ClusterService clusterService;
71
72 protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
73
74 private MastershipListener mastershipListener = new InnerMastershipListener();
75
76 private Map<DeviceId, DriverHandler> driverHandlers =
77 Maps.newConcurrentMap();
78
79 @Activate
80 protected void activate() {
81 mastershipService.addListener(mastershipListener);
82 log.info("Started");
83 }
84
85 @Deactivate
86 protected void deactivate() {
87 mastershipService.removeListener(mastershipListener);
88 log.info("Stopped");
89 }
90
91 @Override
92 public Future<Boolean> filter(DeviceId deviceId,
93 Collection<FilteringObjective> filterObjectives) {
94 DriverHandler handler = driverHandlers.get(deviceId);
95 checkState(handler != null, "Driver not initialized");
96
97 Pipeliner pipe = handler.behaviour(Pipeliner.class);
98
99 return pipe.filter(filterObjectives);
100 }
101
102 @Override
103 public Future<Boolean> forward(DeviceId deviceId,
104 Collection<ForwardingObjective> forwardingObjectives) {
105 DriverHandler handler = driverHandlers.get(deviceId);
106 checkState(handler != null, "Driver not initialized");
107
108 Pipeliner pipe = handler.behaviour(Pipeliner.class);
109
110 return pipe.forward(forwardingObjectives);
111 }
112
113 @Override
114 public Future<Boolean> next(DeviceId deviceId,
115 Collection<NextObjective> nextObjectives) {
116 DriverHandler handler = driverHandlers.get(deviceId);
117 checkState(handler != null, "Driver not initialized");
118
119 Pipeliner pipe = handler.behaviour(Pipeliner.class);
120
121 return pipe.next(nextObjectives);
122 }
123
124 private class InnerMastershipListener implements MastershipListener {
125 @Override
126 public void event(MastershipEvent event) {
127 switch (event.type()) {
128
129 case MASTER_CHANGED:
130 //TODO: refactor this into a method
131 if (event.roleInfo().master().equals(
132 clusterService.getLocalNode().id())) {
133 DriverHandler handler = lookupDriver(event.subject());
134 if (handler != null) {
135 Pipeliner pipe = handler.behaviour(Pipeliner.class);
136 pipe.init(event.subject(), serviceDirectory);
137 driverHandlers.put(event.subject(), handler);
138 log.info("Driver {} bound to device {}",
139 handler.data().type().name(), event.subject());
140 } else {
141 log.error("No driver for device {}", event.subject());
142 }
143
144 }
145
146 break;
147 case BACKUPS_CHANGED:
148 break;
149 default:
150 log.warn("Unknown mastership type {}", event.type());
151 }
152 }
153
154 private DriverHandler lookupDriver(DeviceId deviceId) {
155 Device device = deviceService.getDevice(deviceId);
156
157 Driver driver = driverService.getDriver(device.manufacturer(),
158 device.hwVersion(), device.swVersion());
159
160 return driverService.createHandler(driver.name(), deviceId);
161 }
162 }
163}