6cda13b8ff0df231ad44c6c55ab35a8712b448f1
[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 org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
13 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
14 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
15 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
16 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
17 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public final class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
35     private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
36
37     private static final QNameModule MODULE = QNameModule.create(
38         XMLNamespace.of("tag:opendaylight.org,2017:controller:yang:lowlevel:target"), Revision.of("2017-02-15"))
39         .intern();
40     private static final QName OUTPUT = YangConstants.operationOutputQName(MODULE).intern();
41     private static final QName CONSTANT = QName.create(MODULE, "constant").intern();
42     private static final QName CONTEXT = QName.create(MODULE, "context").intern();
43     private static final QName GET_SINGLETON_CONSTANT = QName.create(MODULE, "get-singleton-constant").intern();
44
45     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
46             ServiceGroupIdentifier.create("get-singleton-constant-service");
47
48     private final DOMRpcProviderService rpcProviderService;
49     private final String constant;
50     private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
51
52     private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService, final String constant) {
53         this.rpcProviderService = rpcProviderService;
54         this.constant = constant;
55     }
56
57     public static ClusterSingletonServiceRegistration registerNew(
58             final ClusterSingletonServiceProvider singletonService, final DOMRpcProviderService rpcProviderService,
59             final String constant) {
60         LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
61
62         return singletonService.registerClusterSingletonService(
63             new SingletonGetConstantService(rpcProviderService, constant));
64     }
65
66     @Override
67     public ListenableFuture<DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc, final ContainerNode input) {
68         LOG.debug("get-singleton-constant invoked, current value: {}", constant);
69
70         return Futures.immediateFuture(new DefaultDOMRpcResult(Builders.containerBuilder()
71             .withNodeIdentifier(new NodeIdentifier(OUTPUT))
72             .withChild(ImmutableNodes.leafNode(CONSTANT, constant))
73             .build()));
74     }
75
76     @Override
77     public void instantiateServiceInstance() {
78         LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
79         final DOMRpcIdentifier id = DOMRpcIdentifier.create(GET_SINGLETON_CONSTANT);
80
81         rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
82     }
83
84     @Override
85     public ListenableFuture<Void> closeServiceInstance() {
86         LOG.debug("Closing get-singleton-constant instance");
87         rpcRegistration.close();
88         return Futures.immediateFuture(null);
89     }
90
91     @Override
92     public ServiceGroupIdentifier getIdentifier() {
93         return SERVICE_GROUP_IDENTIFIER;
94     }
95 }