0b5999df8087cf686035be0aa844a65042f9e44a
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasEndpointManagerListener.java
1 package org.opendaylight.groupbasedpolicy.renderer.faas;
2
3 /*
4  * Copyright (c) 2015 Huawei Technologies and others. All rights reserved.
5  * 
6  * This program and the accompanying materials are made available under the
7  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
8  * and is available at http://www.eclipse.org/legal/epl-v10.html
9  */
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.util.IidFactory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class FaasEndpointManagerListener implements DataChangeListener, AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(FaasEndpointManagerListener.class);
32
33     private final ListenerRegistration<DataChangeListener> registerListener;
34
35     public FaasEndpointManagerListener(DataBroker dataProvider) {
36         this.registerListener = checkNotNull(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
37                 IidFactory.endpointsIidWildcard(), this, AsyncDataBroker.DataChangeScope.SUBTREE);
38     }
39
40     @Override
41     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
42         // Create
43         for (DataObject dao : change.getCreatedData().values()) {
44             if (dao instanceof Endpoint) {
45                 LOG.debug("Created Endpoint {}", (Endpoint) dao);
46             } else if (dao instanceof EndpointL3) {
47                 LOG.debug("Created EndpointL3 {}", (EndpointL3) dao);
48             } else if (dao instanceof EndpointL3Prefix) {
49                 LOG.debug("Created EndpointL3Prefix {}", (EndpointL3Prefix) dao);
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                 LOG.debug("Updated Endpoint {}", (Endpoint) dao);
57             } else if (entry.getValue() instanceof EndpointL3) {
58                 LOG.debug("Updated EndpointL3 {}", (EndpointL3) dao);
59             } else if (entry.getValue() instanceof EndpointL3Prefix) {
60                 LOG.debug("Updated EndpointL3Prefix {}", (EndpointL3Prefix) dao);
61             }
62         }
63         // Remove
64         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
65             DataObject old = change.getOriginalData().get(iid);
66             if (old == null) {
67                 continue;
68             }
69             if (old instanceof Endpoint) {
70                 LOG.debug("Removed Endpoint {}", (Endpoint) old);
71             } else if (old instanceof EndpointL3) {
72                 LOG.debug("Removed EndpointL3 {}", (EndpointL3) old);
73             } else if (old instanceof EndpointL3Prefix) {
74                 LOG.debug("Removed EndpointL3Prefix {}", (EndpointL3Prefix) old);
75             }
76         }
77     }
78
79     @Override
80     public void close() throws Exception {
81         if (registerListener != null)
82             registerListener.close();
83     }
84 }