blob: 78650fe65b9ea3e3165626549cb3288275e1bc70 [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
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
17package org.onosproject.provider.of.group.impl;
18
19import com.google.common.collect.Maps;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070020
sangho5afd02a2015-02-03 20:07:35 -080021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onosproject.core.DefaultGroupId;
27import org.onosproject.core.GroupId;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.group.DefaultGroup;
30import org.onosproject.net.group.Group;
31import org.onosproject.net.group.GroupBuckets;
32import org.onosproject.net.group.GroupDescription;
33import org.onosproject.net.group.GroupOperation;
34import org.onosproject.net.group.GroupOperations;
35import org.onosproject.net.group.GroupProvider;
36import org.onosproject.net.group.GroupProviderRegistry;
37import org.onosproject.net.group.GroupProviderService;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070038import org.onosproject.net.group.StoredGroupBucketEntry;
sangho5afd02a2015-02-03 20:07:35 -080039import org.onosproject.net.provider.AbstractProvider;
40import org.onosproject.net.provider.ProviderId;
41import org.onosproject.openflow.controller.Dpid;
42import org.onosproject.openflow.controller.OpenFlowController;
43import org.onosproject.openflow.controller.OpenFlowEventListener;
44import org.onosproject.openflow.controller.OpenFlowSwitch;
45import org.onosproject.openflow.controller.OpenFlowSwitchListener;
46import org.onosproject.openflow.controller.RoleState;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070047import org.projectfloodlight.openflow.protocol.OFBucketCounter;
sangho5afd02a2015-02-03 20:07:35 -080048import org.projectfloodlight.openflow.protocol.OFErrorMsg;
49import org.projectfloodlight.openflow.protocol.OFErrorType;
50import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
51import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
52import org.projectfloodlight.openflow.protocol.OFGroupMod;
53import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
54import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
55import org.projectfloodlight.openflow.protocol.OFGroupType;
56import org.projectfloodlight.openflow.protocol.OFMessage;
57import org.projectfloodlight.openflow.protocol.OFPortStatus;
58import org.projectfloodlight.openflow.protocol.OFStatsReply;
59import org.projectfloodlight.openflow.protocol.OFStatsType;
sangho01a883c2015-02-23 11:01:50 -080060import org.projectfloodlight.openflow.protocol.OFVersion;
sangho5afd02a2015-02-03 20:07:35 -080061import org.slf4j.Logger;
62
63import java.util.Collection;
64import java.util.Map;
65import java.util.Optional;
66import java.util.concurrent.atomic.AtomicLong;
67
68import static org.slf4j.LoggerFactory.getLogger;
69
70/**
71 * Provider which uses an OpenFlow controller to handle Group.
72 */
73@Component(immediate = true)
74public class OpenFlowGroupProvider extends AbstractProvider implements GroupProvider {
75
76 private final Logger log = getLogger(getClass());
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected OpenFlowController controller;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected GroupProviderRegistry providerRegistry;
83
84 private GroupProviderService providerService;
85
86 static final int POLL_INTERVAL = 10;
87
88 private final InternalGroupProvider listener = new InternalGroupProvider();
89
sanghoa09f2742015-02-06 14:49:47 -080090 private static final AtomicLong XID_COUNTER = new AtomicLong(1);
sangho5afd02a2015-02-03 20:07:35 -080091 private final Map<Dpid, GroupStatsCollector> collectors = Maps.newHashMap();
sanghoa09f2742015-02-06 14:49:47 -080092 private final Map<Long, OFStatsReply> groupStats = Maps.newConcurrentMap();
sangho22a805d2015-02-13 09:45:43 -080093 private final Map<GroupId, GroupOperation> pendingGroupOperations =
sangho5afd02a2015-02-03 20:07:35 -080094 Maps.newConcurrentMap();
95
sanghoa09f2742015-02-06 14:49:47 -080096 /* Map<Group ID, Transaction ID> */
sangho22a805d2015-02-13 09:45:43 -080097 private final Map<GroupId, Long> pendingXidMaps = Maps.newConcurrentMap();
sanghoa09f2742015-02-06 14:49:47 -080098
sangho5afd02a2015-02-03 20:07:35 -080099 /**
100 * Creates a OpenFlow group provider.
101 */
102 public OpenFlowGroupProvider() {
103 super(new ProviderId("of", "org.onosproject.provider.group"));
104 }
105
106 @Activate
107 public void activate() {
108 providerService = providerRegistry.register(this);
109 controller.addListener(listener);
110 controller.addEventListener(listener);
111
112 for (OpenFlowSwitch sw : controller.getSwitches()) {
sangho01a883c2015-02-23 11:01:50 -0800113 if (isGroupSupported(sw)) {
114 GroupStatsCollector gsc = new GroupStatsCollector(sw, POLL_INTERVAL);
115 gsc.start();
116 collectors.put(new Dpid(sw.getId()), gsc);
117 }
sangho5afd02a2015-02-03 20:07:35 -0800118 }
119
120 log.info("Started");
121 }
122
123 @Deactivate
124 public void deactivate() {
125 providerRegistry.unregister(this);
126 providerService = null;
127
128 log.info("Stopped");
129 }
130
131 @Override
132 public void performGroupOperation(DeviceId deviceId, GroupOperations groupOps) {
133 Map<OFGroupMod, OpenFlowSwitch> mods = Maps.newIdentityHashMap();
134 final Dpid dpid = Dpid.dpid(deviceId.uri());
135 OpenFlowSwitch sw = controller.getSwitch(dpid);
136 for (GroupOperation groupOperation: groupOps.operations()) {
137 if (sw == null) {
sanghoa09f2742015-02-06 14:49:47 -0800138 log.error("SW {} is not found", dpid);
sangho5afd02a2015-02-03 20:07:35 -0800139 return;
140 }
sanghoa09f2742015-02-06 14:49:47 -0800141 final Long groupModXid = XID_COUNTER.getAndIncrement();
sangho5afd02a2015-02-03 20:07:35 -0800142 GroupModBuilder builder =
143 GroupModBuilder.builder(groupOperation.buckets(),
144 groupOperation.groupId(),
145 groupOperation.groupType(),
146 sw.factory(),
147 Optional.of(groupModXid));
148 OFGroupMod groupMod = null;
149 switch (groupOperation.opType()) {
150 case ADD:
151 groupMod = builder.buildGroupAdd();
152 break;
153 case MODIFY:
154 groupMod = builder.buildGroupMod();
155 break;
156 case DELETE:
157 groupMod = builder.buildGroupDel();
158 break;
159 default:
160 log.error("Unsupported Group operation");
161 }
162 sw.sendMsg(groupMod);
sangho22a805d2015-02-13 09:45:43 -0800163 GroupId groudId = new DefaultGroupId(groupMod.getGroup().getGroupNumber());
164 pendingGroupOperations.put(groudId, groupOperation);
165 pendingXidMaps.put(groudId, groupModXid);
sangho5afd02a2015-02-03 20:07:35 -0800166 }
167 }
168
169 private void pushGroupMetrics(Dpid dpid, OFStatsReply statsReply) {
170 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
171
172 OFGroupStatsReply groupStatsReply = null;
173 OFGroupDescStatsReply groupDescStatsReply = null;
174
sanghoa09f2742015-02-06 14:49:47 -0800175 synchronized (groupStats) {
176 if (statsReply.getStatsType() == OFStatsType.GROUP) {
177 OFStatsReply reply = groupStats.get(statsReply.getXid() + 1);
178 if (reply != null) {
179 groupStatsReply = (OFGroupStatsReply) statsReply;
180 groupDescStatsReply = (OFGroupDescStatsReply) reply;
181 groupStats.remove(statsReply.getXid() + 1);
182 } else {
183 groupStats.put(statsReply.getXid(), statsReply);
184 }
185 } else if (statsReply.getStatsType() == OFStatsType.GROUP_DESC) {
186 OFStatsReply reply = groupStats.get(statsReply.getXid() - 1);
187 if (reply != null) {
188 groupStatsReply = (OFGroupStatsReply) reply;
189 groupDescStatsReply = (OFGroupDescStatsReply) statsReply;
190 groupStats.remove(statsReply.getXid() - 1);
191 } else {
192 groupStats.put(statsReply.getXid(), statsReply);
193 }
sangho5afd02a2015-02-03 20:07:35 -0800194 }
195 }
196
197 if (groupStatsReply != null && groupDescStatsReply != null) {
198 Collection<Group> groups = buildGroupMetrics(deviceId,
199 groupStatsReply, groupDescStatsReply);
200 providerService.pushGroupMetrics(deviceId, groups);
201 for (Group group: groups) {
202 pendingGroupOperations.remove(group.id());
sanghoa09f2742015-02-06 14:49:47 -0800203 pendingXidMaps.remove(group.id());
sangho5afd02a2015-02-03 20:07:35 -0800204 }
205 }
206 }
207
208 private Collection<Group> buildGroupMetrics(DeviceId deviceId,
209 OFGroupStatsReply groupStatsReply,
210 OFGroupDescStatsReply groupDescStatsReply) {
211
212 Map<Integer, Group> groups = Maps.newHashMap();
213
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700214
sangho5afd02a2015-02-03 20:07:35 -0800215 for (OFGroupDescStatsEntry entry: groupDescStatsReply.getEntries()) {
216 int id = entry.getGroup().getGroupNumber();
217 GroupId groupId = new DefaultGroupId(id);
218 GroupDescription.Type type = getGroupType(entry.getGroupType());
219 GroupBuckets buckets = new GroupBucketEntryBuilder(entry.getBuckets(),
220 entry.getGroupType()).build();
221 DefaultGroup group = new DefaultGroup(groupId, deviceId, type, buckets);
222 groups.put(id, group);
223 }
224
225 for (OFGroupStatsEntry entry: groupStatsReply.getEntries()) {
226 int groupId = entry.getGroup().getGroupNumber();
227 DefaultGroup group = (DefaultGroup) groups.get(groupId);
228 if (group != null) {
229 group.setBytes(entry.getByteCount().getValue());
230 group.setLife(entry.getDurationSec());
231 group.setPackets(entry.getPacketCount().getValue());
232 group.setReferenceCount(entry.getRefCount());
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -0700233 int bucketIndex = 0;
234 for (OFBucketCounter bucketStats:entry.getBucketStats()) {
235 ((StoredGroupBucketEntry) group.buckets().buckets()
236 .get(bucketIndex))
237 .setPackets(bucketStats
238 .getPacketCount().getValue());
239 ((StoredGroupBucketEntry) group.buckets().buckets()
240 .get(bucketIndex))
241 .setBytes(entry.getBucketStats()
242 .get(bucketIndex)
243 .getByteCount().getValue());
244 bucketIndex++;
245 }
sangho5afd02a2015-02-03 20:07:35 -0800246 }
247 }
248
249 return groups.values();
250 }
251
252 private GroupDescription.Type getGroupType(OFGroupType type) {
253 switch (type) {
254 case ALL:
255 return GroupDescription.Type.ALL;
256 case INDIRECT:
257 return GroupDescription.Type.INDIRECT;
258 case SELECT:
259 return GroupDescription.Type.SELECT;
260 case FF:
261 return GroupDescription.Type.FAILOVER;
262 default:
263 log.error("Unsupported OF group type : {}", type);
264 break;
265 }
266 return null;
267 }
268
sanghoa09f2742015-02-06 14:49:47 -0800269 /**
270 * Returns a transaction ID for entire group operations and increases
271 * the counter by the number given.
272 *
273 * @param increase the amount to increase the counter by
274 * @return a transaction ID
275 */
276 public static long getXidAndAdd(int increase) {
277 return XID_COUNTER.getAndAdd(increase);
278 }
279
sangho01a883c2015-02-23 11:01:50 -0800280 private boolean isGroupSupported(OpenFlowSwitch sw) {
281 if (sw.factory().getVersion() == OFVersion.OF_10 ||
282 sw.factory().getVersion() == OFVersion.OF_11 ||
283 sw.factory().getVersion() == OFVersion.OF_12) {
284 return false;
285 }
286
287 return true;
288 }
289
sangho5afd02a2015-02-03 20:07:35 -0800290 private class InternalGroupProvider
291 implements OpenFlowSwitchListener, OpenFlowEventListener {
292
293 @Override
294 public void handleMessage(Dpid dpid, OFMessage msg) {
295 switch (msg.getType()) {
296 case STATS_REPLY:
297 pushGroupMetrics(dpid, (OFStatsReply) msg);
298 break;
299 case ERROR:
300 OFErrorMsg errorMsg = (OFErrorMsg) msg;
301 if (errorMsg.getErrType() == OFErrorType.GROUP_MOD_FAILED) {
sangho22a805d2015-02-13 09:45:43 -0800302 GroupId pendingGroupId = null;
303 for (Map.Entry<GroupId, Long> entry: pendingXidMaps.entrySet()) {
sanghoa09f2742015-02-06 14:49:47 -0800304 if (entry.getValue() == errorMsg.getXid()) {
305 pendingGroupId = entry.getKey();
306 break;
307 }
308 }
sangho22a805d2015-02-13 09:45:43 -0800309 if (pendingGroupId == null) {
sanghoa09f2742015-02-06 14:49:47 -0800310 log.warn("Error for unknown group operation: {}",
311 errorMsg.getXid());
312 } else {
313 GroupOperation operation =
314 pendingGroupOperations.get(pendingGroupId);
sangho7ff01812015-02-09 16:21:53 -0800315 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
sanghoa09f2742015-02-06 14:49:47 -0800316 if (operation != null) {
sangho7ff01812015-02-09 16:21:53 -0800317 providerService.groupOperationFailed(deviceId,
318 operation);
sanghoa09f2742015-02-06 14:49:47 -0800319 pendingGroupOperations.remove(pendingGroupId);
320 pendingXidMaps.remove(pendingGroupId);
321 log.warn("Received an group mod error {}", msg);
322 } else {
323 log.error("Cannot find pending group operation with group ID: {}",
324 pendingGroupId);
325 }
sangho5afd02a2015-02-03 20:07:35 -0800326 }
327 break;
328 }
329 default:
Jonathan Hart7baba072015-02-23 14:27:59 -0800330 break;
sangho5afd02a2015-02-03 20:07:35 -0800331 }
332 }
333
334 @Override
335 public void switchAdded(Dpid dpid) {
sangho01a883c2015-02-23 11:01:50 -0800336 OpenFlowSwitch sw = controller.getSwitch(dpid);
337 if (isGroupSupported(sw)) {
338 GroupStatsCollector gsc = new GroupStatsCollector(
339 controller.getSwitch(dpid), POLL_INTERVAL);
340 gsc.start();
341 collectors.put(dpid, gsc);
342 }
sangho5afd02a2015-02-03 20:07:35 -0800343 }
344
345 @Override
346 public void switchRemoved(Dpid dpid) {
347 GroupStatsCollector collector = collectors.remove(dpid);
348 if (collector != null) {
349 collector.stop();
350 }
351 }
352
353 @Override
354 public void switchChanged(Dpid dpid) {
355 }
356
357 @Override
358 public void portChanged(Dpid dpid, OFPortStatus status) {
359 }
360
361 @Override
362 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
363 }
364 }
365
366}