Bug 7805 - Implement agent RPCs for shard leader movement testing
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / PrefixLeaderHandler.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.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.concurrent.CompletionStage;
15 import org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer;
16 import org.opendaylight.controller.cluster.dom.api.CDSShardAccess;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
21 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomePrefixLeaderInput;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class PrefixLeaderHandler {
29
30     private static final Logger LOG = LoggerFactory.getLogger(PrefixLeaderHandler.class);
31
32     private final DOMDataTreeService domDataTreeService;
33     private final BindingNormalizedNodeSerializer serializer;
34
35     public PrefixLeaderHandler(final DOMDataTreeService domDataTreeService,
36                                final BindingNormalizedNodeSerializer serializer) {
37         this.domDataTreeService = domDataTreeService;
38         this.serializer = serializer;
39     }
40
41     public ListenableFuture<RpcResult<Void>> makeLeaderLocal(final BecomePrefixLeaderInput input) {
42
43         final YangInstanceIdentifier yid = serializer.toYangInstanceIdentifier(input.getPrefix());
44         final DOMDataTreeIdentifier prefix = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yid);
45
46         final CDSDataTreeProducer producer =
47                 (CDSDataTreeProducer) domDataTreeService.createProducer(Collections.singleton(prefix));
48
49         final CDSShardAccess shardAccess = producer.getShardAccess(prefix);
50
51         final CompletionStage<Void> completionStage = shardAccess.makeLeaderLocal();
52
53         completionStage.exceptionally(throwable -> {
54             LOG.error("Leader movement failed.", throwable);
55             return null;
56         });
57
58         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
59     }
60 }