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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import io.netty.util.concurrent.Promise;
14 import java.net.InetSocketAddress;
15 import java.security.PublicKey;
16 import org.opendaylight.netconf.api.NetconfMessage;
17 import org.opendaylight.netconf.api.NetconfTerminationReason;
18 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
19 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
20 import org.opendaylight.netconf.client.NetconfClientSession;
21 import org.opendaylight.netconf.client.NetconfClientSessionListener;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
30 class CallHomeMountSessionContext {
31 public 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");
56 public ContextKey getContextKey() {
60 Node getConfigNode() {
61 NodeBuilder builder = new NodeBuilder();
62 return builder.setNodeId(getId()).addAugmentation(NetconfNode.class, configNetconfNode()).build();
65 private NetconfNode configNetconfNode() {
66 NetconfNodeBuilder node = new NetconfNodeBuilder();
67 node.setHost(new Host(key.getIpAddress()));
68 node.setPort(key.getPort());
69 node.setTcpOnly(Boolean.FALSE);
70 node.setCredentials(new LoginPasswordBuilder().setUsername("ommited").setPassword("ommited").build());
71 node.setSchemaless(Boolean.FALSE);
75 @SuppressWarnings("unchecked")
76 <V> Promise<V> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
77 return (Promise<V>) activator.activate(wrap(sessionListener));
80 @SuppressWarnings("deprecation")
81 private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
82 return new NetconfClientSessionListener() {
84 public void onSessionUp(final NetconfClientSession session) {
85 delegate.onSessionUp(session);
89 public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
91 delegate.onSessionTerminated(session, reason);
98 public void onSessionDown(final NetconfClientSession session, final Exception exc) {
102 delegate.onSessionDown(session, exc);
107 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
108 delegate.onMessage(session, message);
113 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
114 justification = "https://github.com/spotbugs/spotbugs/issues/811")
115 private void removeSelf() {
116 onClose.onClosed(this);
119 InetSocketAddress getRemoteAddress() {
120 return protocol.getRemoteAddress();
123 PublicKey getRemoteServerKey() {
124 return protocol.getRemoteServerKey();