f68ea1d33b36253508e71d81fb7224ebdefbf72c
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / listener / PeopleCarListener.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.listener;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.CarPeople;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPerson;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPersonBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPersonKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarBought;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarPurchaseListener;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public class PeopleCarListener implements CarPurchaseListener {
29
30     private static final Logger LOG = LoggerFactory.getLogger(PeopleCarListener.class);
31
32     private DataBroker dataProvider;
33
34
35
36     public void setDataProvider(final DataBroker salDataProvider) {
37         this.dataProvider = salDataProvider;
38     }
39
40     @Override
41     public void onCarBought(CarBought notification) {
42
43         final CarPersonBuilder carPersonBuilder = new CarPersonBuilder();
44         carPersonBuilder.setCarId(notification.getCarId());
45         carPersonBuilder.setPersonId(notification.getPersonId());
46         CarPersonKey key = new CarPersonKey(notification.getCarId(), notification.getPersonId());
47         carPersonBuilder.setKey(key);
48         final CarPerson carPerson = carPersonBuilder.build();
49
50         LOG.info("Car bought, adding car-person entry: [{}]", carPerson);
51
52         InstanceIdentifier<CarPerson> carPersonIId = InstanceIdentifier.<CarPeople>builder(CarPeople.class)
53                 .child(CarPerson.class, carPerson.getKey()).build();
54
55
56         WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
57         tx.put(LogicalDatastoreType.CONFIGURATION, carPersonIId, carPerson, true);
58
59         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
60             @Override
61             public void onSuccess(final Void result) {
62                 LOG.info("Successfully added car-person entry: [{}]", carPerson);
63             }
64
65             @Override
66             public void onFailure(final Throwable ex) {
67                 LOG.error(String.format("Failed to add car-person entry: [%s]", carPerson), ex);
68             }
69         }, MoreExecutors.directExecutor());
70     }
71 }