blob: 13690d4f9a8c3594a862da2c1921bb50a9fed9be [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.impl.WallclockClockManager;
30import org.onosproject.store.serializers.KryoNamespaces;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070031import org.onosproject.store.service.EventuallyConsistentMap;
32import org.onosproject.store.service.EventuallyConsistentMapEvent;
33import org.onosproject.store.service.EventuallyConsistentMapListener;
34import 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
62 @Activate
63 public void activate() {
64 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
65 .register(KryoNamespaces.API);
66
Jonathan Hart6ec029a2015-03-24 17:12:35 -070067 properties = storageService.<String, String>eventuallyConsistentMapBuilder()
68 .withName("cfg")
69 .withSerializer(serializer)
70 .withClockService(new WallclockClockManager<>())
71 .build();
72
Thomas Vachuska6d697f12015-03-08 20:59:50 -070073 properties.addListener(new InternalPropertiesListener());
74 log.info("Started");
75 }
76
77 @Deactivate
78 public void deactivate() {
Thomas Vachuska152f9fd2015-04-02 16:28:13 -070079 properties.destroy();
Thomas Vachuska6d697f12015-03-08 20:59:50 -070080 log.info("Stopped");
81 }
82
83 @Override
84 public void setProperty(String componentName, String name, String value) {
85 properties.put(key(componentName, name), value);
86
87 }
88
89 @Override
90 public void unsetProperty(String componentName, String name) {
91 properties.remove(key(componentName, name));
92 }
93
94 /**
95 * Listener to component configuration properties distributed map changes.
96 */
97 private final class InternalPropertiesListener
98 implements EventuallyConsistentMapListener<String, String> {
99
100 @Override
101 public void event(EventuallyConsistentMapEvent<String, String> event) {
102 String[] keys = event.key().split(SEP);
103 String value = event.value();
104 if (event.type() == PUT) {
105 delegate.notify(new ComponentConfigEvent(PROPERTY_SET, keys[0], keys[1], value));
106 } else if (event.type() == REMOVE) {
107 delegate.notify(new ComponentConfigEvent(PROPERTY_UNSET, keys[0], keys[1], null));
108 }
109 }
110 }
111
112 // Generates a key from component name and property name.
113 private String key(String componentName, String name) {
114 return componentName + SEP + name;
115 }
116
117}