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
9 package org.opendaylight.controller.clustering.it.provider;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.SettableFuture;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarPurchaseService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.People;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PeopleService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PersonContext;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.Person;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.PersonBuilder;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.concurrent.Future;
34 public class PeopleProvider implements PeopleService, AutoCloseable {
36 private static final Logger log = LoggerFactory.getLogger(PeopleProvider.class);
38 private DataBroker dataProvider;
40 private BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration;
42 public void setDataProvider(final DataBroker salDataProvider) {
43 this.dataProvider = salDataProvider;
47 public void setRpcRegistration(BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration) {
48 this.rpcRegistration = rpcRegistration;
52 public Future<RpcResult<Void>> addPerson(AddPersonInput input) {
53 log.info("RPC addPerson : adding person [{}]", input);
55 PersonBuilder builder = new PersonBuilder(input);
56 final Person person = builder.build();
57 final SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
59 // Each entry will be identifiable by a unique key, we have to create that identifier
60 final InstanceIdentifier.InstanceIdentifierBuilder<Person> personIdBuilder =
61 InstanceIdentifier.<People>builder(People.class)
62 .child(Person.class, person.getKey());
63 final InstanceIdentifier personId = personIdBuilder.build();
64 // Place entry in data store tree
65 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
66 tx.put(LogicalDatastoreType.CONFIGURATION, personId, person);
68 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
70 public void onSuccess(final Void result) {
71 log.info("RPC addPerson : person added successfully [{}]", person);
72 rpcRegistration.registerPath(PersonContext.class, personId);
73 log.info("RPC addPerson : routed rpc registered for instance ID [{}]", personId);
74 futureResult.set(RpcResultBuilder.<Void>success().build());
78 public void onFailure(final Throwable t) {
79 log.info("RPC addPerson : person addition failed [{}]", person);
80 futureResult.set(RpcResultBuilder.<Void>failed()
81 .withError(RpcError.ErrorType.APPLICATION, t.getMessage()).build());
88 public void close() throws Exception {