2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.clustering.it.provider.impl;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
22 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
26 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
40 private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
42 private static final QName OUTPUT =
43 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "output");
44 private static final QName CONSTANT =
45 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "constant");
46 private static final QName CONTEXT =
47 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "context");
48 private static final QName GET_SINGLETON_CONSTANT =
49 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15",
50 "get-singleton-constant");
52 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
53 ServiceGroupIdentifier.create("get-singleton-constant-service");
55 private final DOMRpcProviderService rpcProviderService;
56 private final String constant;
57 private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
59 private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService,
60 final String constant) {
63 this.rpcProviderService = rpcProviderService;
64 this.constant = constant;
67 public static ClusterSingletonServiceRegistration registerNew(final ClusterSingletonServiceProvider singletonService,
68 final DOMRpcProviderService rpcProviderService,
69 final String constant) {
70 LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
72 return singletonService
73 .registerClusterSingletonService(new SingletonGetConstantService(rpcProviderService, constant));
78 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull DOMRpcIdentifier rpc, @Nullable NormalizedNode<?, ?> input) {
79 LOG.debug("get-singleton-constant invoked, current value: {}", constant);
81 final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
82 .withNodeIdentifier(new NodeIdentifier(CONSTANT))
86 final ContainerNode result = ImmutableContainerNodeBuilder.create()
87 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
91 return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
95 public void instantiateServiceInstance() {
96 LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
97 final DOMRpcIdentifier id = DOMRpcIdentifier.create(SchemaPath.create(true, GET_SINGLETON_CONSTANT));
99 rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
103 public ListenableFuture<Void> closeServiceInstance() {
104 LOG.debug("Closing get-singleton-constant instance");
105 rpcRegistration.close();
106 return Futures.immediateFuture(null);
110 public ServiceGroupIdentifier getIdentifier() {
111 return SERVICE_GROUP_IDENTIFIER;