yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 17 | package org.onosproject.incubator.net.virtual.store.impl; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Maps; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 20 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 21 | import org.onosproject.incubator.net.virtual.VirtualNetworkFlowObjectiveStore; |
| 22 | import org.onosproject.net.behaviour.DefaultNextGroup; |
| 23 | import org.onosproject.net.behaviour.NextGroup; |
| 24 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
| 25 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
| 26 | import org.onosproject.store.service.AtomicCounter; |
| 27 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Reference; |
| 31 | import org.osgi.service.component.annotations.ReferenceCardinality; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.HashMap; |
| 35 | import java.util.Map; |
| 36 | import java.util.concurrent.BlockingQueue; |
| 37 | import java.util.concurrent.ConcurrentMap; |
| 38 | import java.util.concurrent.ExecutorService; |
| 39 | import java.util.concurrent.Executors; |
| 40 | import java.util.concurrent.LinkedBlockingQueue; |
| 41 | |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | import static org.slf4j.LoggerFactory.getLogger; |
| 44 | |
| 45 | /** |
| 46 | * Single instance implementation of store to manage |
| 47 | * the inventory of created next groups for virtual network. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Component(immediate = true, service = VirtualNetworkFlowObjectiveStore.class) |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 50 | public class SimpleVirtualFlowObjectiveStore |
| 51 | extends AbstractVirtualStore<ObjectiveEvent, FlowObjectiveStoreDelegate> |
| 52 | implements VirtualNetworkFlowObjectiveStore { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | private ConcurrentMap<NetworkId, ConcurrentMap<Integer, byte[]>> nextGroupsMap; |
| 57 | |
| 58 | private AtomicCounter nextIds; |
| 59 | |
| 60 | // event queue to separate map-listener threads from event-handler threads (tpool) |
| 61 | private BlockingQueue<VirtualObjectiveEvent> eventQ; |
| 62 | private ExecutorService tpool; |
| 63 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 65 | protected StorageService storageService; |
| 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | tpool = Executors.newFixedThreadPool(4, groupedThreads("onos/virtual/flobj-notifier", "%d", log)); |
| 70 | eventQ = new LinkedBlockingQueue<>(); |
| 71 | tpool.execute(new FlowObjectiveNotifier()); |
| 72 | |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 73 | initNextGroupsMap(); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 74 | |
| 75 | nextIds = storageService.getAtomicCounter("next-objective-counter"); |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 79 | public void deactivate() { |
| 80 | log.info("Stopped"); |
| 81 | } |
| 82 | |
| 83 | protected void initNextGroupsMap() { |
| 84 | nextGroupsMap = Maps.newConcurrentMap(); |
| 85 | } |
| 86 | |
| 87 | protected void updateNextGroupsMap(NetworkId networkId, |
| 88 | ConcurrentMap<Integer, byte[]> nextGroups) { |
| 89 | } |
| 90 | |
| 91 | protected ConcurrentMap<Integer, byte[]> getNextGroups(NetworkId networkId) { |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 92 | nextGroupsMap.computeIfAbsent(networkId, n -> Maps.newConcurrentMap()); |
| 93 | return nextGroupsMap.get(networkId); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void putNextGroup(NetworkId networkId, Integer nextId, NextGroup group) { |
| 98 | ConcurrentMap<Integer, byte[]> nextGroups = getNextGroups(networkId); |
| 99 | nextGroups.put(nextId, group.data()); |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 100 | updateNextGroupsMap(networkId, nextGroups); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 101 | |
| 102 | eventQ.add(new VirtualObjectiveEvent(networkId, ObjectiveEvent.Type.ADD, nextId)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public NextGroup getNextGroup(NetworkId networkId, Integer nextId) { |
| 107 | ConcurrentMap<Integer, byte[]> nextGroups = getNextGroups(networkId); |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 108 | byte[] groupData = nextGroups.get(nextId); |
| 109 | if (groupData != null) { |
| 110 | return new DefaultNextGroup(groupData); |
| 111 | } |
| 112 | return null; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public NextGroup removeNextGroup(NetworkId networkId, Integer nextId) { |
| 117 | ConcurrentMap<Integer, byte[]> nextGroups = getNextGroups(networkId); |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 118 | byte[] nextGroup = nextGroups.remove(nextId); |
| 119 | updateNextGroupsMap(networkId, nextGroups); |
| 120 | |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 121 | eventQ.add(new VirtualObjectiveEvent(networkId, ObjectiveEvent.Type.REMOVE, nextId)); |
Claudine Chiu | cdc4b8c | 2017-03-30 00:34:39 -0400 | [diff] [blame] | 122 | |
| 123 | return new DefaultNextGroup(nextGroup); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Map<Integer, NextGroup> getAllGroups(NetworkId networkId) { |
| 128 | ConcurrentMap<Integer, byte[]> nextGroups = getNextGroups(networkId); |
| 129 | |
| 130 | Map<Integer, NextGroup> nextGroupMappings = new HashMap<>(); |
| 131 | for (int key : nextGroups.keySet()) { |
| 132 | NextGroup nextGroup = getNextGroup(networkId, key); |
| 133 | if (nextGroup != null) { |
| 134 | nextGroupMappings.put(key, nextGroup); |
| 135 | } |
| 136 | } |
| 137 | return nextGroupMappings; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int allocateNextId(NetworkId networkId) { |
| 142 | return (int) nextIds.incrementAndGet(); |
| 143 | } |
| 144 | |
| 145 | private class FlowObjectiveNotifier implements Runnable { |
| 146 | @Override |
| 147 | public void run() { |
| 148 | try { |
| 149 | while (!Thread.currentThread().isInterrupted()) { |
| 150 | VirtualObjectiveEvent vEvent = eventQ.take(); |
| 151 | notifyDelegate(vEvent.networkId(), vEvent); |
| 152 | } |
| 153 | } catch (InterruptedException ex) { |
| 154 | Thread.currentThread().interrupt(); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private class VirtualObjectiveEvent extends ObjectiveEvent { |
| 160 | NetworkId networkId; |
| 161 | |
| 162 | public VirtualObjectiveEvent(NetworkId networkId, Type type, |
| 163 | Integer objective) { |
| 164 | super(type, objective); |
| 165 | this.networkId = networkId; |
| 166 | } |
| 167 | |
| 168 | NetworkId networkId() { |
| 169 | return networkId; |
| 170 | } |
| 171 | } |
| 172 | } |