Merge "neutron now works with jetty"
[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 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;
32
33 public class PeopleProvider implements PeopleService, AutoCloseable {
34
35   private static final Logger log = LoggerFactory.getLogger(PeopleProvider.class);
36
37   private DataBroker dataProvider;
38
39   private BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration;
40
41   public void setDataProvider(final DataBroker salDataProvider) {
42     this.dataProvider = salDataProvider;
43   }
44
45
46   public void setRpcRegistration(BindingAwareBroker.RoutedRpcRegistration<CarPurchaseService> rpcRegistration) {
47     this.rpcRegistration = rpcRegistration;
48   }
49
50   @Override
51   public Future<RpcResult<Void>> addPerson(AddPersonInput input) {
52     log.info("RPC addPerson : adding person [{}]", input);
53
54     PersonBuilder builder = new PersonBuilder(input);
55     final Person person = builder.build();
56     final SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
57
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);
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.<Void>success().build());
74       }
75
76       @Override
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());
81       }
82     });
83     return futureResult;
84   }
85
86   @Override
87   public void close() throws Exception {
88
89   }
90 }