blob: 94e17b105ce0e5357d2eea46cde4b57590bcb979 [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() {
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 EventuallyConsistentMapListener<String, String> {
98
99 @Override
100 public void event(EventuallyConsistentMapEvent<String, String> event) {
101 String[] keys = event.key().split(SEP);
102 String value = event.value();
103 if (event.type() == PUT) {
104 delegate.notify(new ComponentConfigEvent(PROPERTY_SET, keys[0], keys[1], value));
105 } else if (event.type() == REMOVE) {
106 delegate.notify(new ComponentConfigEvent(PROPERTY_UNSET, keys[0], keys[1], null));
107 }
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
116}