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