2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.clustering.it.provider;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.HashSet;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.RpcProviderService;
19 import org.opendaylight.mdsal.binding.api.WriteTransaction;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarPurchaseService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.People;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PeopleService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.Person;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.PersonBuilder;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class PeopleProvider implements PeopleService, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(PeopleProvider.class);
42 private final Set<ObjectRegistration<?>> regs = new HashSet<>();
43 private DataBroker dataProvider;
44 private RpcProviderService rpcProviderService;
45 private CarPurchaseService rpcImplementation;
47 public void setDataProvider(final DataBroker salDataProvider) {
48 this.dataProvider = salDataProvider;
51 public void setRpcImplementation(final CarPurchaseService rpcImplementation) {
52 this.rpcImplementation = rpcImplementation;
56 public ListenableFuture<RpcResult<AddPersonOutput>> addPerson(final AddPersonInput input) {
57 LOG.info("RPC addPerson : adding person [{}]", input);
59 PersonBuilder builder = new PersonBuilder(input);
60 final Person person = builder.build();
61 final SettableFuture<RpcResult<AddPersonOutput>> futureResult = SettableFuture.create();
63 // Each entry will be identifiable by a unique key, we have to create that identifier
64 final InstanceIdentifier<Person> personId = InstanceIdentifier.builder(People.class)
65 .child(Person.class, person.key()).build();
66 // Place entry in data store tree
67 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
68 tx.put(LogicalDatastoreType.CONFIGURATION, personId, person);
70 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
72 public void onSuccess(final CommitInfo result) {
73 LOG.info("RPC addPerson : person added successfully [{}]", person);
74 regs.add(rpcProviderService.registerRpcImplementation(CarPurchaseService.class, rpcImplementation,
75 ImmutableSet.of(personId)));
76 LOG.info("RPC addPerson : routed rpc registered for instance ID [{}]", personId);
77 futureResult.set(RpcResultBuilder.success(new AddPersonOutputBuilder().build()).build());
81 public void onFailure(final Throwable ex) {
82 LOG.error("RPC addPerson : person addition failed [{}]", person, ex);
83 futureResult.set(RpcResultBuilder.<AddPersonOutput>failed()
84 .withError(RpcError.ErrorType.APPLICATION, ex.getMessage()).build());
86 }, MoreExecutors.directExecutor());
92 regs.forEach(ObjectRegistration::close);