2 * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.mount;
10 import static java.util.Objects.requireNonNull;
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;
28 // Non-final to allow mocking
29 class CallHomeMountSessionContext {
31 interface CloseCallback {
32 void onClosed(CallHomeMountSessionContext deviceContext);
35 private final NodeId nodeId;
36 private final CallHomeChannelActivator activator;
37 private final CallHomeProtocolSessionContext protocol;
38 private final CloseCallback onClose;
40 private final ContextKey key;
42 CallHomeMountSessionContext(final String nodeId, final CallHomeProtocolSessionContext protocol,
43 final CallHomeChannelActivator activator, final CloseCallback callback) {
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");
52 CallHomeProtocolSessionContext getProtocol() {
60 ContextKey getContextKey() {
64 Node getConfigNode() {
65 return new NodeBuilder()
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")
75 .setSchemaless(Boolean.FALSE)
80 @SuppressWarnings("unchecked")
81 <V> Promise<V> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
82 return (Promise<V>) activator.activate(wrap(sessionListener));
86 public String toString() {
87 return MoreObjects.toStringHelper(this)
88 .add("address", protocol.getRemoteAddress())
89 .add("hostKey", protocol.getRemoteServerKey())
93 private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
94 return new NetconfClientSessionListener() {
96 public void onSessionUp(final NetconfClientSession session) {
97 delegate.onSessionUp(session);
101 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
103 delegate.onSessionTerminated(session, reason);
110 public void onSessionDown(final NetconfClientSession session, final Exception exc) {
114 delegate.onSessionDown(session, exc);
119 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
120 delegate.onMessage(session, message);
125 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
126 justification = "https://github.com/spotbugs/spotbugs/issues/811")
127 private void removeSelf() {
128 onClose.onClosed(this);