blob: bf992643e3d892278e2b85831a6ad8e354683e72 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -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.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.onlab.util.KryoNamespace;
25import org.onosproject.cfg.ComponentConfigEvent;
26import org.onosproject.cfg.ComponentConfigStore;
27import org.onosproject.cfg.ComponentConfigStoreDelegate;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070028import org.onosproject.store.AbstractStore;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070029import org.onosproject.store.serializers.KryoNamespaces;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070030import org.onosproject.store.service.EventuallyConsistentMap;
31import org.onosproject.store.service.EventuallyConsistentMapEvent;
32import org.onosproject.store.service.EventuallyConsistentMapListener;
Madan Jampani3e033bd2015-04-08 13:03:49 -070033import org.onosproject.store.service.LogicalClockService;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070034import org.onosproject.store.service.StorageService;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070035import org.slf4j.Logger;
36
37import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_SET;
38import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_UNSET;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070039import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
40import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070041import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Manages inventory of component configurations in a distributed data store
45 * that uses optimistic replication and gossip based anti-entropy techniques.
46 */
47@Component(immediate = true)
48@Service
49public class GossipComponentConfigStore
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 EventuallyConsistentMap<String, String> properties;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart6ec029a2015-03-24 17:12:35 -070060 protected StorageService storageService;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070061
Madan Jampani3e033bd2015-04-08 13:03:49 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected LogicalClockService clockService;
64
Thomas Vachuska6d697f12015-03-08 20:59:50 -070065 @Activate
66 public void activate() {
67 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
68 .register(KryoNamespaces.API);
69
Jonathan Hart6ec029a2015-03-24 17:12:35 -070070 properties = storageService.<String, String>eventuallyConsistentMapBuilder()
71 .withName("cfg")
72 .withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -070073 .withTimestampProvider((k, v) -> clockService.getTimestamp())
Jonathan Hart6ec029a2015-03-24 17:12:35 -070074 .build();
75
Thomas Vachuska6d697f12015-03-08 20:59:50 -070076 properties.addListener(new InternalPropertiesListener());
77 log.info("Started");
78 }
79
80 @Deactivate
81 public void deactivate() {
Thomas Vachuska152f9fd2015-04-02 16:28:13 -070082 properties.destroy();
Thomas Vachuska6d697f12015-03-08 20:59:50 -070083 log.info("Stopped");
84 }
85
86 @Override
87 public void setProperty(String componentName, String name, String value) {
88 properties.put(key(componentName, name), value);
89
90 }
91
92 @Override
93 public void unsetProperty(String componentName, String name) {
94 properties.remove(key(componentName, name));
95 }
96
97 /**
98 * Listener to component configuration properties distributed map changes.
99 */
100 private final class InternalPropertiesListener
101 implements EventuallyConsistentMapListener<String, String> {
102
103 @Override
104 public void event(EventuallyConsistentMapEvent<String, String> event) {
105 String[] keys = event.key().split(SEP);
106 String value = event.value();
107 if (event.type() == PUT) {
108 delegate.notify(new ComponentConfigEvent(PROPERTY_SET, keys[0], keys[1], value));
109 } else if (event.type() == REMOVE) {
110 delegate.notify(new ComponentConfigEvent(PROPERTY_UNSET, keys[0], keys[1], null));
111 }
112 }
113 }
114
115 // Generates a key from component name and property name.
116 private String key(String componentName, String name) {
117 return componentName + SEP + name;
118 }
119
120}