blob: 5ec54228f6318d4ab6cb71f969b278c94581f54f [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 Punnassery7b0c15e2017-07-07 15:08:19 -070026import org.onosproject.event.AbstractListenerManager;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.StorageService;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080029import org.onosproject.config.DynamicConfigEvent;
30import org.onosproject.config.DynamicConfigListener;
31import org.onosproject.config.DynamicConfigService;
32import org.onosproject.config.DynamicConfigStore;
33import org.onosproject.config.DynamicConfigStoreDelegate;
34import org.onosproject.config.FailedException;
35import org.onosproject.config.Filter;
Gaurav Agrawal02dbee32017-05-11 19:04:52 +053036import org.onosproject.yang.model.RpcHandler;
37import org.onosproject.yang.model.RpcInput;
38import org.onosproject.yang.model.RpcOutput;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080039import org.onosproject.yang.model.DataNode;
40import org.onosproject.yang.model.ResourceId;
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070041//TODO import org.onosproject.yang.model.RpcRegistry;
42//TODO import org.onosproject.yang.model.RpcService;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080043
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070044import java.util.concurrent.CompletableFuture;
45
46import org.slf4j.Logger;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070050 * Implementation of the Dynamic Config Service.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080051 *
52 */
Sithara Punnassery06208792017-02-10 16:25:29 -080053@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080054@Component(immediate = true)
55@Service
56public class DynamicConfigManager
57 extends AbstractListenerManager<DynamicConfigEvent, DynamicConfigListener>
58 implements DynamicConfigService {
59 private final Logger log = getLogger(getClass());
60 private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate();
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062 protected DynamicConfigStore store;
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070063 //TODO after 14420 is merged
64 //private ConsistentMap<RpcService, RpcHandler> handlerRegistry;
Sithara Punnassery644a2662017-06-09 02:02:57 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected StorageService storageService;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080067
68 @Activate
69 public void activate() {
70 store.setDelegate(storeDelegate);
Sithara Punnassery43833e12017-03-14 16:29:19 -070071 eventDispatcher.addSink(DynamicConfigEvent.class, listenerRegistry);
Sithara Punnassery644a2662017-06-09 02:02:57 -070072 log.info("Started");
73 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
74 .register(KryoNamespaces.BASIC)
75 .register(Class.class)
76 .register(RpcHandler.class)
Sithara Punnassery644a2662017-06-09 02:02:57 -070077 .register(ResourceId.class);
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070078 //TODO after 14420 is merged
79 /*handlerRegistry = storageService.<RpcService, RpcHandler>consistentMapBuilder()
Sithara Punnassery644a2662017-06-09 02:02:57 -070080 .withSerializer(Serializer.using(kryoBuilder.build()))
81 .withName("config-object-store")
82 .withRelaxedReadConsistency()
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070083 .build();*/
Sithara Punnassery644a2662017-06-09 02:02:57 -070084 log.info("Started");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080085 }
86
87 @Deactivate
88 public void deactivate() {
89 store.unsetDelegate(storeDelegate);
Sithara Punnassery43833e12017-03-14 16:29:19 -070090 eventDispatcher.removeSink(DynamicConfigEvent.class);
Sithara Punnassery644a2662017-06-09 02:02:57 -070091 log.info("Stopped");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080092 }
93
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -070094 public void createNode(ResourceId path, DataNode node) {
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -070095 store.addNode(path, node).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080096 }
97
98 public DataNode readNode(ResourceId path, Filter filter) {
99 return store.readNode(path, filter).join();
100 }
101
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800102 public void updateNode(ResourceId path, DataNode node) {
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700103 store.updateNode(path, node).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800104 }
105
106 public void deleteNode(ResourceId path) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800107 store.deleteNodeRecursive(path).join();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800108 }
109
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800110 public void replaceNode(ResourceId path, DataNode node) {
111 throw new FailedException("Not yet implemented");
112 }
Sithara Punnassery43833e12017-03-14 16:29:19 -0700113
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700114 public Boolean nodeExist(ResourceId path) {
115 return store.nodeExist(path).join();
116 }
117
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -0700118 //TODO after RPC abstractions are merged; else will lead to build failure
119 /*public void registerHandler(RpcHandler handler, RpcCommand command) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700120 handlerRegistry.put(command, handler);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800121 }
122
123 public void unRegisterHandler(RpcHandler handler, RpcCommand command) {
Sithara Punnassery644a2662017-06-09 02:02:57 -0700124 Versioned<RpcHandler> ret = handlerRegistry.get(command);
125 if ((ret == null) || (ret.value() == null)) {
126 throw new FailedException("No registered handler found, cannot unregister");
127 }
128 handlerRegistry.remove(command);
Sithara Punnassery7b0c15e2017-07-07 15:08:19 -0700129 }*/
130
131 public CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input) {
132 //TODO after RPC abstractions are merged; else will lead to build failure
133 throw new FailedException("Not yet implemented");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800134 }
135
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800136 /**
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800137 * Auxiliary store delegate to receive notification about changes in the store.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800138 */
139 private class InternalStoreDelegate implements DynamicConfigStoreDelegate {
140 public void notify(DynamicConfigEvent event) {
Sithara Punnassery43833e12017-03-14 16:29:19 -0700141 post(event);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800142 }
143 }
144}