2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.natservice.internal;
10 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
11 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.FloatingIpInfo;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.floating.ip.info.RouterPorts;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.floating.ip.info.RouterPortsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.floating.ip.info.RouterPortsKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.floating.ip.info.router.ports.PortsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.floating.ip.info.router.ports.PortsKey;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.Optional;
29 public class RouterPortsListener extends AbstractDataChangeListener<RouterPorts> implements AutoCloseable{
30 private static final Logger LOG = LoggerFactory.getLogger(RouterPortsListener.class);
31 private ListenerRegistration<DataChangeListener> listenerRegistration;
32 private final DataBroker broker;
35 public RouterPortsListener (final DataBroker db) {
36 super(RouterPorts.class);
42 public void close() throws Exception {
43 if (listenerRegistration != null) {
45 listenerRegistration.close();
46 } catch (final Exception e) {
47 LOG.error("Error when cleaning up DataChangeListener.", e);
49 listenerRegistration = null;
51 LOG.info("Router ports Listener Closed");
54 private void registerListener(final DataBroker db) {
56 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
57 getWildCardPath(), RouterPortsListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
58 } catch (final Exception e) {
59 LOG.error("RouterPorts DataChange listener registration fail!", e);
60 throw new IllegalStateException("RouterPorts Listener registration Listener failed.", e);
64 private InstanceIdentifier<RouterPorts> getWildCardPath() {
65 return InstanceIdentifier.create(FloatingIpInfo.class).child(RouterPorts.class);
70 protected void add(final InstanceIdentifier<RouterPorts> identifier, final RouterPorts routerPorts) {
71 LOG.trace("Add router ports method - key: " + identifier + ", value=" + routerPorts );
72 Optional<RouterPorts> optRouterPorts = NatUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
73 if(optRouterPorts.isPresent()) {
74 RouterPorts ports = optRouterPorts.get();
75 String routerName = ports.getRouterId();
76 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier,
77 new RouterPortsBuilder().setKey(new RouterPortsKey(routerName)).setRouterId(routerName)
78 .setExternalNetworkId(routerPorts.getExternalNetworkId()).build());
80 String routerName = routerPorts.getRouterId();
81 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, identifier,
82 new RouterPortsBuilder().setKey(new RouterPortsKey(routerName)).setRouterId(routerName)
83 .setExternalNetworkId(routerPorts.getExternalNetworkId()).build());
88 protected void remove(InstanceIdentifier<RouterPorts> identifier, RouterPorts routerPorts) {
89 LOG.trace("Remove router ports method - key: " + identifier + ", value=" + routerPorts );
90 //MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, identifier);
95 protected void update(InstanceIdentifier<RouterPorts> identifier, RouterPorts original, RouterPorts update) {
96 LOG.trace("Update router ports method - key: " + identifier + ", original=" + original + ", update=" + update );