Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [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.config.impl; |
| 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; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 24 | import org.onosproject.config.ResourceIdParser; |
| 25 | import org.onosproject.config.RpcExecutor; |
| 26 | import org.onosproject.config.RpcMessageId; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 27 | import org.onosproject.event.AbstractListenerManager; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 28 | import org.onosproject.config.DynamicConfigEvent; |
| 29 | import org.onosproject.config.DynamicConfigListener; |
| 30 | import org.onosproject.config.DynamicConfigService; |
| 31 | import org.onosproject.config.DynamicConfigStore; |
| 32 | import org.onosproject.config.DynamicConfigStoreDelegate; |
| 33 | import org.onosproject.config.FailedException; |
| 34 | import org.onosproject.config.Filter; |
Gaurav Agrawal | 02dbee3 | 2017-05-11 19:04:52 +0530 | [diff] [blame] | 35 | import org.onosproject.yang.model.RpcInput; |
| 36 | import org.onosproject.yang.model.RpcOutput; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 37 | import org.onosproject.yang.model.DataNode; |
| 38 | import org.onosproject.yang.model.ResourceId; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 39 | import org.onosproject.yang.model.RpcRegistry; |
| 40 | import org.onosproject.yang.model.RpcService; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 41 | |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 42 | import java.util.HashSet; |
| 43 | import java.util.Map; |
| 44 | import java.util.Set; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 45 | import java.util.concurrent.CompletableFuture; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 46 | import java.util.concurrent.ConcurrentHashMap; |
| 47 | import java.util.concurrent.Executors; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 48 | |
| 49 | import org.slf4j.Logger; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 53 | * Implementation of the Dynamic Config Service. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 54 | * |
| 55 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class DynamicConfigManager |
| 59 | extends AbstractListenerManager<DynamicConfigEvent, DynamicConfigListener> |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 60 | implements DynamicConfigService, RpcRegistry { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 61 | private final Logger log = getLogger(getClass()); |
| 62 | private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate(); |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 64 | protected DynamicConfigStore store; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 65 | private ConcurrentHashMap<String, RpcService> handlerRegistry = new ConcurrentHashMap<>(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | store.setDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 70 | eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 71 | log.info("Started"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
| 76 | store.unsetDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 77 | eventDispatcher.removeSink(DynamicConfigEvent.class); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 78 | handlerRegistry.clear(); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 79 | log.info("Stopped"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 82 | public void createNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 83 | store.addNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public DataNode readNode(ResourceId path, Filter filter) { |
| 87 | return store.readNode(path, filter).join(); |
| 88 | } |
| 89 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 90 | public void updateNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 91 | store.updateNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public void deleteNode(ResourceId path) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 95 | store.deleteNodeRecursive(path).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 98 | public void replaceNode(ResourceId path, DataNode node) { |
| 99 | throw new FailedException("Not yet implemented"); |
| 100 | } |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 101 | |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 102 | public Boolean nodeExist(ResourceId path) { |
| 103 | return store.nodeExist(path).join(); |
| 104 | } |
| 105 | |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 106 | public Set<RpcService> getRpcServices() { |
| 107 | Set<RpcService> res = new HashSet(); |
| 108 | for (Map.Entry<String, RpcService> e : handlerRegistry.entrySet()) { |
| 109 | res.add(e.getValue()); |
| 110 | } |
| 111 | return res; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 114 | public RpcService getRpcService(Class<? extends RpcService> intfc) { |
| 115 | return handlerRegistry.get(intfc.getSimpleName()); |
| 116 | } |
| 117 | |
| 118 | public void registerRpcService(RpcService handler) { |
| 119 | for (Class<?> intfc : handler.getClass().getInterfaces()) { |
| 120 | if (RpcService.class.isAssignableFrom(intfc)) { |
| 121 | handlerRegistry.put(intfc.getSimpleName(), handler); |
| 122 | } |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 123 | } |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public void unregisterRpcService(RpcService handler) { |
| 127 | for (Class<?> intfc : handler.getClass().getInterfaces()) { |
| 128 | if (RpcService.class.isAssignableFrom(intfc)) { |
| 129 | String key = intfc.getSimpleName(); |
| 130 | if (handlerRegistry.get(key) == null) { |
| 131 | throw new FailedException("No registered handler found, cannot unregister"); |
| 132 | } |
| 133 | handlerRegistry.remove(key); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private int getSvcId(RpcService handler, String srvc) { |
| 139 | Class<?>[] intfcs = handler.getClass().getInterfaces(); |
| 140 | for (int i = 0; i < intfcs.length; i++) { |
| 141 | if (intfcs[i].getSimpleName().compareTo(srvc) == 0) { |
| 142 | return i; |
| 143 | } |
| 144 | } |
| 145 | throw new FailedException("No handler found, cannot invoke"); |
| 146 | } |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 147 | |
| 148 | public CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input) { |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 149 | String[] ctxt = ResourceIdParser.getService(id); |
| 150 | RpcService handler = handlerRegistry.get(ctxt[0]); |
| 151 | if (handler == null) { |
| 152 | throw new FailedException("No registered handler found, cannot invoke"); |
| 153 | } |
| 154 | return CompletableFuture.supplyAsync( |
| 155 | new RpcExecutor(handler, getSvcId(handler, ctxt[0]), ctxt[1], RpcMessageId.generate(), input), |
| 156 | Executors.newSingleThreadExecutor()); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 159 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 160 | * Auxiliary store delegate to receive notification about changes in the store. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 161 | */ |
| 162 | private class InternalStoreDelegate implements DynamicConfigStoreDelegate { |
| 163 | public void notify(DynamicConfigEvent event) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 164 | post(event); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |