blob: 75a9b8995f52f77dbc73b19c43469a2d30dcfac4 [file] [log] [blame]
alshabib2a441c62015-04-13 18:39:38 -07001/*
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 */
16package org.onosproject.store.flowobjective.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24import org.onlab.util.KryoNamespace;
25import org.onosproject.net.behaviour.DefaultNextGroup;
26import org.onosproject.net.behaviour.NextGroup;
27import org.onosproject.net.flowobjective.FlowObjectiveStore;
28import org.onosproject.net.flowobjective.FlowObjectiveStoreDelegate;
29import org.onosproject.net.flowobjective.ObjectiveEvent;
30import org.onosproject.store.AbstractStore;
alshabibf6ea9e62015-04-21 17:08:26 -070031import org.onosproject.store.service.AtomicCounter;
alshabib2a441c62015-04-13 18:39:38 -070032import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.Serializer;
34import org.onosproject.store.service.StorageService;
35import org.onosproject.store.service.Versioned;
36import org.slf4j.Logger;
37
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Manages the inventory of created next groups.
42 */
43@Component(immediate = true, enabled = true)
44@Service
45public 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
alshabibf6ea9e62015-04-21 17:08:26 -070056 private AtomicCounter nextIds;
57
alshabib2a441c62015-04-13 18:39:38 -070058 @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 Vavilapallie48b3cf2015-07-06 11:43:07 -070065 .register(Versioned.class)
alshabib2a441c62015-04-13 18:39:38 -070066 .build()))
67 .build();
68
alshabibf6ea9e62015-04-21 17:08:26 -070069 nextIds = storageService.atomicCounterBuilder()
70 .withName("next-objective-counter")
Madan Jampanie17d3282016-02-03 15:30:57 -080071 .build()
72 .asAtomicCounter();
alshabibf6ea9e62015-04-21 17:08:26 -070073
alshabib2a441c62015-04-13 18:39:38 -070074 log.info("Started");
75 }
76
77
78 @Deactivate
79 public void deactivate() {
80 log.info("Stopped");
81 }
82
alshabib2a441c62015-04-13 18:39:38 -070083 @Override
84 public void putNextGroup(Integer nextId, NextGroup group) {
Saurav Das423fe2b2015-12-04 10:52:59 -080085 nextGroups.put(nextId, group.data());
alshabib2a441c62015-04-13 18:39:38 -070086 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 }
alshabibf6ea9e62015-04-21 17:08:26 -070097
98 @Override
Saurav Das423fe2b2015-12-04 10:52:59 -080099 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
alshabibf6ea9e62015-04-21 17:08:26 -0700109 public int allocateNextId() {
110 return (int) nextIds.incrementAndGet();
111 }
alshabib2a441c62015-04-13 18:39:38 -0700112}