blob: 5794f456781800cba31c82f39cc5dd257f87d7b0 [file] [log] [blame]
alshabib2a441c62015-04-13 18:39:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabib2a441c62015-04-13 18:39:38 -07003 *
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
Madan Jampanid5714e02016-04-19 14:15:20 -070072 nextIds = storageService.getAtomicCounter("next-objective-counter");
alshabib2a441c62015-04-13 18:39:38 -070073 log.info("Started");
74 }
75
76
77 @Deactivate
78 public void deactivate() {
79 log.info("Stopped");
80 }
81
alshabib2a441c62015-04-13 18:39:38 -070082 @Override
83 public void putNextGroup(Integer nextId, NextGroup group) {
Saurav Das423fe2b2015-12-04 10:52:59 -080084 nextGroups.put(nextId, group.data());
alshabib2a441c62015-04-13 18:39:38 -070085 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
Saurav Das423fe2b2015-12-04 10:52:59 -080098 public NextGroup removeNextGroup(Integer nextId) {
99 Versioned<byte[]> versionGroup = nextGroups.remove(nextId);
100 if (versionGroup != null) {
101 notifyDelegate(new ObjectiveEvent(ObjectiveEvent.Type.REMOVE, nextId));
102 return new DefaultNextGroup(versionGroup.value());
103 }
104 return null;
105 }
106
107 @Override
Saurav Das24431192016-03-07 19:13:00 -0800108 public Map<Integer, NextGroup> getAllGroups() {
109 Map<Integer, NextGroup> nextGroupMappings = new HashMap<>();
110 for (int key : nextGroups.keySet()) {
111 NextGroup nextGroup = getNextGroup(key);
112 if (nextGroup != null) {
113 nextGroupMappings.put(key, nextGroup);
114 }
115 }
116 return nextGroupMappings;
117 }
118
119 @Override
alshabibf6ea9e62015-04-21 17:08:26 -0700120 public int allocateNextId() {
121 return (int) nextIds.incrementAndGet();
122 }
alshabib2a441c62015-04-13 18:39:38 -0700123}