yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.incubator.store.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkPacketStore; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.packet.OutboundPacket; |
| 27 | import org.onosproject.net.packet.PacketEvent; |
| 28 | import org.onosproject.net.packet.PacketRequest; |
| 29 | import org.onosproject.net.packet.PacketStoreDelegate; |
| 30 | |
| 31 | import java.util.HashSet; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
| 35 | |
yoonseon | dc3210d | 2017-01-25 16:03:10 -0800 | [diff] [blame^] | 36 | /** |
| 37 | * Simple single instance implementation of the virtual packet store. |
| 38 | */ |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 39 | public class SimpleVirtualPacketStore |
| 40 | extends AbstractVirtualStore<PacketEvent, PacketStoreDelegate> |
| 41 | implements VirtualNetworkPacketStore { |
| 42 | |
| 43 | private Map<NetworkId, Map<TrafficSelector, Set<PacketRequest>>> requests |
| 44 | = Maps.newConcurrentMap(); |
| 45 | |
| 46 | @Override |
| 47 | public void emit(NetworkId networkId, OutboundPacket packet) { |
| 48 | notifyDelegate(networkId, new PacketEvent(PacketEvent.Type.EMIT, packet)); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void requestPackets(NetworkId networkId, PacketRequest request) { |
| 53 | requests.computeIfAbsent(networkId, k -> Maps.newConcurrentMap()); |
| 54 | |
| 55 | requests.get(networkId).compute(request.selector(), (s, existingRequests) -> { |
| 56 | if (existingRequests == null) { |
| 57 | return ImmutableSet.of(request); |
| 58 | } else if (!existingRequests.contains(request)) { |
| 59 | if (hasDelegate(networkId)) { |
| 60 | delegateMap.get(networkId).requestPackets(request); |
| 61 | } |
| 62 | return ImmutableSet.<PacketRequest>builder() |
| 63 | .addAll(existingRequests) |
| 64 | .add(request) |
| 65 | .build(); |
| 66 | } else { |
| 67 | return existingRequests; |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void cancelPackets(NetworkId networkId, PacketRequest request) { |
| 74 | requests.get(networkId).computeIfPresent(request.selector(), (s, existingRequests) -> { |
| 75 | if (existingRequests.contains(request)) { |
| 76 | HashSet<PacketRequest> newRequests = Sets.newHashSet(existingRequests); |
| 77 | newRequests.remove(request); |
| 78 | if (newRequests.size() > 0) { |
| 79 | return ImmutableSet.copyOf(newRequests); |
| 80 | } else { |
| 81 | if (hasDelegate(networkId)) { |
| 82 | delegateMap.get(networkId).cancelPackets(request); |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | } else { |
| 87 | return existingRequests; |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public List<PacketRequest> existingRequests(NetworkId networkId) { |
| 94 | List<PacketRequest> list = Lists.newArrayList(); |
| 95 | requests.get(networkId).values().forEach(list::addAll); |
| 96 | list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue()); |
| 97 | return list; |
| 98 | } |
| 99 | } |