0eae16cd1837397d8cb529b303360cb132b01ebf
[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.Map;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.groupbasedpolicy.util.IidFactory;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27
28 public class EndpointManagerListener implements DataChangeListener, AutoCloseable {
29
30     private final ListenerRegistration<DataChangeListener> registerListener;
31     private final EndpointManager endpointManager;
32
33     public EndpointManagerListener(DataBroker dataProvider, EndpointManager endpointManager) {
34         this.endpointManager = checkNotNull(endpointManager);
35         this.registerListener = checkNotNull(dataProvider).registerDataChangeListener(
36                 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
37     }
38
39     @Override
40     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
41         //Create
42         for (DataObject dao : change.getCreatedData().values()) {
43             if (dao instanceof Endpoint) {
44                 endpointManager.processEndpoint(null, (Endpoint) dao);
45             } else if (dao instanceof EndpointL3) {
46                 endpointManager.processL3Endpoint(null, (EndpointL3) dao);
47             } else if (dao instanceof EndpointL3Prefix) {
48                 //noinspection UnnecessaryContinue
49                 continue;
50             }
51         }
52         //Update
53         Map<InstanceIdentifier<?>, DataObject> dao = change.getUpdatedData();
54         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : dao.entrySet()) {
55             if (entry.getValue() instanceof Endpoint) {
56                 Endpoint oldEp = (Endpoint) change.getOriginalData().get(entry.getKey());
57                 endpointManager.processEndpoint(oldEp, (Endpoint) entry.getValue());
58             } else if (entry.getValue() instanceof EndpointL3) {
59                 EndpointL3 oldEp3 = (EndpointL3) change.getOriginalData().get(entry.getKey());
60                 endpointManager.processL3Endpoint(oldEp3, (EndpointL3) entry.getValue());
61             } else if (entry.getValue() instanceof EndpointL3Prefix) {
62                 //noinspection UnnecessaryContinue
63                 continue;
64             }
65         }
66         //Remove
67         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
68             DataObject old = change.getOriginalData().get(iid);
69             if (old == null) {
70                 continue;
71             }
72             if (old instanceof Endpoint) {
73                 endpointManager.processEndpoint((Endpoint) old, null);
74             } else if (old instanceof EndpointL3) {
75                 endpointManager.processL3Endpoint((EndpointL3) old, null);
76             } else if (old instanceof EndpointL3Prefix) {
77                 //noinspection UnnecessaryContinue
78                 continue;
79             }
80         }
81     }
82
83     @Override
84     public void close() throws Exception {
85         if (registerListener != null)
86             registerListener.close();
87     }
88 }