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