Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.cfg; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.cfg.ComponentConfigEvent; |
| 26 | import org.onosproject.cfg.ComponentConfigStore; |
| 27 | import org.onosproject.cfg.ComponentConfigStoreDelegate; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 28 | import org.onosproject.store.AbstractStore; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 31 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.LogicalClockService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_SET; |
| 38 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_UNSET; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 39 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 40 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 41 | import 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 |
| 49 | public 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 Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 60 | protected StorageService storageService; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 61 | |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected LogicalClockService clockService; |
| 64 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 65 | @Activate |
| 66 | public void activate() { |
| 67 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 68 | .register(KryoNamespaces.API); |
| 69 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 70 | properties = storageService.<String, String>eventuallyConsistentMapBuilder() |
| 71 | .withName("cfg") |
| 72 | .withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame^] | 73 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 74 | .build(); |
| 75 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 76 | properties.addListener(new InternalPropertiesListener()); |
| 77 | log.info("Started"); |
| 78 | } |
| 79 | |
| 80 | @Deactivate |
| 81 | public void deactivate() { |
Thomas Vachuska | 152f9fd | 2015-04-02 16:28:13 -0700 | [diff] [blame] | 82 | properties.destroy(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 83 | 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 | } |