Convert DataChangeListeners to DataTreeChangeListeners
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / endpoint / EndpointManagerListener.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.groupbasedpolicy.renderer.ofoverlay.endpoint;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.util.IidFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25
26 public class EndpointManagerListener implements AutoCloseable {
27
28     private final List<ListenerRegistration<?>> listenerRegistrations = new ArrayList<>();
29     private final EndpointManager endpointManager;
30
31     public EndpointManagerListener(DataBroker dataProvider, EndpointManager endpointManager) {
32         this.endpointManager = checkNotNull(endpointManager);
33         checkNotNull(dataProvider);
34
35         listenerRegistrations.add(dataProvider.registerDataTreeChangeListener(new DataTreeIdentifier<>(
36             LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class)),
37             changes -> onEndpointChanged(changes)));
38
39         listenerRegistrations.add(dataProvider.registerDataTreeChangeListener(new DataTreeIdentifier<>(
40             LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(EndpointL3.class)),
41             changes -> onEndpointL3Changed(changes)));
42     }
43
44     private void onEndpointChanged(Collection<DataTreeModification<Endpoint>> changes) {
45         for (DataTreeModification<Endpoint> change: changes) {
46             DataObjectModification<Endpoint> rootNode = change.getRootNode();
47             switch (rootNode.getModificationType()) {
48                 case SUBTREE_MODIFIED:
49                 case WRITE:
50                     endpointManager.processEndpoint(rootNode.getDataBefore(), rootNode.getDataAfter());
51                     break;
52                 case DELETE:
53                     endpointManager.processEndpoint(rootNode.getDataBefore(), null);
54                     break;
55                 default:
56                     break;
57             }
58         }
59     }
60
61     private void onEndpointL3Changed(Collection<DataTreeModification<EndpointL3>> changes) {
62         for (DataTreeModification<EndpointL3> change: changes) {
63             DataObjectModification<EndpointL3> rootNode = change.getRootNode();
64             switch (rootNode.getModificationType()) {
65                 case SUBTREE_MODIFIED:
66                 case WRITE:
67                     endpointManager.processL3Endpoint(rootNode.getDataBefore(), rootNode.getDataAfter());
68                     break;
69                 case DELETE:
70                     endpointManager.processL3Endpoint(rootNode.getDataBefore(), null);
71                     break;
72                 default:
73                     break;
74             }
75         }
76     }
77
78     @Override
79     public void close() {
80         for (ListenerRegistration<?> reg: listenerRegistrations) {
81             reg.close();
82         }
83     }
84 }