Fix findbugs warnings
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / PeopleProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.clustering.it.provider;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
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 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.AddPersonOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.People;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PeopleService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PersonContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.Person;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.PersonBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class PeopleProvider implements PeopleService, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(PeopleProvider.class);
38
39     private DataBroker dataProvider;
40
41     private BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration;
42
43     public void setDataProvider(final DataBroker salDataProvider) {
44         this.dataProvider = salDataProvider;
45     }
46
47
48     public void setRpcRegistration(final BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration) {
49         this.rpcRegistration = rpcRegistration;
50     }
51
52     @Override
53     public ListenableFuture<RpcResult<AddPersonOutput>> addPerson(final AddPersonInput input) {
54         LOG.info("RPC addPerson : adding person [{}]", input);
55
56         PersonBuilder builder = new PersonBuilder(input);
57         final Person person = builder.build();
58         final SettableFuture<RpcResult<AddPersonOutput>> futureResult = SettableFuture.create();
59
60         // Each entry will be identifiable by a unique key, we have to create that identifier
61         final InstanceIdentifier<Person> personId = InstanceIdentifier.builder(People.class)
62                 .child(Person.class, person.key()).build();
63         // Place entry in data store tree
64         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
65         tx.put(LogicalDatastoreType.CONFIGURATION, personId, person, true);
66
67         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
68             @Override
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.success(new AddPersonOutputBuilder().build()).build());
74             }
75
76             @Override
77             public void onFailure(final Throwable ex) {
78                 LOG.error("RPC addPerson : person addition failed [{}]", person, ex);
79                 futureResult.set(RpcResultBuilder.<AddPersonOutput>failed()
80                         .withError(RpcError.ErrorType.APPLICATION, ex.getMessage()).build());
81             }
82         }, MoreExecutors.directExecutor());
83         return futureResult;
84     }
85
86     @Override
87     public void close() {
88     }
89 }