blob: 78e88a2b9a28ea9e7df1b842219092c1e914013d [file] [log] [blame]
Jonathan Hart4f60f982014-10-27 08:11:17 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart4f60f982014-10-27 08:11:17 -07003 *
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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Jonathan Hart4f60f982014-10-27 08:11:17 -070017
Brian O'Connor21b028e2015-10-08 22:50:02 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
alshabib42947782015-03-31 14:59:06 -070021import com.google.common.collect.Sets;
Jonathan Hart4f60f982014-10-27 08:11:17 -070022import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Service;
Brian O'Connor21b028e2015-10-08 22:50:02 -070024import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.packet.OutboundPacket;
26import org.onosproject.net.packet.PacketEvent;
27import org.onosproject.net.packet.PacketEvent.Type;
alshabib42947782015-03-31 14:59:06 -070028import org.onosproject.net.packet.PacketRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.packet.PacketStore;
30import org.onosproject.net.packet.PacketStoreDelegate;
31import org.onosproject.store.AbstractStore;
Jonathan Hart4f60f982014-10-27 08:11:17 -070032
Brian O'Connor21b028e2015-10-08 22:50:02 -070033import java.util.HashSet;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070034import java.util.List;
Brian O'Connor21b028e2015-10-08 22:50:02 -070035import java.util.Map;
alshabib42947782015-03-31 14:59:06 -070036import java.util.Set;
37
Jonathan Hart4f60f982014-10-27 08:11:17 -070038/**
39 * Simple single instance implementation of the packet store.
40 */
41@Component(immediate = true)
42@Service
43public class SimplePacketStore
44 extends AbstractStore<PacketEvent, PacketStoreDelegate>
45 implements PacketStore {
46
Brian O'Connor21b028e2015-10-08 22:50:02 -070047 private Map<TrafficSelector, Set<PacketRequest>> requests = Maps.newConcurrentMap();
alshabib42947782015-03-31 14:59:06 -070048
Jonathan Hart4f60f982014-10-27 08:11:17 -070049 @Override
50 public void emit(OutboundPacket packet) {
51 notifyDelegate(new PacketEvent(Type.EMIT, packet));
52 }
53
alshabib42947782015-03-31 14:59:06 -070054 @Override
Brian O'Connor21b028e2015-10-08 22:50:02 -070055 public void requestPackets(PacketRequest request) {
56 requests.compute(request.selector(), (s, existingRequests) -> {
57 if (existingRequests == null) {
58 return ImmutableSet.of(request);
59 } else if (!existingRequests.contains(request)) {
60 if (delegate != null) {
61 delegate.requestPackets(request);
62 }
63 return ImmutableSet.<PacketRequest>builder()
64 .addAll(existingRequests)
65 .add(request)
66 .build();
67 } else {
68 return existingRequests;
69 }
70 });
alshabib42947782015-03-31 14:59:06 -070071 }
72
73 @Override
Brian O'Connor21b028e2015-10-08 22:50:02 -070074 public void cancelPackets(PacketRequest request) {
75 requests.computeIfPresent(request.selector(), (s, existingRequests) -> {
76 if (existingRequests.contains(request)) {
77 HashSet<PacketRequest> newRequests = Sets.newHashSet(existingRequests);
78 newRequests.remove(request);
79 if (newRequests.size() > 0) {
80 return ImmutableSet.copyOf(newRequests);
81 } else {
82 if (delegate != null) {
83 delegate.cancelPackets(request);
84 }
85 return null;
86 }
87 } else {
88 return existingRequests;
89 }
90 });
Thomas Vachuska27bee092015-06-23 19:03:10 -070091 }
92
93 @Override
Thomas Vachuska7f171b22015-08-21 12:49:08 -070094 public List<PacketRequest> existingRequests() {
Brian O'Connor21b028e2015-10-08 22:50:02 -070095 List<PacketRequest> list = Lists.newArrayList();
96 requests.values().forEach(list::addAll);
97 list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue());
98 return list;
alshabib42947782015-03-31 14:59:06 -070099 }
100
Jonathan Hart4f60f982014-10-27 08:11:17 -0700101}