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 | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 26 | import org.onosproject.config.DynamicConfigEvent; |
| 27 | import org.onosproject.config.DynamicConfigListener; |
| 28 | import org.onosproject.config.DynamicConfigService; |
| 29 | import org.onosproject.config.DynamicConfigStore; |
| 30 | import org.onosproject.config.DynamicConfigStoreDelegate; |
| 31 | import org.onosproject.config.FailedException; |
| 32 | import org.onosproject.config.Filter; |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 33 | import org.onosproject.store.serializers.KryoNamespaces; |
| 34 | import org.onosproject.store.service.ConsistentMap; |
| 35 | import org.onosproject.store.service.StorageService; |
| 36 | import org.onosproject.store.service.Serializer; |
| 37 | import org.onosproject.store.service.Versioned; |
Gaurav Agrawal | 02dbee3 | 2017-05-11 19:04:52 +0530 | [diff] [blame] | 38 | import org.onosproject.yang.model.RpcCaller; |
| 39 | import org.onosproject.yang.model.RpcCommand; |
| 40 | import org.onosproject.yang.model.RpcHandler; |
| 41 | import org.onosproject.yang.model.RpcInput; |
| 42 | import org.onosproject.yang.model.RpcOutput; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 43 | import org.onosproject.yang.model.DataNode; |
| 44 | import org.onosproject.yang.model.ResourceId; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 45 | import org.onosproject.event.AbstractListenerManager; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Demo application to use the DynamicConfig Service and DynamicConfigStore. |
| 52 | * |
| 53 | */ |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 54 | @Beta |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 55 | @Component(immediate = true) |
| 56 | @Service |
| 57 | public class DynamicConfigManager |
| 58 | extends AbstractListenerManager<DynamicConfigEvent, DynamicConfigListener> |
| 59 | implements DynamicConfigService { |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate(); |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 63 | protected DynamicConfigStore store; |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 64 | private ConsistentMap<RpcCommand, RpcHandler> handlerRegistry; |
| 65 | private ConsistentMap<Integer, RpcCaller> callerRegistry; |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected StorageService storageService; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | store.setDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 72 | eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 73 | log.info("Started"); |
| 74 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 75 | .register(KryoNamespaces.BASIC) |
| 76 | .register(Class.class) |
| 77 | .register(RpcHandler.class) |
| 78 | .register(RpcCaller.class) |
| 79 | .register(RpcCommand.class) |
| 80 | .register(ResourceId.class); |
| 81 | callerRegistry = storageService.<Integer, RpcCaller>consistentMapBuilder() |
| 82 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 83 | .withName("config-object-store") |
| 84 | .withRelaxedReadConsistency() |
| 85 | .build(); |
| 86 | handlerRegistry = storageService.<RpcCommand, RpcHandler>consistentMapBuilder() |
| 87 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 88 | .withName("config-object-store") |
| 89 | .withRelaxedReadConsistency() |
| 90 | .build(); |
| 91 | log.info("Started"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | public void deactivate() { |
| 96 | store.unsetDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 97 | eventDispatcher.removeSink(DynamicConfigEvent.class); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 98 | log.info("Stopped"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 101 | public void createNodeRecursive(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 102 | store.addNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public DataNode readNode(ResourceId path, Filter filter) { |
| 106 | return store.readNode(path, filter).join(); |
| 107 | } |
| 108 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 109 | public void updateNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 110 | store.updateNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public void deleteNode(ResourceId path) { |
| 114 | throw new FailedException("Not yet implemented"); |
| 115 | } |
| 116 | |
| 117 | public void deleteNodeRecursive(ResourceId path) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 118 | store.deleteNodeRecursive(path).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 121 | public void replaceNode(ResourceId path, DataNode node) { |
| 122 | throw new FailedException("Not yet implemented"); |
| 123 | } |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 124 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 125 | public Integer getNumberOfChildren(ResourceId path, Filter filter) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 126 | throw new FailedException("Not yet implemented"); |
| 127 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 128 | |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 129 | public Boolean nodeExist(ResourceId path) { |
| 130 | return store.nodeExist(path).join(); |
| 131 | } |
| 132 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 133 | public void registerHandler(RpcHandler handler, RpcCommand command) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 134 | handlerRegistry.put(command, handler); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void unRegisterHandler(RpcHandler handler, RpcCommand command) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 138 | Versioned<RpcHandler> ret = handlerRegistry.get(command); |
| 139 | if ((ret == null) || (ret.value() == null)) { |
| 140 | throw new FailedException("No registered handler found, cannot unregister"); |
| 141 | } |
| 142 | handlerRegistry.remove(command); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | public void invokeRpc(RpcCaller caller, Integer msgId, RpcCommand command, RpcInput input) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 146 | callerRegistry.put(msgId, caller); |
| 147 | Versioned<RpcHandler> hndlr = handlerRegistry.get(command); |
| 148 | if ((hndlr == null) || (hndlr.value() == null)) { |
| 149 | throw new FailedException("No registered handler found, cannot invoke"); |
| 150 | } |
| 151 | hndlr.value().executeRpc(msgId, command, input); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public void rpcResponse(Integer msgId, RpcOutput output) { |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame^] | 155 | Versioned<RpcCaller> caller = callerRegistry.get(msgId); |
| 156 | if (caller.value() == null) { |
| 157 | throw new FailedException("No registered receiver found, cannot relay response"); |
| 158 | } |
| 159 | caller.value().receiveResponse(msgId, output); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 160 | } |
| 161 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 162 | * Auxiliary store delegate to receive notification about changes in the store. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 163 | */ |
| 164 | private class InternalStoreDelegate implements DynamicConfigStoreDelegate { |
| 165 | public void notify(DynamicConfigEvent event) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 166 | post(event); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |