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