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