Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 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.onosproject.cfg.ComponentConfigEvent; |
| 25 | import org.onosproject.cfg.ComponentConfigStore; |
| 26 | import org.onosproject.cfg.ComponentConfigStoreDelegate; |
| 27 | import org.onosproject.store.AbstractStore; |
| 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.ConsistentMap; |
| 30 | import org.onosproject.store.service.MapEvent; |
| 31 | import org.onosproject.store.service.MapEventListener; |
| 32 | import org.onosproject.store.service.Serializer; |
| 33 | import org.onosproject.store.service.StorageService; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_SET; |
| 37 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_UNSET; |
| 38 | import static org.onosproject.store.service.MapEvent.Type.INSERT; |
| 39 | import static org.onosproject.store.service.MapEvent.Type.REMOVE; |
| 40 | import static org.onosproject.store.service.MapEvent.Type.UPDATE; |
| 41 | import 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 | */ |
| 47 | @Component(immediate = true, enabled = true) |
| 48 | @Service |
| 49 | public 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(); |
| 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 | } |