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