blob: 94d72ec8fa80a267626244a339103ed5d5df4371 [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;
31import org.onosproject.store.service.ConsistentMap;
32import org.onosproject.store.service.Serializer;
33import org.onosproject.store.service.StorageService;
34import org.onosproject.store.service.Versioned;
35import org.slf4j.Logger;
36
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Manages the inventory of created next groups.
41 */
42@Component(immediate = true, enabled = true)
43@Service
44public 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}