Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 19 | import org.onosproject.cfg.ComponentConfigEvent; |
| 20 | import org.onosproject.cfg.ComponentConfigStore; |
| 21 | import org.onosproject.cfg.ComponentConfigStoreDelegate; |
| 22 | import org.onosproject.store.AbstractStore; |
| 23 | import org.onosproject.store.serializers.KryoNamespaces; |
| 24 | import org.onosproject.store.service.ConsistentMap; |
| 25 | import org.onosproject.store.service.MapEvent; |
| 26 | import org.onosproject.store.service.MapEventListener; |
| 27 | import org.onosproject.store.service.Serializer; |
| 28 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 29 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 37 | import java.util.Objects; |
| 38 | import java.util.Set; |
| 39 | |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 40 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_SET; |
| 41 | import static org.onosproject.cfg.ComponentConfigEvent.Type.PROPERTY_UNSET; |
| 42 | import static org.onosproject.store.service.MapEvent.Type.INSERT; |
| 43 | import static org.onosproject.store.service.MapEvent.Type.REMOVE; |
| 44 | import static org.onosproject.store.service.MapEvent.Type.UPDATE; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Manages inventory of component configurations in a distributed data store |
| 49 | * that provides strong sequential consistency guarantees. |
| 50 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 51 | @Component(immediate = true, service = ComponentConfigStore.class) |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 52 | public class DistributedComponentConfigStore |
| 53 | extends AbstractStore<ComponentConfigEvent, ComponentConfigStoreDelegate> |
| 54 | implements ComponentConfigStore { |
| 55 | |
| 56 | private static final String SEP = "#"; |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | private ConsistentMap<String, String> properties; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 63 | protected StorageService storageService; |
| 64 | |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 65 | private InternalPropertiesListener propertiesListener = new InternalPropertiesListener(); |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | properties = storageService.<String, String>consistentMapBuilder() |
| 70 | .withName("onos-component-cfg") |
| 71 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 72 | .withRelaxedReadConsistency() |
| 73 | .build(); |
| 74 | |
| 75 | properties.addListener(propertiesListener); |
| 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
| 81 | properties.removeListener(propertiesListener); |
| 82 | log.info("Stopped"); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void setProperty(String componentName, String name, String value) { |
| 87 | properties.put(key(componentName, name), value); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void unsetProperty(String componentName, String name) { |
| 93 | properties.remove(key(componentName, name)); |
| 94 | } |
| 95 | |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public Set<String> getProperties(String componentName) { |
| 98 | ImmutableSet.Builder<String> names = ImmutableSet.builder(); |
| 99 | properties.keySet().stream() |
| 100 | .filter((String k) -> Objects.equals(componentName, k.substring(0, k.indexOf(SEP)))) |
| 101 | .map((String k) -> k.substring(k.indexOf(SEP) + 1)) |
| 102 | .forEach(names::add); |
| 103 | return names.build(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public String getProperty(String componentName, String name) { |
| 108 | Versioned<String> v = properties.get(key(componentName, name)); |
| 109 | return v != null ? v.value() : null; |
| 110 | } |
| 111 | |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Listener to component configuration properties distributed map changes. |
| 114 | */ |
| 115 | private final class InternalPropertiesListener |
| 116 | implements MapEventListener<String, String> { |
| 117 | |
| 118 | @Override |
| 119 | public void event(MapEvent<String, String> event) { |
| 120 | String[] keys = event.key().split(SEP); |
| 121 | if (event.type() == INSERT || event.type() == UPDATE) { |
| 122 | String value = event.newValue().value(); |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 123 | notifyDelegate(new ComponentConfigEvent(PROPERTY_SET, keys[0], keys[1], value)); |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 124 | } else if (event.type() == REMOVE) { |
Thomas Vachuska | d4955ae | 2016-08-23 14:56:37 -0700 | [diff] [blame] | 125 | notifyDelegate(new ComponentConfigEvent(PROPERTY_UNSET, keys[0], keys[1], null)); |
Madan Jampani | a4be184 | 2016-05-24 15:56:02 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Generates a key from component name and property name. |
| 131 | private String key(String componentName, String name) { |
| 132 | return componentName + SEP + name; |
| 133 | } |
| 134 | } |