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.CheckedFuture;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
23 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public final class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
37 private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
39 private static final QName OUTPUT =
40 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "output");
41 private static final QName CONSTANT =
42 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "constant");
43 private static final QName CONTEXT =
44 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "context");
45 private static final QName GET_SINGLETON_CONSTANT =
46 QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15",
47 "get-singleton-constant");
49 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
50 ServiceGroupIdentifier.create("get-singleton-constant-service");
52 private final DOMRpcProviderService rpcProviderService;
53 private final String constant;
54 private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
56 private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService,
57 final String constant) {
60 this.rpcProviderService = rpcProviderService;
61 this.constant = constant;
64 public static ClusterSingletonServiceRegistration registerNew(
65 final ClusterSingletonServiceProvider singletonService, final DOMRpcProviderService rpcProviderService,
66 final String constant) {
67 LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
69 return singletonService
70 .registerClusterSingletonService(new SingletonGetConstantService(rpcProviderService, constant));
74 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(DOMRpcIdentifier rpc,
75 NormalizedNode<?, ?> input) {
76 LOG.debug("get-singleton-constant invoked, current value: {}", constant);
78 final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
79 .withNodeIdentifier(new NodeIdentifier(CONSTANT))
83 final ContainerNode result = ImmutableContainerNodeBuilder.create()
84 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
88 return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
92 public void instantiateServiceInstance() {
93 LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
94 final DOMRpcIdentifier id = DOMRpcIdentifier.create(SchemaPath.create(true, GET_SINGLETON_CONSTANT));
96 rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
100 public ListenableFuture<Void> closeServiceInstance() {
101 LOG.debug("Closing get-singleton-constant instance");
102 rpcRegistration.close();
103 return Futures.immediateFuture(null);
107 public ServiceGroupIdentifier getIdentifier() {
108 return SERVICE_GROUP_IDENTIFIER;