Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / pipeline / ClusteredNetconfDeviceCommunicator.java
1 /*
2  * Copyright (c) 2015 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.netconf.topology.pipeline;
10
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;
21
22 public class ClusteredNetconfDeviceCommunicator extends NetconfDeviceCommunicator {
23
24     private final EntityOwnershipService ownershipService;
25
26     private final ArrayList<NetconfClientSessionListener> netconfClientSessionListeners = new ArrayList<>();
27     private EntityOwnershipListenerRegistration ownershipListenerRegistration = null;
28
29     public ClusteredNetconfDeviceCommunicator(RemoteDeviceId id, NetconfDevice remoteDevice, EntityOwnershipService ownershipService, final int rpcMessageLimit) {
30         super(id, remoteDevice, rpcMessageLimit);
31         this.ownershipService = ownershipService;
32     }
33
34     @Override
35     public void onMessage(NetconfClientSession session, NetconfMessage message) {
36         super.onMessage(session, message);
37         for(NetconfClientSessionListener listener : netconfClientSessionListeners) {
38             listener.onMessage(session, message);
39         }
40     }
41
42     @Override
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);
48         }
49     }
50
51     @Override
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);
57         }
58     }
59
60     @Override
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);
66         }
67     }
68
69     public NetconfClientSessionListenerRegistration registerNetconfClientSessionListener(NetconfClientSessionListener listener) {
70         netconfClientSessionListeners.add(listener);
71         return new NetconfClientSessionListenerRegistration(listener);
72     }
73
74     public class NetconfClientSessionListenerRegistration {
75
76         private final NetconfClientSessionListener listener;
77
78         public NetconfClientSessionListenerRegistration(NetconfClientSessionListener listener) {
79             this.listener = listener;
80         }
81
82         public void close() {
83             netconfClientSessionListeners.remove(listener);
84         }
85     }
86 }