Adjust for RPCService methods changing
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / SingletonGetConstantService.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.net.URI;
13 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
14 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
15 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
16 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
17 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
18 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.common.YangConstants;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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;
34
35 public final class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
38
39     private static final QNameModule MODULE = QNameModule.create(
40         URI.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target"), Revision.of("2017-02-15")).intern();
41     private static final QName OUTPUT = YangConstants.operationOutputQName(MODULE).intern();
42     private static final QName CONSTANT = QName.create(MODULE, "constant").intern();
43     private static final QName CONTEXT = QName.create(MODULE, "context").intern();
44     private static final QName GET_SINGLETON_CONSTANT = QName.create(MODULE, "get-singleton-constant").intern();
45
46     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
47             ServiceGroupIdentifier.create("get-singleton-constant-service");
48
49     private final DOMRpcProviderService rpcProviderService;
50     private final String constant;
51     private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
52
53     private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService, final String constant) {
54         this.rpcProviderService = rpcProviderService;
55         this.constant = constant;
56     }
57
58     public static ClusterSingletonServiceRegistration registerNew(
59             final ClusterSingletonServiceProvider singletonService, final DOMRpcProviderService rpcProviderService,
60             final String constant) {
61         LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
62
63         return singletonService.registerClusterSingletonService(
64             new SingletonGetConstantService(rpcProviderService, constant));
65     }
66
67     @Override
68     public ListenableFuture<? extends DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc, final ContainerNode input) {
69         LOG.debug("get-singleton-constant invoked, current value: {}", constant);
70
71         return Futures.immediateFuture(new DefaultDOMRpcResult(ImmutableContainerNodeBuilder.create()
72             .withNodeIdentifier(new NodeIdentifier(OUTPUT))
73             .withChild(ImmutableLeafNodeBuilder.create()
74                 .withNodeIdentifier(new NodeIdentifier(CONSTANT))
75                 .withValue(constant)
76                 .build())
77             .build()));
78     }
79
80     @Override
81     public void instantiateServiceInstance() {
82         LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
83         final DOMRpcIdentifier id = DOMRpcIdentifier.create(SchemaPath.create(true, GET_SINGLETON_CONSTANT));
84
85         rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
86     }
87
88     @Override
89     public ListenableFuture<Void> closeServiceInstance() {
90         LOG.debug("Closing get-singleton-constant instance");
91         rpcRegistration.close();
92         return Futures.immediateFuture(null);
93     }
94
95     @Override
96     public ServiceGroupIdentifier getIdentifier() {
97         return SERVICE_GROUP_IDENTIFIER;
98     }
99 }