2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.clustering.it.provider.impl;
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.DOMDataTreeProducerException;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
22 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomePrefixLeaderInput;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class PrefixLeaderHandler {
31 private static final Logger LOG = LoggerFactory.getLogger(PrefixLeaderHandler.class);
33 private final DOMDataTreeService domDataTreeService;
34 private final BindingNormalizedNodeSerializer serializer;
36 public PrefixLeaderHandler(final DOMDataTreeService domDataTreeService,
37 final BindingNormalizedNodeSerializer serializer) {
38 this.domDataTreeService = domDataTreeService;
39 this.serializer = serializer;
42 public ListenableFuture<RpcResult<Void>> makeLeaderLocal(final BecomePrefixLeaderInput input) {
44 final YangInstanceIdentifier yid = serializer.toYangInstanceIdentifier(input.getPrefix());
45 final DOMDataTreeIdentifier prefix = new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yid);
47 try (final CDSDataTreeProducer producer =
48 (CDSDataTreeProducer) domDataTreeService.createProducer(Collections.singleton(prefix))) {
50 final CDSShardAccess shardAccess = producer.getShardAccess(prefix);
52 final CompletionStage<Void> completionStage = shardAccess.makeLeaderLocal();
54 completionStage.exceptionally(throwable -> {
55 LOG.error("Leader movement failed.", throwable);
58 } catch (final DOMDataTreeProducerException e) {
59 LOG.warn("Error while closing producer", e);
62 return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());