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