Merge "Created Benchmark for InMemoryDataStore Write Op"
[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
9 package org.opendaylight.controller.clustering.it.provider;
10
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;
31
32 import java.util.concurrent.Future;
33
34 public class PeopleProvider implements PeopleService, AutoCloseable {
35
36   private static final Logger log = LoggerFactory.getLogger(PeopleProvider.class);
37
38   private DataBroker dataProvider;
39
40   private BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration;
41
42   public void setDataProvider(final DataBroker salDataProvider) {
43     this.dataProvider = salDataProvider;
44   }
45
46
47   public void setRpcRegistration(BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration) {
48     this.rpcRegistration = rpcRegistration;
49   }
50
51   @Override
52   public Future<RpcResult<Void>> addPerson(AddPersonInput input) {
53     log.info("RPC addPerson : adding person [{}]", input);
54
55     PersonBuilder builder = new PersonBuilder(input);
56     final Person person = builder.build();
57     final SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
58
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);
67
68     Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
69       @Override
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());
75       }
76
77       @Override
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());
82       }
83     });
84     return futureResult;
85   }
86
87   @Override
88   public void close() throws Exception {
89
90   }
91 }