BUG 7798 - Implement agent RPCs for singleton RPC registration testing
[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
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
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;
37
38 public class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
39
40     private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
41
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");
51
52     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
53             ServiceGroupIdentifier.create("get-singleton-constant-service");
54
55     private final DOMRpcProviderService rpcProviderService;
56     private final String constant;
57     private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
58
59     private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService,
60                                         final String constant) {
61
62
63         this.rpcProviderService = rpcProviderService;
64         this.constant = constant;
65     }
66
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);
71
72         return singletonService
73                 .registerClusterSingletonService(new SingletonGetConstantService(rpcProviderService, constant));
74     }
75
76     @Nonnull
77     @Override
78     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull DOMRpcIdentifier rpc, @Nullable NormalizedNode<?, ?> input) {
79         LOG.debug("get-singleton-constant invoked, current value: {}", constant);
80
81         final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
82                 .withNodeIdentifier(new NodeIdentifier(CONSTANT))
83                 .withValue(constant)
84                 .build();
85
86         final ContainerNode result = ImmutableContainerNodeBuilder.create()
87                 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
88                 .withChild(value)
89                 .build();
90
91         return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
92     }
93
94     @Override
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));
98
99         rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
100     }
101
102     @Override
103     public ListenableFuture<Void> closeServiceInstance() {
104         LOG.debug("Closing get-singleton-constant instance");
105         rpcRegistration.close();
106         return Futures.immediateFuture(null);
107     }
108
109     @Override
110     public ServiceGroupIdentifier getIdentifier() {
111         return SERVICE_GROUP_IDENTIFIER;
112     }
113 }