2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.coe.listeners;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.apache.aries.blueprint.annotation.service.Reference;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.netvirt.coe.utils.CoeUtils;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.ServiceInformation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.service.information.Services;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class ServiceListener implements DataTreeChangeListener<Services> {
38 private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
40 private final CoeUtils coeUtils;
41 private final ManagedNewTransactionRunner txRunner;
43 private ListenerRegistration<ServiceListener> listenerRegistration;
46 public ServiceListener(@Reference final DataBroker dataBroker, final CoeUtils coeUtils) {
47 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
48 this.coeUtils = coeUtils;
49 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
52 protected InstanceIdentifier<Services> getWildCardPath() {
53 return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
56 public void registerListener(LogicalDatastoreType dsType, final DataBroker db) {
57 final DataTreeIdentifier<Services> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
58 listenerRegistration = db.registerDataTreeChangeListener(treeId, ServiceListener.this);
63 if (listenerRegistration != null) {
65 listenerRegistration.close();
67 listenerRegistration = null;
73 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
74 for (DataTreeModification<Services> change : changes) {
75 final DataObjectModification<Services> mod = change.getRootNode();
77 switch (mod.getModificationType()) {
79 LOG.info("Service deleted {}", mod.getDataBefore());
81 case SUBTREE_MODIFIED:
82 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
83 add(mod.getDataAfter());
86 if (mod.getDataBefore() == null) {
87 LOG.info("Service added {}", mod.getDataAfter());
89 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
91 add(mod.getDataAfter());
94 // FIXME: May be not a good idea to throw.
95 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
100 void add(Services services) {
101 txRunner.callWithNewReadWriteTransactionAndSubmit(
102 CONFIGURATION, tx -> coeUtils.updateVpnInterfaceWithExtraRouteAdjacency(tx, services));