blob: 054ff05925af94754dba5378c016cdecc3956127 [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;
Brian O'Connor21b028e2015-10-08 22:50:02 -070022import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.packet.OutboundPacket;
24import org.onosproject.net.packet.PacketEvent;
25import org.onosproject.net.packet.PacketEvent.Type;
alshabib42947782015-03-31 14:59:06 -070026import org.onosproject.net.packet.PacketRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.packet.PacketStore;
28import org.onosproject.net.packet.PacketStoreDelegate;
29import org.onosproject.store.AbstractStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Component;
Jonathan Hart4f60f982014-10-27 08:11:17 -070031
Brian O'Connor21b028e2015-10-08 22:50:02 -070032import java.util.HashSet;
Thomas Vachuska7f171b22015-08-21 12:49:08 -070033import java.util.List;
Brian O'Connor21b028e2015-10-08 22:50:02 -070034import java.util.Map;
alshabib42947782015-03-31 14:59:06 -070035import java.util.Set;
36
Jonathan Hart4f60f982014-10-27 08:11:17 -070037/**
38 * Simple single instance implementation of the packet store.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Component(immediate = true, service = PacketStore.class)
Jonathan Hart4f60f982014-10-27 08:11:17 -070041public class SimplePacketStore
42 extends AbstractStore<PacketEvent, PacketStoreDelegate>
43 implements PacketStore {
44
Brian O'Connor21b028e2015-10-08 22:50:02 -070045 private Map<TrafficSelector, Set<PacketRequest>> requests = Maps.newConcurrentMap();
alshabib42947782015-03-31 14:59:06 -070046
Jonathan Hart4f60f982014-10-27 08:11:17 -070047 @Override
48 public void emit(OutboundPacket packet) {
49 notifyDelegate(new PacketEvent(Type.EMIT, packet));
50 }
51
alshabib42947782015-03-31 14:59:06 -070052 @Override
Brian O'Connor21b028e2015-10-08 22:50:02 -070053 public void requestPackets(PacketRequest request) {
54 requests.compute(request.selector(), (s, existingRequests) -> {
55 if (existingRequests == null) {
56 return ImmutableSet.of(request);
57 } else if (!existingRequests.contains(request)) {
58 if (delegate != null) {
59 delegate.requestPackets(request);
60 }
61 return ImmutableSet.<PacketRequest>builder()
62 .addAll(existingRequests)
63 .add(request)
64 .build();
65 } else {
66 return existingRequests;
67 }
68 });
alshabib42947782015-03-31 14:59:06 -070069 }
70
71 @Override
Brian O'Connor21b028e2015-10-08 22:50:02 -070072 public void cancelPackets(PacketRequest request) {
73 requests.computeIfPresent(request.selector(), (s, existingRequests) -> {
74 if (existingRequests.contains(request)) {
75 HashSet<PacketRequest> newRequests = Sets.newHashSet(existingRequests);
76 newRequests.remove(request);
77 if (newRequests.size() > 0) {
78 return ImmutableSet.copyOf(newRequests);
79 } else {
80 if (delegate != null) {
81 delegate.cancelPackets(request);
82 }
83 return null;
84 }
85 } else {
86 return existingRequests;
87 }
88 });
Thomas Vachuska27bee092015-06-23 19:03:10 -070089 }
90
91 @Override
Thomas Vachuska7f171b22015-08-21 12:49:08 -070092 public List<PacketRequest> existingRequests() {
Brian O'Connor21b028e2015-10-08 22:50:02 -070093 List<PacketRequest> list = Lists.newArrayList();
94 requests.values().forEach(list::addAll);
95 list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue());
96 return list;
alshabib42947782015-03-31 14:59:06 -070097 }
98
Jonathan Hart4f60f982014-10-27 08:11:17 -070099}