Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [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.config.impl; |
| 17 | |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 25 | import org.onlab.util.KryoNamespace; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 26 | import org.onosproject.event.AbstractListenerManager; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.StorageService; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 29 | import org.onosproject.config.DynamicConfigEvent; |
| 30 | import org.onosproject.config.DynamicConfigListener; |
| 31 | import org.onosproject.config.DynamicConfigService; |
| 32 | import org.onosproject.config.DynamicConfigStore; |
| 33 | import org.onosproject.config.DynamicConfigStoreDelegate; |
| 34 | import org.onosproject.config.FailedException; |
| 35 | import org.onosproject.config.Filter; |
Gaurav Agrawal | 02dbee3 | 2017-05-11 19:04:52 +0530 | [diff] [blame] | 36 | import org.onosproject.yang.model.RpcHandler; |
| 37 | import org.onosproject.yang.model.RpcInput; |
| 38 | import org.onosproject.yang.model.RpcOutput; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 39 | import org.onosproject.yang.model.DataNode; |
| 40 | import org.onosproject.yang.model.ResourceId; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 41 | //TODO import org.onosproject.yang.model.RpcRegistry; |
| 42 | //TODO import org.onosproject.yang.model.RpcService; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 43 | |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 44 | import java.util.concurrent.CompletableFuture; |
| 45 | |
| 46 | import org.slf4j.Logger; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 50 | * Implementation of the Dynamic Config Service. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 51 | * |
| 52 | */ |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 53 | @Beta |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 54 | @Component(immediate = true) |
| 55 | @Service |
| 56 | public class DynamicConfigManager |
| 57 | extends AbstractListenerManager<DynamicConfigEvent, DynamicConfigListener> |
| 58 | implements DynamicConfigService { |
| 59 | private final Logger log = getLogger(getClass()); |
| 60 | private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate(); |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 62 | protected DynamicConfigStore store; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 63 | //TODO after 14420 is merged |
| 64 | //private ConsistentMap<RpcService, RpcHandler> handlerRegistry; |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected StorageService storageService; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | store.setDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 71 | eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 72 | log.info("Started"); |
| 73 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 74 | .register(KryoNamespaces.BASIC) |
| 75 | .register(Class.class) |
| 76 | .register(RpcHandler.class) |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 77 | .register(ResourceId.class); |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 78 | //TODO after 14420 is merged |
| 79 | /*handlerRegistry = storageService.<RpcService, RpcHandler>consistentMapBuilder() |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 80 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 81 | .withName("config-object-store") |
| 82 | .withRelaxedReadConsistency() |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 83 | .build();*/ |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 84 | log.info("Started"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | store.unsetDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 90 | eventDispatcher.removeSink(DynamicConfigEvent.class); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 91 | log.info("Stopped"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 94 | public void createNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 95 | store.addNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public DataNode readNode(ResourceId path, Filter filter) { |
| 99 | return store.readNode(path, filter).join(); |
| 100 | } |
| 101 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 102 | public void updateNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 103 | store.updateNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | public void deleteNode(ResourceId path) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 107 | store.deleteNodeRecursive(path).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 110 | public void replaceNode(ResourceId path, DataNode node) { |
| 111 | throw new FailedException("Not yet implemented"); |
| 112 | } |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 113 | |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 114 | public Boolean nodeExist(ResourceId path) { |
| 115 | return store.nodeExist(path).join(); |
| 116 | } |
| 117 | |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 118 | //TODO after RPC abstractions are merged; else will lead to build failure |
| 119 | /*public void registerHandler(RpcHandler handler, RpcCommand command) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 120 | handlerRegistry.put(command, handler); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public void unRegisterHandler(RpcHandler handler, RpcCommand command) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 124 | Versioned<RpcHandler> ret = handlerRegistry.get(command); |
| 125 | if ((ret == null) || (ret.value() == null)) { |
| 126 | throw new FailedException("No registered handler found, cannot unregister"); |
| 127 | } |
| 128 | handlerRegistry.remove(command); |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame^] | 129 | }*/ |
| 130 | |
| 131 | public CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input) { |
| 132 | //TODO after RPC abstractions are merged; else will lead to build failure |
| 133 | throw new FailedException("Not yet implemented"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 136 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 137 | * Auxiliary store delegate to receive notification about changes in the store. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 138 | */ |
| 139 | private class InternalStoreDelegate implements DynamicConfigStoreDelegate { |
| 140 | public void notify(DynamicConfigEvent event) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 141 | post(event); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |