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