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
8 package org.opendaylight.controller.clustering.it.provider.impl;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
13 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
14 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
15 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
16 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
17 import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
18 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.api.ServiceGroupIdentifier;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
31 private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
33 private static final QNameModule MODULE =
34 QNameModule.ofRevision("tag:opendaylight.org,2017:controller:yang:lowlevel:target", "2017-02-15").intern();
35 private static final QName OUTPUT = YangConstants.operationOutputQName(MODULE).intern();
36 private static final QName CONSTANT = QName.create(MODULE, "constant").intern();
37 private static final QName CONTEXT = QName.create(MODULE, "context").intern();
38 private static final QName GET_SINGLETON_CONSTANT = QName.create(MODULE, "get-singleton-constant").intern();
40 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
41 new ServiceGroupIdentifier("get-singleton-constant-service");
43 private final DOMRpcProviderService rpcProviderService;
44 private final String constant;
46 private Registration rpcRegistration = null;
48 private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService, final String constant) {
49 this.rpcProviderService = rpcProviderService;
50 this.constant = constant;
53 public static Registration registerNew(final ClusterSingletonServiceProvider singletonService,
54 final DOMRpcProviderService rpcProviderService, final String constant) {
55 LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
57 return singletonService.registerClusterSingletonService(
58 new SingletonGetConstantService(rpcProviderService, constant));
62 public ListenableFuture<DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc, final ContainerNode input) {
63 LOG.debug("get-singleton-constant invoked, current value: {}", constant);
65 return Futures.immediateFuture(new DefaultDOMRpcResult(ImmutableNodes.newContainerBuilder()
66 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
67 .withChild(ImmutableNodes.leafNode(CONSTANT, constant))
72 public void instantiateServiceInstance() {
73 LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
74 final DOMRpcIdentifier id = DOMRpcIdentifier.create(GET_SINGLETON_CONSTANT);
76 rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
80 public ListenableFuture<Void> closeServiceInstance() {
81 LOG.debug("Closing get-singleton-constant instance");
82 rpcRegistration.close();
83 return Futures.immediateFuture(null);
87 public ServiceGroupIdentifier getIdentifier() {
88 return SERVICE_GROUP_IDENTIFIER;