yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [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 | */ |
| 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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
yoonseon | 97b9b59 | 2017-01-31 14:35:06 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 34 | |
| 35 | import java.util.HashSet; |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | import java.util.Set; |
| 39 | |
yoonseon | 97b9b59 | 2017-01-31 14:35:06 -0800 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
yoonseon | dc3210d | 2017-01-25 16:03:10 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Simple single instance implementation of the virtual packet store. |
| 44 | */ |
yoonseon | 97b9b59 | 2017-01-31 14:35:06 -0800 | [diff] [blame] | 45 | //TODO: support distributed packet store for virtual networks |
| 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 47 | @Component(immediate = true, service = VirtualNetworkPacketStore.class) |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 48 | public class SimpleVirtualPacketStore |
| 49 | extends AbstractVirtualStore<PacketEvent, PacketStoreDelegate> |
| 50 | implements VirtualNetworkPacketStore { |
| 51 | |
yoonseon | 97b9b59 | 2017-01-31 14:35:06 -0800 | [diff] [blame] | 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 54 | private Map<NetworkId, Map<TrafficSelector, Set<PacketRequest>>> requests |
| 55 | = Maps.newConcurrentMap(); |
| 56 | |
yoonseon | 97b9b59 | 2017-01-31 14:35:06 -0800 | [diff] [blame] | 57 | @Activate |
| 58 | public void activate() { |
| 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
| 64 | log.info("Stopped"); |
| 65 | } |
| 66 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 67 | @Override |
| 68 | public void emit(NetworkId networkId, OutboundPacket packet) { |
| 69 | notifyDelegate(networkId, new PacketEvent(PacketEvent.Type.EMIT, packet)); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void requestPackets(NetworkId networkId, PacketRequest request) { |
| 74 | requests.computeIfAbsent(networkId, k -> Maps.newConcurrentMap()); |
| 75 | |
| 76 | requests.get(networkId).compute(request.selector(), (s, existingRequests) -> { |
| 77 | if (existingRequests == null) { |
Claudine Chiu | 93ce3e8 | 2017-02-18 14:28:22 -0500 | [diff] [blame] | 78 | if (hasDelegate(networkId)) { |
| 79 | delegateMap.get(networkId).requestPackets(request); |
| 80 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 81 | return ImmutableSet.of(request); |
| 82 | } else if (!existingRequests.contains(request)) { |
| 83 | if (hasDelegate(networkId)) { |
| 84 | delegateMap.get(networkId).requestPackets(request); |
| 85 | } |
| 86 | return ImmutableSet.<PacketRequest>builder() |
| 87 | .addAll(existingRequests) |
| 88 | .add(request) |
| 89 | .build(); |
| 90 | } else { |
| 91 | return existingRequests; |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void cancelPackets(NetworkId networkId, PacketRequest request) { |
| 98 | requests.get(networkId).computeIfPresent(request.selector(), (s, existingRequests) -> { |
| 99 | if (existingRequests.contains(request)) { |
| 100 | HashSet<PacketRequest> newRequests = Sets.newHashSet(existingRequests); |
| 101 | newRequests.remove(request); |
Claudine Chiu | 93ce3e8 | 2017-02-18 14:28:22 -0500 | [diff] [blame] | 102 | if (hasDelegate(networkId)) { |
| 103 | delegateMap.get(networkId).cancelPackets(request); |
| 104 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 105 | if (newRequests.size() > 0) { |
| 106 | return ImmutableSet.copyOf(newRequests); |
| 107 | } else { |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 108 | return null; |
| 109 | } |
| 110 | } else { |
| 111 | return existingRequests; |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public List<PacketRequest> existingRequests(NetworkId networkId) { |
| 118 | List<PacketRequest> list = Lists.newArrayList(); |
Claudine Chiu | 1f036b8 | 2017-03-09 16:45:56 -0500 | [diff] [blame] | 119 | if (requests.get(networkId) != null) { |
| 120 | requests.get(networkId).values().forEach(list::addAll); |
| 121 | list.sort((o1, o2) -> o1.priority().priorityValue() - o2.priority().priorityValue()); |
| 122 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 123 | return list; |
| 124 | } |
| 125 | } |