blob: fac50737e1f71ee07047db6d27efd42ce76ce08a [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
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 */
16package org.onosproject.config.impl;
17
Sithara Punnassery06208792017-02-10 16:25:29 -080018import com.google.common.annotations.Beta;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Sithara Punnassery644a2662017-06-09 02:02:57 -070025import org.onlab.util.KryoNamespace;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080026import org.onosproject.config.DynamicConfigEvent;
27import org.onosproject.config.DynamicConfigListener;
28import org.onosproject.config.DynamicConfigService;
29import org.onosproject.config.DynamicConfigStore;
30import org.onosproject.config.DynamicConfigStoreDelegate;
31import org.onosproject.config.FailedException;
32import org.onosproject.config.Filter;
Sithara Punnassery644a2662017-06-09 02:02:57 -070033import org.onosproject.store.serializers.KryoNamespaces;
34import org.onosproject.store.service.ConsistentMap;
35import org.onosproject.store.service.StorageService;
36import org.onosproject.store.service.Serializer;
37import org.onosproject.store.service.Versioned;
Gaurav Agrawal02dbee32017-05-11 19:04:52 +053038import org.onosproject.yang.model.RpcCaller;
39import org.onosproject.yang.model.RpcCommand;
40import org.onosproject.yang.model.RpcHandler;
41import org.onosproject.yang.model.RpcInput;
42import org.onosproject.yang.model.RpcOutput;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080043import org.onosproject.yang.model.DataNode;
44import org.onosproject.yang.model.ResourceId;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080045import org.onosproject.event.AbstractListenerManager;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080046import org.slf4j.Logger;
47
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Demo application to use the DynamicConfig Service and DynamicConfigStore.
52 *
53 */
Sithara Punnassery06208792017-02-10 16:25:29 -080054@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080055@Component(immediate = true)
56@Service
57public 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 Punnassery9306e6b2017-02-06 15:38:19 -080063 protected DynamicConfigStore store;
Sithara Punnassery644a2662017-06-09 02:02:57 -070064 private ConsistentMap<RpcCommand, RpcHandler> handlerRegistry;
65 private ConsistentMap<Integer, RpcCaller> callerRegistry;
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected StorageService storageService;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080068
69 @Activate
70 public void activate() {
71 store.setDelegate(storeDelegate);
Sithara Punnassery43833e12017-03-14 16:29:19 -070072 eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry);
Sithara Punnassery644a2662017-06-09 02:02:57 -070073 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 Punnassery9306e6b2017-02-06 15:38:19 -080092 }
93
94 @Deactivate
95 public void deactivate() {
96 store.unsetDelegate(storeDelegate);
Sithara Punnassery43833e12017-03-14 16:29:19 -070097 eventDispatcher.removeSink(DynamicConfigEvent.class);
Sithara Punnassery644a2662017-06-09 02:02:57 -070098 log.info("Stopped");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080099 }
100
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800101 public void createNodeRecursive(ResourceId path, DataNode node) {
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700102 store.addNode(path, node).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800103 }
104
105 public DataNode readNode(ResourceId path, Filter filter) {
106 return store.readNode(path, filter).join();
107 }
108
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800109 public void updateNode(ResourceId path, DataNode node) {
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700110 store.updateNode(path, node).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800111 }
112
113 public void deleteNode(ResourceId path) {
114 throw new FailedException("Not yet implemented");
115 }
116
117 public void deleteNodeRecursive(ResourceId path) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800118 store.deleteNodeRecursive(path).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800119 }
120
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800121 public void replaceNode(ResourceId path, DataNode node) {
122 throw new FailedException("Not yet implemented");
123 }
Sithara Punnassery43833e12017-03-14 16:29:19 -0700124
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800125 public Integer getNumberOfChildren(ResourceId path, Filter filter) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800126 throw new FailedException("Not yet implemented");
127 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800128
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700129 public Boolean nodeExist(ResourceId path) {
130 return store.nodeExist(path).join();
131 }
132
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800133 public void registerHandler(RpcHandler handler, RpcCommand command) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700134 handlerRegistry.put(command, handler);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800135 }
136
137 public void unRegisterHandler(RpcHandler handler, RpcCommand command) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700138 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 Punnassery9306e6b2017-02-06 15:38:19 -0800143 }
144
145 public void invokeRpc(RpcCaller caller, Integer msgId, RpcCommand command, RpcInput input) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700146 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 Punnassery9306e6b2017-02-06 15:38:19 -0800152 }
153
154 public void rpcResponse(Integer msgId, RpcOutput output) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700155 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 Punnassery9306e6b2017-02-06 15:38:19 -0800160 }
161 /**
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800162 * Auxiliary store delegate to receive notification about changes in the store.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800163 */
164 private class InternalStoreDelegate implements DynamicConfigStoreDelegate {
165 public void notify(DynamicConfigEvent event) {
Sithara Punnassery43833e12017-03-14 16:29:19 -0700166 post(event);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800167 }
168 }
169}