blob: 534289c80f5aa7b8ee84aa0be2dc416e5145aa4b [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
Saurav Das24431192016-03-07 19:13:00 -080040import java.util.HashMap;
41import java.util.Map;
42
alshabib2a441c62015-04-13 18:39:38 -070043/**
44 * Manages the inventory of created next groups.
45 */
46@Component(immediate = true, enabled = true)
47@Service
48public 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
alshabibf6ea9e62015-04-21 17:08:26 -070059 private AtomicCounter nextIds;
60
alshabib2a441c62015-04-13 18:39:38 -070061 @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 Vavilapallie48b3cf2015-07-06 11:43:07 -070068 .register(Versioned.class)
alshabib2a441c62015-04-13 18:39:38 -070069 .build()))
70 .build();
71
alshabibf6ea9e62015-04-21 17:08:26 -070072 nextIds = storageService.atomicCounterBuilder()
73 .withName("next-objective-counter")
Madan Jampanie17d3282016-02-03 15:30:57 -080074 .build()
75 .asAtomicCounter();
alshabibf6ea9e62015-04-21 17:08:26 -070076
alshabib2a441c62015-04-13 18:39:38 -070077 log.info("Started");
78 }
79
80
81 @Deactivate
82 public void deactivate() {
83 log.info("Stopped");
84 }
85
alshabib2a441c62015-04-13 18:39:38 -070086 @Override
87 public void putNextGroup(Integer nextId, NextGroup group) {
Saurav Das423fe2b2015-12-04 10:52:59 -080088 nextGroups.put(nextId, group.data());
alshabib2a441c62015-04-13 18:39:38 -070089 notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.ADD, nextId));
90 }
91
92 @Override
93 public NextGroup getNextGroup(Integer nextId) {
94 Versioned<byte[]> versionGroup = nextGroups.get(nextId);
95 if (versionGroup != null) {
96 return new DefaultNextGroup(versionGroup.value());
97 }
98 return null;
99 }
alshabibf6ea9e62015-04-21 17:08:26 -0700100
101 @Override
Saurav Das423fe2b2015-12-04 10:52:59 -0800102 public NextGroup removeNextGroup(Integer nextId) {
103 Versioned<byte[]> versionGroup = nextGroups.remove(nextId);
104 if (versionGroup != null) {
105 notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.REMOVE, nextId));
106 return new DefaultNextGroup(versionGroup.value());
107 }
108 return null;
109 }
110
111 @Override
Saurav Das24431192016-03-07 19:13:00 -0800112 public Map<Integer, NextGroup> getAllGroups() {
113 Map<Integer, NextGroup> nextGroupMappings = new HashMap<>();
114 for (int key : nextGroups.keySet()) {
115 NextGroup nextGroup = getNextGroup(key);
116 if (nextGroup != null) {
117 nextGroupMappings.put(key, nextGroup);
118 }
119 }
120 return nextGroupMappings;
121 }
122
123 @Override
alshabibf6ea9e62015-04-21 17:08:26 -0700124 public int allocateNextId() {
125 return (int) nextIds.incrementAndGet();
126 }
alshabib2a441c62015-04-13 18:39:38 -0700127}