alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [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 | package org.onosproject.store.flowobjective.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.net.behaviour.DefaultNextGroup; |
| 26 | import org.onosproject.net.behaviour.NextGroup; |
| 27 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 28 | import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate; |
| 29 | import org.onosproject.net.flowobjective.ObjectiveEvent; |
| 30 | import org.onosproject.store.AbstractStore; |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.AtomicCounter; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.ConsistentMap; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.MapEvent; |
| 34 | import org.onosproject.store.service.MapEventListener; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 35 | import org.onosproject.store.service.Serializer; |
| 36 | import org.onosproject.store.service.StorageService; |
| 37 | import org.onosproject.store.service.Versioned; |
| 38 | import org.slf4j.Logger; |
| 39 | |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 40 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 43 | import java.util.HashMap; |
| 44 | import java.util.Map; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 45 | import java.util.concurrent.BlockingQueue; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | import java.util.concurrent.Executors; |
| 48 | import java.util.concurrent.LinkedBlockingQueue; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 49 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Manages the inventory of created next groups. |
| 52 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 53 | @Component(immediate = true) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 54 | @Service |
| 55 | public class DistributedFlowObjectiveStore |
| 56 | extends AbstractStore<ObjectiveEvent, FlowObjectiveStoreDelegate> |
| 57 | implements FlowObjectiveStore { |
| 58 | |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
| 61 | private ConsistentMap<Integer, byte[]> nextGroups; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected StorageService storageService; |
| 65 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 66 | private AtomicCounter nextIds; |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 67 | private MapEventListener<Integer, byte[]> mapListener = new NextGroupListener(); |
| 68 | // event queue to separate map-listener threads from event-handler threads (tpool) |
| 69 | private BlockingQueue<ObjectiveEvent> eventQ; |
| 70 | private ExecutorService tpool; |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 71 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 72 | @Activate |
| 73 | public void activate() { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 74 | tpool = Executors.newFixedThreadPool(4, groupedThreads("onos/flobj-notifier", "%d", log)); |
| 75 | eventQ = new LinkedBlockingQueue<ObjectiveEvent>(); |
| 76 | tpool.execute(new FlowObjectiveNotifier()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 77 | nextGroups = storageService.<Integer, byte[]>consistentMapBuilder() |
| 78 | .withName("flowobjective-groups") |
| 79 | .withSerializer(Serializer.using( |
| 80 | new KryoNamespace.Builder() |
| 81 | .register(byte[].class) |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 82 | .register(Versioned.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 83 | .build("DistributedFlowObjectiveStore"))) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 84 | .build(); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 85 | nextGroups.addListener(mapListener); |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 86 | nextIds = storageService.getAtomicCounter("next-objective-counter"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 93 | nextGroups.removeListener(mapListener); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 94 | tpool.shutdown(); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public void putNextGroup(Integer nextId, NextGroup group) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 100 | nextGroups.put(nextId, group.data()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public NextGroup getNextGroup(Integer nextId) { |
| 105 | Versioned<byte[]> versionGroup = nextGroups.get(nextId); |
| 106 | if (versionGroup != null) { |
| 107 | return new DefaultNextGroup(versionGroup.value()); |
| 108 | } |
| 109 | return null; |
| 110 | } |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 111 | |
| 112 | @Override |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 113 | public NextGroup removeNextGroup(Integer nextId) { |
| 114 | Versioned<byte[]> versionGroup = nextGroups.remove(nextId); |
| 115 | if (versionGroup != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 116 | return new DefaultNextGroup(versionGroup.value()); |
| 117 | } |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | @Override |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 122 | public Map<Integer, NextGroup> getAllGroups() { |
| 123 | Map<Integer, NextGroup> nextGroupMappings = new HashMap<>(); |
| 124 | for (int key : nextGroups.keySet()) { |
| 125 | NextGroup nextGroup = getNextGroup(key); |
| 126 | if (nextGroup != null) { |
| 127 | nextGroupMappings.put(key, nextGroup); |
| 128 | } |
| 129 | } |
| 130 | return nextGroupMappings; |
| 131 | } |
| 132 | |
| 133 | @Override |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 134 | public int allocateNextId() { |
| 135 | return (int) nextIds.incrementAndGet(); |
| 136 | } |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 137 | |
| 138 | private class FlowObjectiveNotifier implements Runnable { |
| 139 | @Override |
| 140 | public void run() { |
| 141 | try { |
| 142 | while (!Thread.currentThread().isInterrupted()) { |
| 143 | notifyDelegate(eventQ.take()); |
| 144 | } |
| 145 | } catch (InterruptedException ex) { |
| 146 | Thread.currentThread().interrupt(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private class NextGroupListener implements MapEventListener<Integer, byte[]> { |
| 152 | @Override |
| 153 | public void event(MapEvent<Integer, byte[]> event) { |
| 154 | switch (event.type()) { |
| 155 | case INSERT: |
| 156 | eventQ.add(new ObjectiveEvent(ObjectiveEvent.Type.ADD, event.key())); |
| 157 | break; |
| 158 | case REMOVE: |
| 159 | eventQ.add(new ObjectiveEvent(ObjectiveEvent.Type.REMOVE, event.key())); |
| 160 | break; |
| 161 | case UPDATE: |
| 162 | // TODO Introduce UPDATE ObjectiveEvent when the map is being updated |
| 163 | break; |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 170 | } |