Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.packet.OutboundPacket; |
| 26 | import org.onosproject.net.packet.PacketEvent; |
| 27 | import org.onosproject.net.packet.PacketEvent.Type; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 28 | import org.onosproject.net.packet.PacketRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.packet.PacketStore; |
| 30 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 31 | import org.onosproject.store.AbstractStore; |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 32 | |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 33 | import java.util.HashSet; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 34 | import java.util.List; |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 35 | import java.util.Map; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 36 | import java.util.Set; |
| 37 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Simple single instance implementation of the packet store. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class SimplePacketStore |
| 44 | extends AbstractStore<PacketEvent, PacketStoreDelegate> |
| 45 | implements PacketStore { |
| 46 | |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 47 | private Map<TrafficSelector, Set<PacketRequest>> requests = Maps.newConcurrentMap(); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 48 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 49 | @Override |
| 50 | public void emit(OutboundPacket packet) { |
| 51 | notifyDelegate(new PacketEvent(Type.EMIT, packet)); |
| 52 | } |
| 53 | |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 54 | @Override |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 55 | 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 | }); |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 74 | 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 Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 94 | public List<PacketRequest> existingRequests() { |
Brian O'Connor | 21b028e | 2015-10-08 22:50:02 -0700 | [diff] [blame] | 95 | 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; |
alshabib | 4294778 | 2015-03-31 14:59:06 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 101 | } |