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; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.Versioned; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 40 | import java.util.HashMap; |
| 41 | import java.util.Map; |
| 42 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Manages the inventory of created next groups. |
| 45 | */ |
| 46 | @Component(immediate = true, enabled = true) |
| 47 | @Service |
| 48 | public class DistributedFlowObjectiveStore |
| 49 | extends AbstractStore<ObjectiveEvent, FlowObjectiveStoreDelegate> |
| 50 | implements FlowObjectiveStore { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private ConsistentMap<Integer, byte[]> nextGroups; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected StorageService storageService; |
| 58 | |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 59 | private AtomicCounter nextIds; |
| 60 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 61 | @Activate |
| 62 | public void activate() { |
| 63 | nextGroups = storageService.<Integer, byte[]>consistentMapBuilder() |
| 64 | .withName("flowobjective-groups") |
| 65 | .withSerializer(Serializer.using( |
| 66 | new KryoNamespace.Builder() |
| 67 | .register(byte[].class) |
Srikanth Vavilapalli | e48b3cf | 2015-07-06 11:43:07 -0700 | [diff] [blame] | 68 | .register(Versioned.class) |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 69 | .build())) |
| 70 | .build(); |
| 71 | |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 72 | nextIds = storageService.getAtomicCounter("next-objective-counter"); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | @Deactivate |
| 78 | public void deactivate() { |
| 79 | log.info("Stopped"); |
| 80 | } |
| 81 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public void putNextGroup(Integer nextId, NextGroup group) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 84 | nextGroups.put(nextId, group.data()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 85 | notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.ADD, nextId)); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public NextGroup getNextGroup(Integer nextId) { |
| 90 | Versioned<byte[]> versionGroup = nextGroups.get(nextId); |
| 91 | if (versionGroup != null) { |
| 92 | return new DefaultNextGroup(versionGroup.value()); |
| 93 | } |
| 94 | return null; |
| 95 | } |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 96 | |
| 97 | @Override |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 98 | public NextGroup removeNextGroup(Integer nextId) { |
| 99 | Versioned<byte[]> versionGroup = nextGroups.remove(nextId); |
| 100 | if (versionGroup != null) { |
| 101 | notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.REMOVE, nextId)); |
| 102 | return new DefaultNextGroup(versionGroup.value()); |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 108 | public Map<Integer, NextGroup> getAllGroups() { |
| 109 | Map<Integer, NextGroup> nextGroupMappings = new HashMap<>(); |
| 110 | for (int key : nextGroups.keySet()) { |
| 111 | NextGroup nextGroup = getNextGroup(key); |
| 112 | if (nextGroup != null) { |
| 113 | nextGroupMappings.put(key, nextGroup); |
| 114 | } |
| 115 | } |
| 116 | return nextGroupMappings; |
| 117 | } |
| 118 | |
| 119 | @Override |
alshabib | f6ea9e6 | 2015-04-21 17:08:26 -0700 | [diff] [blame] | 120 | public int allocateNextId() { |
| 121 | return (int) nextIds.incrementAndGet(); |
| 122 | } |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 123 | } |