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.RpcExecutor; |
| 25 | import org.onosproject.config.RpcMessageId; |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 26 | import org.onosproject.d.config.DeviceResourceIds; |
| 27 | import org.onosproject.d.config.ResourceIds; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 28 | import org.onosproject.event.AbstractListenerManager; |
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.RpcInput; |
| 37 | import org.onosproject.yang.model.RpcOutput; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 38 | import org.onosproject.yang.model.DataNode; |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 39 | import org.onosproject.yang.model.DataNode.Type; |
| 40 | import org.onosproject.yang.model.InnerNode; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 41 | import org.onosproject.yang.model.ResourceId; |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 42 | import org.onosproject.yang.model.RpcContext; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 43 | import org.onosproject.yang.model.RpcRegistry; |
| 44 | import org.onosproject.yang.model.RpcService; |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 45 | import org.onosproject.yang.model.SchemaContextProvider; |
| 46 | |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 47 | import java.util.HashSet; |
| 48 | import java.util.Map; |
| 49 | import java.util.Set; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 50 | import java.util.concurrent.CompletableFuture; |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentHashMap; |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 53 | |
| 54 | import static org.onosproject.d.config.DeviceResourceIds.DCS_NAMESPACE; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 58 | * Implementation of the Dynamic Config Service. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 59 | * |
| 60 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class DynamicConfigManager |
| 64 | extends AbstractListenerManager<DynamicConfigEvent, DynamicConfigListener> |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 65 | implements DynamicConfigService, RpcRegistry { |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 66 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 67 | private final Logger log = getLogger(getClass()); |
| 68 | private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate(); |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 69 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 71 | protected DynamicConfigStore store; |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected SchemaContextProvider contextProvider; |
| 75 | |
| 76 | // FIXME is it OK this is not using the Store? |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 77 | private ConcurrentHashMap<String, RpcService> handlerRegistry = new ConcurrentHashMap<>(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 78 | |
| 79 | @Activate |
| 80 | public void activate() { |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 81 | initStore(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 82 | store.setDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 83 | eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry); |
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 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Ensure built-in tree nodes exists. |
| 89 | */ |
| 90 | private void initStore() { |
| 91 | store.nodeExist(ResourceIds.ROOT_ID) |
| 92 | .thenAccept(exists -> { |
| 93 | if (!exists) { |
| 94 | log.info("Root node does not exist!, creating..."); |
| 95 | store.addNode(null, |
| 96 | InnerNode.builder(DeviceResourceIds.ROOT_NAME, DCS_NAMESPACE) |
| 97 | .type(Type.SINGLE_INSTANCE_NODE).build()); |
| 98 | } |
| 99 | }).join(); |
| 100 | |
| 101 | store.nodeExist(DeviceResourceIds.DEVICES_ID) |
| 102 | .thenAccept(exists -> { |
| 103 | if (!exists) { |
| 104 | log.info("devices node does not exist!, creating..."); |
| 105 | store.addNode(ResourceIds.ROOT_ID, |
| 106 | InnerNode.builder(DeviceResourceIds.DEVICES_NAME, DCS_NAMESPACE) |
| 107 | .type(Type.SINGLE_INSTANCE_NODE).build()); |
| 108 | } |
| 109 | }).join(); |
| 110 | } |
| 111 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 112 | @Deactivate |
| 113 | public void deactivate() { |
| 114 | store.unsetDelegate(storeDelegate); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 115 | eventDispatcher.removeSink(DynamicConfigEvent.class); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 116 | handlerRegistry.clear(); |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 117 | log.info("Stopped"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 120 | @Override |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 121 | public void createNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 122 | store.addNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 125 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 126 | public DataNode readNode(ResourceId path, Filter filter) { |
| 127 | return store.readNode(path, filter).join(); |
| 128 | } |
| 129 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 130 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 131 | public void updateNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 132 | store.updateNode(path, node).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 135 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 136 | public void deleteNode(ResourceId path) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 137 | store.deleteNodeRecursive(path).join(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 140 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 141 | public void replaceNode(ResourceId path, DataNode node) { |
| 142 | throw new FailedException("Not yet implemented"); |
| 143 | } |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 144 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 145 | @Override |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 146 | public Boolean nodeExist(ResourceId path) { |
| 147 | return store.nodeExist(path).join(); |
| 148 | } |
| 149 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 150 | @Override |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 151 | public Set<RpcService> getRpcServices() { |
| 152 | Set<RpcService> res = new HashSet(); |
| 153 | for (Map.Entry<String, RpcService> e : handlerRegistry.entrySet()) { |
| 154 | res.add(e.getValue()); |
| 155 | } |
| 156 | return res; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 159 | @Override |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 160 | public RpcService getRpcService(Class<? extends RpcService> intfc) { |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 161 | return handlerRegistry.get(intfc.getName()); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 164 | @Override |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 165 | public void registerRpcService(RpcService handler) { |
| 166 | for (Class<?> intfc : handler.getClass().getInterfaces()) { |
| 167 | if (RpcService.class.isAssignableFrom(intfc)) { |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 168 | handlerRegistry.put(intfc.getName(), handler); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 169 | } |
Sithara Punnassery | 644a266 | 2017-06-09 02:02:57 -0700 | [diff] [blame] | 170 | } |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 173 | @Override |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 174 | public void unregisterRpcService(RpcService handler) { |
| 175 | for (Class<?> intfc : handler.getClass().getInterfaces()) { |
| 176 | if (RpcService.class.isAssignableFrom(intfc)) { |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 177 | String key = intfc.getName(); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 178 | if (handlerRegistry.get(key) == null) { |
| 179 | throw new FailedException("No registered handler found, cannot unregister"); |
| 180 | } |
| 181 | handlerRegistry.remove(key); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private int getSvcId(RpcService handler, String srvc) { |
| 187 | Class<?>[] intfcs = handler.getClass().getInterfaces(); |
| 188 | for (int i = 0; i < intfcs.length; i++) { |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 189 | if (intfcs[i].getName().compareTo(srvc) == 0) { |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 190 | return i; |
| 191 | } |
| 192 | } |
| 193 | throw new FailedException("No handler found, cannot invoke"); |
| 194 | } |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 195 | |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 196 | @Override |
Sithara Punnassery | 7b0c15e | 2017-07-07 15:08:19 -0700 | [diff] [blame] | 197 | public CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input) { |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 198 | RpcContext context = contextProvider.getRpcContext(id); |
| 199 | String srvcIntf = context.serviceIntf().getName(); |
| 200 | RpcService handler = handlerRegistry.get(srvcIntf); |
Sithara Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 201 | if (handler == null) { |
| 202 | throw new FailedException("No registered handler found, cannot invoke"); |
| 203 | } |
| 204 | return CompletableFuture.supplyAsync( |
Gaurav Agrawal | 0032133 | 2017-12-01 17:06:34 +0530 | [diff] [blame] | 205 | new RpcExecutor(handler, getSvcId(handler, srvcIntf), |
| 206 | context.rpcName(), RpcMessageId.generate(), input)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 209 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 210 | * Auxiliary store delegate to receive notification about changes in the store. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 211 | */ |
| 212 | private class InternalStoreDelegate implements DynamicConfigStoreDelegate { |
Yuta HIGUCHI | 289f45c | 2017-11-01 17:49:45 -0700 | [diff] [blame] | 213 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 214 | public void notify(DynamicConfigEvent event) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 215 | post(event); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |