Rework createReconnectingClient()
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / CallHomeMountSessionContext.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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 package org.opendaylight.netconf.callhome.mount;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import io.netty.util.concurrent.Promise;
15 import org.opendaylight.netconf.api.NetconfMessage;
16 import org.opendaylight.netconf.api.NetconfTerminationReason;
17 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
18 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
19 import org.opendaylight.netconf.client.NetconfClientSession;
20 import org.opendaylight.netconf.client.NetconfClientSessionListener;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
27
28 // Non-final to allow mocking
29 class CallHomeMountSessionContext {
30     @FunctionalInterface
31     interface CloseCallback {
32         void onClosed(CallHomeMountSessionContext deviceContext);
33     }
34
35     private final NodeId nodeId;
36     private final CallHomeChannelActivator activator;
37     private final CallHomeProtocolSessionContext protocol;
38     private final CloseCallback onClose;
39     // FIXME: Remove this
40     private final ContextKey key;
41
42     CallHomeMountSessionContext(final String nodeId, final CallHomeProtocolSessionContext protocol,
43                                 final CallHomeChannelActivator activator, final CloseCallback callback) {
44
45         this.nodeId = new NodeId(requireNonNull(nodeId, "nodeId"));
46         this.key = ContextKey.from(protocol.getRemoteAddress());
47         this.protocol = requireNonNull(protocol, "protocol");
48         this.activator = requireNonNull(activator, "activator");
49         this.onClose = requireNonNull(callback, "callback");
50     }
51
52     CallHomeProtocolSessionContext getProtocol() {
53         return protocol;
54     }
55
56     NodeId getId() {
57         return nodeId;
58     }
59
60     ContextKey getContextKey() {
61         return key;
62     }
63
64     Node getConfigNode() {
65         return new NodeBuilder()
66                 .setNodeId(getId())
67                 .addAugmentation(new NetconfNodeBuilder()
68                     .setHost(new Host(key.getIpAddress()))
69                     .setPort(key.getPort())
70                     .setTcpOnly(Boolean.FALSE)
71                     .setCredentials(new LoginPasswordBuilder()
72                         .setUsername("ommited")
73                         .setPassword("ommited")
74                         .build())
75                     .setSchemaless(Boolean.FALSE)
76                     .build())
77                 .build();
78     }
79
80     Promise<NetconfClientSession> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
81         return activator.activate(wrap(sessionListener));
82     }
83
84     @Override
85     public String toString() {
86         return MoreObjects.toStringHelper(this)
87                 .add("address", protocol.getRemoteAddress())
88                 .add("hostKey", protocol.getRemoteServerKey())
89                 .toString();
90     }
91
92     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
93         return new NetconfClientSessionListener() {
94             @Override
95             public void onSessionUp(final NetconfClientSession session) {
96                 delegate.onSessionUp(session);
97             }
98
99             @Override
100             public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
101                 try {
102                     delegate.onSessionTerminated(session, reason);
103                 } finally {
104                     removeSelf();
105                 }
106             }
107
108             @Override
109             public void onSessionDown(final NetconfClientSession session, final Exception exc) {
110                 try {
111                     removeSelf();
112                 } finally {
113                     delegate.onSessionDown(session, exc);
114                 }
115             }
116
117             @Override
118             public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
119                 delegate.onMessage(session, message);
120             }
121         };
122     }
123
124     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
125             justification = "https://github.com/spotbugs/spotbugs/issues/811")
126     private void removeSelf() {
127         onClose.onClosed(this);
128     }
129 }