blob: 12fad4fbb73844b656d14180dcceeb6615ff05f4 [file] [log] [blame]
Madan Jampania4be1842016-05-24 15:56:02 -07001/*
2 * Copyright 2016-present 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.cfg;
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.onosproject.cfg.ComponentConfigEvent;
25import org.onosproject.cfg.ComponentConfigStore;
26import org.onosproject.cfg.ComponentConfigStoreDelegate;
27import org.onosproject.store.AbstractStore;
28import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.store.service.ConsistentMap;
30import org.onosproject.store.service.MapEvent;
31import org.onosproject.store.service.MapEventListener;
32import org.onosproject.store.service.Serializer;
33import org.onosproject.store.service.StorageService;
34import org.slf4j.Logger;
35
36import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_SET;
37import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_UNSET;
38import static org.onosproject.store.service.MapEvent.Type.INSERT;
39import static org.onosproject.store.service.MapEvent.Type.REMOVE;
40import static org.onosproject.store.service.MapEvent.Type.UPDATE;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Manages inventory of component configurations in a distributed data store
45 * that provides strong sequential consistency guarantees.
46 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070047@Component(immediate = true)
Madan Jampania4be1842016-05-24 15:56:02 -070048@Service
49public class DistributedComponentConfigStore
50 extends AbstractStore<ComponentConfigEvent, ComponentConfigStoreDelegate>
51 implements ComponentConfigStore {
52
53 private static final String SEP = "#";
54
55 private final Logger log = getLogger(getClass());
56
57 private ConsistentMap<String, String> properties;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
61
62 InternalPropertiesListener propertiesListener = new InternalPropertiesListener();
63
64 @Activate
65 public void activate() {
66 properties = storageService.<String, String>consistentMapBuilder()
67 .withName("onos-component-cfg")
68 .withSerializer(Serializer.using(KryoNamespaces.API))
69 .withRelaxedReadConsistency()
70 .build();
71
72 properties.addListener(propertiesListener);
73 log.info("Started");
74 }
75
76 @Deactivate
77 public void deactivate() {
78 properties.removeListener(propertiesListener);
79 log.info("Stopped");
80 }
81
82 @Override
83 public void setProperty(String componentName, String name, String value) {
84 properties.put(key(componentName, name), value);
85
86 }
87
88 @Override
89 public void unsetProperty(String componentName, String name) {
90 properties.remove(key(componentName, name));
91 }
92
93 /**
94 * Listener to component configuration properties distributed map changes.
95 */
96 private final class InternalPropertiesListener
97 implements MapEventListener<String, String> {
98
99 @Override
100 public void event(MapEvent<String, String> event) {
101 String[] keys = event.key().split(SEP);
102 if (event.type() == INSERT || event.type() == UPDATE) {
103 String value = event.newValue().value();
Thomas Vachuskad4955ae2016-08-23 14:56:37 -0700104 notifyDelegate(new ComponentConfigEvent(PROPERTY_SET, keys[0], keys[1], value));
Madan Jampania4be1842016-05-24 15:56:02 -0700105 } else if (event.type() == REMOVE) {
Thomas Vachuskad4955ae2016-08-23 14:56:37 -0700106 notifyDelegate(new ComponentConfigEvent(PROPERTY_UNSET, keys[0], keys[1], null));
Madan Jampania4be1842016-05-24 15:56:02 -0700107 }
108 }
109 }
110
111 // Generates a key from component name and property name.
112 private String key(String componentName, String name) {
113 return componentName + SEP + name;
114 }
115}