e9c34d6f87378b8a00b059c1249f59a602a26794
[netvirt.git] / coe / impl / src / main / java / org / opendaylight / netvirt / coe / listeners / ServiceListener.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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.netvirt.coe.listeners;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12
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;
34
35 @Singleton
36 public class ServiceListener implements DataTreeChangeListener<Services> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
39
40     private final CoeUtils coeUtils;
41     private final ManagedNewTransactionRunner txRunner;
42
43     private ListenerRegistration<ServiceListener> listenerRegistration;
44
45     @Inject
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);
50     }
51
52     protected InstanceIdentifier<Services> getWildCardPath() {
53         return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
54     }
55
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);
59     }
60
61     @PreDestroy
62     public void close() {
63         if (listenerRegistration != null) {
64             try {
65                 listenerRegistration.close();
66             } finally {
67                 listenerRegistration = null;
68             }
69         }
70     }
71
72     @Override
73     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
74         for (DataTreeModification<Services> change : changes) {
75             final DataObjectModification<Services> mod = change.getRootNode();
76
77             switch (mod.getModificationType()) {
78                 case DELETE:
79                     LOG.info("Service deleted {}", mod.getDataBefore());
80                     break;
81                 case SUBTREE_MODIFIED:
82                     LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
83                     add(mod.getDataAfter());
84                     break;
85                 case WRITE:
86                     if (mod.getDataBefore() == null) {
87                         LOG.info("Service added {}", mod.getDataAfter());
88                     } else {
89                         LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
90                     }
91                     add(mod.getDataAfter());
92                     break;
93                 default:
94                     // FIXME: May be not a good idea to throw.
95                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
96             }
97         }
98     }
99
100     void add(Services services) {
101         txRunner.callWithNewReadWriteTransactionAndSubmit(
102                 CONFIGURATION, tx -> coeUtils.updateVpnInterfaceWithExtraRouteAdjacency(tx, services));
103     }
104 }