blob: 84caf8025f3fc8e9632587416c524b8edeff55c3 [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;
yoonseon97b9b592017-01-31 14:35:06 -080023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
yoonseon322c9c32016-12-07 16:47:02 -080027import org.onosproject.incubator.net.virtual.NetworkId;
28import org.onosproject.incubator.net.virtual.VirtualNetworkPacketStore;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.packet.OutboundPacket;
31import org.onosproject.net.packet.PacketEvent;
32import org.onosproject.net.packet.PacketRequest;
33import org.onosproject.net.packet.PacketStoreDelegate;
yoonseon97b9b592017-01-31 14:35:06 -080034import org.slf4j.Logger;
yoonseon322c9c32016-12-07 16:47:02 -080035
36import java.util.HashSet;
37import java.util.List;
38import java.util.Map;
39import java.util.Set;
40
yoonseon97b9b592017-01-31 14:35:06 -080041import static org.slf4j.LoggerFactory.getLogger;
42
yoonseondc3210d2017-01-25 16:03:10 -080043/**
44 * Simple single instance implementation of the virtual packet store.
45 */
yoonseon97b9b592017-01-31 14:35:06 -080046//TODO: support distributed packet store for virtual networks
47
48@Component(immediate = true)
49@Service
yoonseon322c9c32016-12-07 16:47:02 -080050public class SimpleVirtualPacketStore
51 extends AbstractVirtualStore<PacketEvent, PacketStoreDelegate>
52 implements VirtualNetworkPacketStore {
53
yoonseon97b9b592017-01-31 14:35:06 -080054 private final Logger log = getLogger(getClass());
55
yoonseon322c9c32016-12-07 16:47:02 -080056 private Map<NetworkId, Map<TrafficSelector, Set<PacketRequest>>> requests
57 = Maps.newConcurrentMap();
58
yoonseon97b9b592017-01-31 14:35:06 -080059 @Activate
60 public void activate() {
61 log.info("Started");
62 }
63
64 @Deactivate
65 public void deactivate() {
66 log.info("Stopped");
67 }
68
yoonseon322c9c32016-12-07 16:47:02 -080069 @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 Chiu93ce3e82017-02-18 14:28:22 -050080 if (hasDelegate(networkId)) {
81 delegateMap.get(networkId).requestPackets(request);
82 }
yoonseon322c9c32016-12-07 16:47:02 -080083 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 Chiu93ce3e82017-02-18 14:28:22 -0500104 if (hasDelegate(networkId)) {
105 delegateMap.get(networkId).cancelPackets(request);
106 }
yoonseon322c9c32016-12-07 16:47:02 -0800107 if (newRequests.size() > 0) {
108 return ImmutableSet.copyOf(newRequests);
109 } else {
yoonseon322c9c32016-12-07 16:47:02 -0800110 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 Chiu1f036b82017-03-09 16:45:56 -0500121 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 }
yoonseon322c9c32016-12-07 16:47:02 -0800125 return list;
126 }
127}