Fix PeopleProvider activation
[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 static java.util.Objects.requireNonNull;
11
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;
18 import java.util.Set;
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;
39
40 public class PeopleProvider implements PeopleService, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(PeopleProvider.class);
43
44     private final Set<ObjectRegistration<?>> regs = new HashSet<>();
45     private final DataBroker dataProvider;
46     private final RpcProviderService rpcProviderService;
47     private final CarPurchaseService rpcImplementation;
48
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);
54     }
55
56     @Override
57     public ListenableFuture<RpcResult<AddPersonOutput>> addPerson(final AddPersonInput input) {
58         LOG.info("RPC addPerson : adding person [{}]", input);
59
60         PersonBuilder builder = new PersonBuilder(input);
61         final Person person = builder.build();
62         final SettableFuture<RpcResult<AddPersonOutput>> futureResult = SettableFuture.create();
63
64         // Each entry will be identifiable by a unique key, we have to create that identifier
65         final InstanceIdentifier<Person> personId = InstanceIdentifier.builder(People.class)
66                 .child(Person.class, person.key()).build();
67         // Place entry in data store tree
68         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
69         tx.put(LogicalDatastoreType.CONFIGURATION, personId, person);
70
71         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
72             @Override
73             public void onSuccess(final CommitInfo result) {
74                 LOG.info("RPC addPerson : person added successfully [{}]", person);
75                 regs.add(rpcProviderService.registerRpcImplementation(CarPurchaseService.class, rpcImplementation,
76                     ImmutableSet.of(personId)));
77                 LOG.info("RPC addPerson : routed rpc registered for instance ID [{}]", personId);
78                 futureResult.set(RpcResultBuilder.success(new AddPersonOutputBuilder().build()).build());
79             }
80
81             @Override
82             public void onFailure(final Throwable ex) {
83                 LOG.error("RPC addPerson : person addition failed [{}]", person, ex);
84                 futureResult.set(RpcResultBuilder.<AddPersonOutput>failed()
85                         .withError(RpcError.ErrorType.APPLICATION, ex.getMessage()).build());
86             }
87         }, MoreExecutors.directExecutor());
88         return futureResult;
89     }
90
91     @Override
92     public void close() {
93         regs.forEach(ObjectRegistration::close);
94         regs.clear();
95     }
96 }