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