Remove support for actions/rpc/notifications
[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 javax.annotation.PreDestroy;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.RpcProviderService;
24 import org.opendaylight.mdsal.binding.api.WriteTransaction;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarPurchaseService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.AddPersonOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.People;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.PeopleService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.Person;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.people.rev140818.people.PersonBuilder;
35 import org.opendaylight.yangtools.concepts.ObjectRegistration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.ErrorType;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @Singleton
48 @Component(service = { })
49 public final class PeopleProvider implements PeopleService, AutoCloseable {
50     private static final Logger LOG = LoggerFactory.getLogger(PeopleProvider.class);
51
52     private final Set<ObjectRegistration<?>> regs = new HashSet<>();
53     private final DataBroker dataProvider;
54     private final RpcProviderService rpcProviderService;
55     private final CarPurchaseService rpcImplementation;
56
57     @Inject
58     @Activate
59     public PeopleProvider(@Reference final DataBroker dataProvider,
60             @Reference final RpcProviderService rpcProviderService,
61             @Reference final CarPurchaseService rpcImplementation) {
62         this.dataProvider = requireNonNull(dataProvider);
63         this.rpcProviderService = requireNonNull(rpcProviderService);
64         this.rpcImplementation = requireNonNull(rpcImplementation);
65
66         // Add global registration
67         regs.add(rpcProviderService.registerRpcImplementation(CarPurchaseService.class, rpcImplementation));
68     }
69
70     @Override
71     public ListenableFuture<RpcResult<AddPersonOutput>> addPerson(final AddPersonInput input) {
72         LOG.info("RPC addPerson : adding person [{}]", input);
73
74         PersonBuilder builder = new PersonBuilder(input);
75         final Person person = builder.build();
76         final SettableFuture<RpcResult<AddPersonOutput>> futureResult = SettableFuture.create();
77
78         // Each entry will be identifiable by a unique key, we have to create that identifier
79         final InstanceIdentifier<Person> personId = InstanceIdentifier.builder(People.class)
80                 .child(Person.class, person.key()).build();
81         // Place entry in data store tree
82         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
83         tx.put(LogicalDatastoreType.CONFIGURATION, personId, person);
84
85         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
86             @Override
87             public void onSuccess(final CommitInfo result) {
88                 LOG.info("RPC addPerson : person added successfully [{}]", person);
89                 regs.add(rpcProviderService.registerRpcImplementation(CarPurchaseService.class, rpcImplementation,
90                     ImmutableSet.of(personId)));
91                 LOG.info("RPC addPerson : routed rpc registered for instance ID [{}]", personId);
92                 futureResult.set(RpcResultBuilder.success(new AddPersonOutputBuilder().build()).build());
93             }
94
95             @Override
96             public void onFailure(final Throwable ex) {
97                 LOG.error("RPC addPerson : person addition failed [{}]", person, ex);
98                 futureResult.set(RpcResultBuilder.<AddPersonOutput>failed()
99                         .withError(ErrorType.APPLICATION, ex.getMessage()).build());
100             }
101         }, MoreExecutors.directExecutor());
102         return futureResult;
103     }
104
105     @PreDestroy
106     @Deactivate
107     @Override
108     public void close() {
109         regs.forEach(ObjectRegistration::close);
110         regs.clear();
111     }
112 }