blob: 8dbc63458baf0e773ced90dcb16c252a9021e3ea [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)
65 .build()))
66 .build();
67
alshabibf6ea9e62015-04-21 17:08:26 -070068 nextIds = storageService.atomicCounterBuilder()
69 .withName("next-objective-counter")
70 .build();
71
alshabib2a441c62015-04-13 18:39:38 -070072 log.info("Started");
73 }
74
75
76 @Deactivate
77 public void deactivate() {
78 log.info("Stopped");
79 }
80
81
82 @Override
83 public void putNextGroup(Integer nextId, NextGroup group) {
84 nextGroups.putIfAbsent(nextId, group.data());
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 }
alshabibf6ea9e62015-04-21 17:08:26 -070096
97 @Override
98 public int allocateNextId() {
99 return (int) nextIds.incrementAndGet();
100 }
alshabib2a441c62015-04-13 18:39:38 -0700101}