5644a6bfc40f2e454749a45b5b125147f1b01a0e
[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.datastore.exceptions.TimeoutException;
16 import org.opendaylight.controller.cluster.dom.api.CDSDataTreeProducer;
17 import org.opendaylight.controller.cluster.dom.api.CDSShardAccess;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
23 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomePrefixLeaderInput;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class PrefixLeaderHandler {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PrefixLeaderHandler.class);
34
35     private final DOMDataTreeService domDataTreeService;
36     private final BindingNormalizedNodeSerializer serializer;
37
38     public PrefixLeaderHandler(final DOMDataTreeService domDataTreeService,
39                                final BindingNormalizedNodeSerializer serializer) {
40         this.domDataTreeService = domDataTreeService;
41         this.serializer = serializer;
42     }
43
44     public ListenableFuture<RpcResult<Void>> makeLeaderLocal(final BecomePrefixLeaderInput input) {
45
46         final YangInstanceIdentifier yid = serializer.toYangInstanceIdentifier(input.getPrefix());
47         final DOMDataTreeIdentifier prefix = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yid);
48
49         try (final CDSDataTreeProducer producer =
50                      (CDSDataTreeProducer) domDataTreeService.createProducer(Collections.singleton(prefix))) {
51
52             final CDSShardAccess shardAccess = producer.getShardAccess(prefix);
53
54             final CompletionStage<Void> completionStage = shardAccess.makeLeaderLocal();
55
56             completionStage.exceptionally(throwable -> {
57                 LOG.error("Leader movement failed.", throwable);
58                 return null;
59             });
60         } catch (final DOMDataTreeProducerException e) {
61             LOG.warn("Error while closing producer", e);
62         } catch (final TimeoutException e) {
63             LOG.warn("Timeout while on producer operation", e);
64             Futures.immediateFuture(RpcResultBuilder.failed().withError(RpcError.ErrorType.RPC,
65                     "resource-denied-transport", "Timeout while opening producer please retry.", "clustering-it",
66                     "clustering-it", e));
67         }
68
69         return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
70     }
71 }