2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.clustering.it.listener;
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;
26 public class PeopleCarListener implements CarPurchaseListener {
27 private static final Logger LOG = LoggerFactory.getLogger(PeopleCarListener.class);
29 private DataBroker dataProvider;
31 public void setDataProvider(final DataBroker salDataProvider) {
32 this.dataProvider = salDataProvider;
36 public void onCarBought(final CarBought notification) {
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();
45 LOG.info("Car bought, adding car-person entry: [{}]", carPerson);
47 InstanceIdentifier<CarPerson> carPersonIId = InstanceIdentifier.builder(CarPeople.class)
48 .child(CarPerson.class, carPerson.key()).build();
51 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
52 tx.put(LogicalDatastoreType.CONFIGURATION, carPersonIId, carPerson);
54 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
56 public void onSuccess(final CommitInfo result) {
57 LOG.info("Successfully added car-person entry: [{}]", carPerson);
61 public void onFailure(final Throwable ex) {
62 LOG.error("Failed to add car-person entry: [{}]", carPerson, ex);
64 }, MoreExecutors.directExecutor());