blob: 944f3e7d89a60e33991d01a0a4958c75860a5006 [file] [log] [blame]
yoonseon322c9c32016-12-07 16:47:02 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
yoonseon322c9c32016-12-07 16:47:02 -08003 *
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
yoonseon97b9b592017-01-31 14:35:06 -080033import org.slf4j.Logger;
yoonseon322c9c32016-12-07 16:47:02 -080034
35import java.util.HashSet;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39
yoonseon97b9b592017-01-31 14:35:06 -080040import static org.slf4j.LoggerFactory.getLogger;
41
yoonseondc3210d2017-01-25 16:03:10 -080042/**
43 * Simple single instance implementation of the virtual packet store.
44 */
yoonseon97b9b592017-01-31 14:35:06 -080045//TODO: support distributed packet store for virtual networks
46
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Component(immediate = true, service = VirtualNetworkPacketStore.class)
yoonseon322c9c32016-12-07 16:47:02 -080048public class SimpleVirtualPacketStore
49 extends AbstractVirtualStore<PacketEvent, PacketStoreDelegate>
50 implements VirtualNetworkPacketStore {
51
yoonseon97b9b592017-01-31 14:35:06 -080052 private final Logger log = getLogger(getClass());
53
yoonseon322c9c32016-12-07 16:47:02 -080054 private Map<NetworkId, Map<TrafficSelector, Set<PacketRequest>>> requests
55 = Maps.newConcurrentMap();
56
yoonseon97b9b592017-01-31 14:35:06 -080057 @Activate
58 public void activate() {
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 log.info("Stopped");
65 }
66
yoonseon322c9c32016-12-07 16:47:02 -080067 @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 Chiu93ce3e82017-02-18 14:28:22 -050078 if (hasDelegate(networkId)) {
79 delegateMap.get(networkId).requestPackets(request);
80 }
yoonseon322c9c32016-12-07 16:47:02 -080081 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 Chiu93ce3e82017-02-18 14:28:22 -0500102 if (hasDelegate(networkId)) {
103 delegateMap.get(networkId).cancelPackets(request);
104 }
yoonseon322c9c32016-12-07 16:47:02 -0800105 if (newRequests.size() > 0) {
106 return ImmutableSet.copyOf(newRequests);
107 } else {
yoonseon322c9c32016-12-07 16:47:02 -0800108 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 Chiu1f036b82017-03-09 16:45:56 -0500119 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 }
yoonseon322c9c32016-12-07 16:47:02 -0800123 return list;
124 }
125}