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; |
| 31 | import org.onosproject.store.service.ConsistentMap; |
| 32 | import org.onosproject.store.service.Serializer; |
| 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.onosproject.store.service.Versioned; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Manages the inventory of created next groups. |
| 41 | */ |
| 42 | @Component(immediate = true, enabled = true) |
| 43 | @Service |
| 44 | public class DistributedFlowObjectiveStore |
| 45 | extends AbstractStore<ObjectiveEvent, FlowObjectiveStoreDelegate> |
| 46 | implements FlowObjectiveStore { |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private ConsistentMap<Integer, byte[]> nextGroups; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected StorageService storageService; |
| 54 | |
| 55 | @Activate |
| 56 | public void activate() { |
| 57 | nextGroups = storageService.<Integer, byte[]>consistentMapBuilder() |
| 58 | .withName("flowobjective-groups") |
| 59 | .withSerializer(Serializer.using( |
| 60 | new KryoNamespace.Builder() |
| 61 | .register(byte[].class) |
| 62 | .build())) |
| 63 | .build(); |
| 64 | |
| 65 | log.info("Started"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | log.info("Stopped"); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | @Override |
| 76 | public void putNextGroup(Integer nextId, NextGroup group) { |
| 77 | nextGroups.putIfAbsent(nextId, group.data()); |
| 78 | notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.ADD, nextId)); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public NextGroup getNextGroup(Integer nextId) { |
| 83 | Versioned<byte[]> versionGroup = nextGroups.get(nextId); |
| 84 | if (versionGroup != null) { |
| 85 | return new DefaultNextGroup(versionGroup.value()); |
| 86 | } |
| 87 | return null; |
| 88 | } |
| 89 | } |