ce9c89eacf68746c59b62b5eeebd776f18da6548
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / intenthandler / listener / VbridgeTopologyListener.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc.  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.lispflowmapping.neutron.intenthandler.listener;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import javax.annotation.Nonnull;
15 import org.checkerframework.checker.lock.qual.GuardedBy;
16 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.mdsal.binding.api.MountPointService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vbridge.topology.rev160129.network.topology.topology.topology.types.VbridgeTopology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Created by Shakib Ahmed on 1/19/17.
30  */
31 public class VbridgeTopologyListener implements ClusteredDataTreeChangeListener<VbridgeTopology>, AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(VbridgeTopologyListener.class);
33
34     private final DataBroker dataBroker;
35     private final MountPointService mountPointService;
36
37     @GuardedBy("this")
38     private final Map<TopologyKey, VppEndpointListener> domains = new ConcurrentHashMap<>();
39
40     public VbridgeTopologyListener(final DataBroker dataBroker,
41                                    final MountPointService mountPointService) {
42         this.dataBroker = Preconditions.checkNotNull(dataBroker);
43         this.mountPointService = Preconditions.checkNotNull(mountPointService);
44     }
45
46     @Override
47     public synchronized void onDataTreeChanged(@Nonnull Collection<DataTreeModification<VbridgeTopology>> changes) {
48         for (DataTreeModification<VbridgeTopology> topologyData : changes) {
49             final KeyedInstanceIdentifier<Topology, TopologyKey> topologyInstanceIdentifier =
50                     (KeyedInstanceIdentifier<Topology, TopologyKey>) topologyData
51                                                                         .getRootPath()
52                                                                         .getRootIdentifier()
53                                                                         .firstIdentifierOf(Topology.class);
54
55             Preconditions.checkArgument(!topologyInstanceIdentifier
56                     .isWildcarded(), "Wildcard topology %s is not supported",
57                     topologyInstanceIdentifier);
58
59             final DataObjectModification<VbridgeTopology> modification =  topologyData.getRootNode();
60
61             switch (modification.getModificationType()) {
62                 case DELETE:
63                     handleVbridgeTopologyDelete(topologyInstanceIdentifier);
64                     break;
65                 case WRITE:
66                     handleVbridgeTopologyWrite(topologyInstanceIdentifier);
67                     break;
68                 default:
69                     LOG.warn("Ignoring unhandled modification type {}", modification.getModificationType());
70                     break;
71
72             }
73         }
74     }
75
76     private void handleVbridgeTopologyDelete(KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
77         VppEndpointListener endpointListener = domains.get(topology.getKey());
78         endpointListener.close();
79         domains.remove(topology.getKey());
80     }
81
82     private void handleVbridgeTopologyWrite(KeyedInstanceIdentifier<Topology, TopologyKey> topology) {
83         if (domains.containsKey(topology.getKey())) {
84             domains.get(topology.getKey()).close();
85             domains.remove(topology.getKey());
86         }
87         domains.put(topology.getKey(), new VppEndpointListener(dataBroker, mountPointService, topology));
88     }
89
90     @Override
91     public void close() {
92         domains.forEach((topologyKey, vppEndpointListener) -> {
93             vppEndpointListener.close();
94         });
95     }
96 }