blob: 4ef268ee714469536b4c83f995311a0bbb57e988 [file] [log] [blame]
yoonseon86bebed2017-02-03 15:23:57 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
yoonseon86bebed2017-02-03 15:23:57 -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
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.incubator.net.virtual.store.impl;
yoonseon86bebed2017-02-03 15:23:57 -080018
19import com.google.common.collect.Maps;
yoonseon86bebed2017-02-03 15:23:57 -080020import org.onosproject.incubator.net.virtual.NetworkId;
21import org.onosproject.incubator.net.virtual.VirtualNetworkFlowObjectiveStore;
22import org.onosproject.net.behaviour.DefaultNextGroup;
23import org.onosproject.net.behaviour.NextGroup;
24import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
25import org.onosproject.net.flowobjective.ObjectiveEvent;
26import org.onosproject.store.service.AtomicCounter;
27import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Reference;
31import org.osgi.service.component.annotations.ReferenceCardinality;
yoonseon86bebed2017-02-03 15:23:57 -080032import org.slf4j.Logger;
33
34import java.util.HashMap;
35import java.util.Map;
36import java.util.concurrent.BlockingQueue;
37import java.util.concurrent.ConcurrentMap;
38import java.util.concurrent.ExecutorService;
39import java.util.concurrent.Executors;
40import java.util.concurrent.LinkedBlockingQueue;
41
42import static org.onlab.util.Tools.groupedThreads;
43import 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 Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = VirtualNetworkFlowObjectiveStore.class)
yoonseon86bebed2017-02-03 15:23:57 -080050public 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 Milkeyd84f89b2018-08-17 14:54:17 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY)
yoonseon86bebed2017-02-03 15:23:57 -080065 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 Chiucdc4b8c2017-03-30 00:34:39 -040073 initNextGroupsMap();
yoonseon86bebed2017-02-03 15:23:57 -080074
75 nextIds = storageService.getAtomicCounter("next-objective-counter");
76 log.info("Started");
77 }
78
Claudine Chiucdc4b8c2017-03-30 00:34:39 -040079 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) {
yoonseon86bebed2017-02-03 15:23:57 -080092 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 Chiucdc4b8c2017-03-30 00:34:39 -0400100 updateNextGroupsMap(networkId, nextGroups);
yoonseon86bebed2017-02-03 15:23:57 -0800101
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 Chiucdc4b8c2017-03-30 00:34:39 -0400108 byte[] groupData = nextGroups.get(nextId);
109 if (groupData != null) {
110 return new DefaultNextGroup(groupData);
111 }
112 return null;
yoonseon86bebed2017-02-03 15:23:57 -0800113 }
114
115 @Override
116 public NextGroup removeNextGroup(NetworkId networkId, Integer nextId) {
117 ConcurrentMap<Integer, byte[]> nextGroups = getNextGroups(networkId);
Claudine Chiucdc4b8c2017-03-30 00:34:39 -0400118 byte[] nextGroup = nextGroups.remove(nextId);
119 updateNextGroupsMap(networkId, nextGroups);
120
yoonseon86bebed2017-02-03 15:23:57 -0800121 eventQ.add(new VirtualObjectiveEvent(networkId, ObjectiveEvent.Type.REMOVE, nextId));
Claudine Chiucdc4b8c2017-03-30 00:34:39 -0400122
123 return new DefaultNextGroup(nextGroup);
yoonseon86bebed2017-02-03 15:23:57 -0800124 }
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}