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