Update MRI projects for Aluminium
[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     @SuppressWarnings("unchecked")
81     <V> Promise<V> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
82         return (Promise<V>) activator.activate(wrap(sessionListener));
83     }
84
85     @Override
86     public String toString() {
87         return MoreObjects.toStringHelper(this)
88                 .add("address", protocol.getRemoteAddress())
89                 .add("hostKey", protocol.getRemoteServerKey())
90                 .toString();
91     }
92
93     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
94         return new NetconfClientSessionListener() {
95             @Override
96             public void onSessionUp(final NetconfClientSession session) {
97                 delegate.onSessionUp(session);
98             }
99
100             @Override
101             public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
102                 try {
103                     delegate.onSessionTerminated(session, reason);
104                 } finally {
105                     removeSelf();
106                 }
107             }
108
109             @Override
110             public void onSessionDown(final NetconfClientSession session, final Exception exc) {
111                 try {
112                     removeSelf();
113                 } finally {
114                     delegate.onSessionDown(session, exc);
115                 }
116             }
117
118             @Override
119             public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
120                 delegate.onMessage(session, message);
121             }
122         };
123     }
124
125     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
126             justification = "https://github.com/spotbugs/spotbugs/issues/811")
127     private void removeSelf() {
128         onClose.onClosed(this);
129     }
130 }