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