Reduce JSR305 proliferation
[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.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;
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 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");
48
49     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
50             ServiceGroupIdentifier.create("get-singleton-constant-service");
51
52     private final DOMRpcProviderService rpcProviderService;
53     private final String constant;
54     private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
55
56     private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService,
57                                         final String constant) {
58
59
60         this.rpcProviderService = rpcProviderService;
61         this.constant = constant;
62     }
63
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);
68
69         return singletonService
70                 .registerClusterSingletonService(new SingletonGetConstantService(rpcProviderService, constant));
71     }
72
73     @Override
74     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(DOMRpcIdentifier rpc,
75             NormalizedNode<?, ?> input) {
76         LOG.debug("get-singleton-constant invoked, current value: {}", constant);
77
78         final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
79                 .withNodeIdentifier(new NodeIdentifier(CONSTANT))
80                 .withValue(constant)
81                 .build();
82
83         final ContainerNode result = ImmutableContainerNodeBuilder.create()
84                 .withNodeIdentifier(new NodeIdentifier(OUTPUT))
85                 .withChild(value)
86                 .build();
87
88         return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
89     }
90
91     @Override
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));
95
96         rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
97     }
98
99     @Override
100     public ListenableFuture<Void> closeServiceInstance() {
101         LOG.debug("Closing get-singleton-constant instance");
102         rpcRegistration.close();
103         return Futures.immediateFuture(null);
104     }
105
106     @Override
107     public ServiceGroupIdentifier getIdentifier() {
108         return SERVICE_GROUP_IDENTIFIER;
109     }
110 }