ea574641927c907831fce985cedb91ca7576dfbb
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasEndpointManagerListener.java
1 /*
2  * Copyright (c) 2015 Huawei Technologies 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.faas;
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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class FaasEndpointManagerListener implements DataChangeListener, AutoCloseable {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FaasEndpointManagerListener.class);
33
34     private final ListenerRegistration<DataChangeListener> registerListener;
35
36     public FaasEndpointManagerListener(DataBroker dataProvider) {
37         this.registerListener = checkNotNull(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
38                 IidFactory.endpointsIidWildcard(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
39     }
40
41     @Override
42     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
43         // Create
44         for (DataObject dao : change.getCreatedData().values()) {
45             if (dao instanceof Endpoint) {
46                 LOG.debug("Created Endpoint {}", (Endpoint) dao);
47             } else if (dao instanceof EndpointL3) {
48                 LOG.debug("Created EndpointL3 {}", (EndpointL3) dao);
49             } else if (dao instanceof EndpointL3Prefix) {
50                 LOG.debug("Created EndpointL3Prefix {}", (EndpointL3Prefix) dao);
51             }
52         }
53         // Update
54         Map<InstanceIdentifier<?>, DataObject> dao = change.getUpdatedData();
55         for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : dao.entrySet()) {
56             if (entry.getValue() instanceof Endpoint) {
57                 LOG.debug("Updated Endpoint {}", (Endpoint) dao);
58             } else if (entry.getValue() instanceof EndpointL3) {
59                 LOG.debug("Updated EndpointL3 {}", (EndpointL3) dao);
60             } else if (entry.getValue() instanceof EndpointL3Prefix) {
61                 LOG.debug("Updated EndpointL3Prefix {}", (EndpointL3Prefix) dao);
62             }
63         }
64         // Remove
65         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
66             DataObject old = change.getOriginalData().get(iid);
67             if (old == null) {
68                 continue;
69             }
70             if (old instanceof Endpoint) {
71                 LOG.debug("Removed Endpoint {}", (Endpoint) old);
72             } else if (old instanceof EndpointL3) {
73                 LOG.debug("Removed EndpointL3 {}", (EndpointL3) old);
74             } else if (old instanceof EndpointL3Prefix) {
75                 LOG.debug("Removed EndpointL3Prefix {}", (EndpointL3Prefix) old);
76             }
77         }
78     }
79
80     @Override
81     public void close() throws Exception {
82         if (registerListener != null)
83             registerListener.close();
84     }
85 }