2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.netconf.topology.pipeline;
11 import java.util.ArrayList;
12 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
13 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
14 import org.opendaylight.netconf.api.NetconfMessage;
15 import org.opendaylight.netconf.api.NetconfTerminationReason;
16 import org.opendaylight.netconf.client.NetconfClientSession;
17 import org.opendaylight.netconf.client.NetconfClientSessionListener;
18 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
19 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
20 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 public class ClusteredNetconfDeviceCommunicator extends NetconfDeviceCommunicator {
24 private final EntityOwnershipService ownershipService;
26 private final ArrayList<NetconfClientSessionListener> netconfClientSessionListeners = new ArrayList<>();
27 private EntityOwnershipListenerRegistration ownershipListenerRegistration = null;
29 public ClusteredNetconfDeviceCommunicator(RemoteDeviceId id, NetconfDevice remoteDevice, EntityOwnershipService ownershipService) {
30 super(id, remoteDevice);
31 this.ownershipService = ownershipService;
35 public void onMessage(NetconfClientSession session, NetconfMessage message) {
36 super.onMessage(session, message);
37 for(NetconfClientSessionListener listener : netconfClientSessionListeners) {
38 listener.onMessage(session, message);
43 public void onSessionDown(NetconfClientSession session, Exception e) {
44 super.onSessionDown(session, e);
45 ownershipListenerRegistration.close();
46 for(NetconfClientSessionListener listener : netconfClientSessionListeners) {
47 listener.onSessionDown(session, e);
52 public void onSessionUp(NetconfClientSession session) {
53 super.onSessionUp(session);
54 ownershipListenerRegistration = ownershipService.registerListener("netconf-node/" + id.getName(), (ClusteredNetconfDevice) remoteDevice);
55 for(NetconfClientSessionListener listener : netconfClientSessionListeners) {
56 listener.onSessionUp(session);
61 public void onSessionTerminated(NetconfClientSession session, NetconfTerminationReason reason) {
62 super.onSessionTerminated(session, reason);
63 ownershipListenerRegistration.close();
64 for(NetconfClientSessionListener listener : netconfClientSessionListeners) {
65 listener.onSessionTerminated(session, reason);
69 public NetconfClientSessionListenerRegistration registerNetconfClientSessionListener(NetconfClientSessionListener listener) {
70 netconfClientSessionListeners.add(listener);
71 return new NetconfClientSessionListenerRegistration(listener);
74 public class NetconfClientSessionListenerRegistration {
76 private final NetconfClientSessionListener listener;
78 public NetconfClientSessionListenerRegistration(NetconfClientSessionListener listener) {
79 this.listener = listener;
83 netconfClientSessionListeners.remove(listener);