blob: 93fc7d81512deb47c704650127c3fa914f6eadd4 [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
yoonseondc3210d2017-01-25 16:03:10 -080036/**
37 * Simple single instance implementation of the virtual packet store.
38 */
yoonseon322c9c32016-12-07 16:47:02 -080039public 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}