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 static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.NotificationService;
19 import org.opendaylight.mdsal.binding.api.WriteTransaction;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.CarPeople;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPerson;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPersonBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.people.rev140818.car.people.CarPersonKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarBought;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.purchase.rev140818.CarPurchaseListener;
28 import org.opendaylight.yangtools.concepts.Registration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
33 import org.osgi.service.component.annotations.Reference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 @Component(service = { })
39 public final class PeopleCarListener implements CarPurchaseListener {
40 private static final Logger LOG = LoggerFactory.getLogger(PeopleCarListener.class);
42 private final DataBroker dataProvider;
43 private final Registration reg;
47 public PeopleCarListener(@Reference final DataBroker dataProvider,
48 @Reference final NotificationService notifService) {
49 this.dataProvider = requireNonNull(dataProvider);
50 reg = notifService.registerNotificationListener(this);
60 public void onCarBought(final CarBought notification) {
62 final CarPerson carPerson = new CarPersonBuilder()
63 .withKey(new CarPersonKey(notification.getCarId(), notification.getPersonId()))
66 LOG.info("Car bought, adding car-person entry: [{}]", carPerson);
68 InstanceIdentifier<CarPerson> carPersonIId = InstanceIdentifier.builder(CarPeople.class)
69 .child(CarPerson.class, carPerson.key()).build();
72 WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
73 tx.put(LogicalDatastoreType.CONFIGURATION, carPersonIId, carPerson);
75 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
77 public void onSuccess(final CommitInfo result) {
78 LOG.info("Successfully added car-person entry: [{}]", carPerson);
82 public void onFailure(final Throwable ex) {
83 LOG.error("Failed to add car-person entry: [{}]", carPerson, ex);
85 }, MoreExecutors.directExecutor());