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
9 package org.opendaylight.netconf.callhome.mount;
11 import com.google.common.base.Preconditions;
12 import io.netty.util.concurrent.Promise;
13 import java.net.InetSocketAddress;
14 import java.security.PublicKey;
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.NetconfNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
29 class CallHomeMountSessionContext {
31 public interface CloseCallback {
33 void onClosed(CallHomeMountSessionContext deviceContext);
37 private final NodeId nodeId;
38 private final CallHomeChannelActivator activator;
39 private final CallHomeProtocolSessionContext protocol;
40 private final CloseCallback onClose;
42 private final ContextKey key;
44 CallHomeMountSessionContext(String nodeId, CallHomeProtocolSessionContext protocol,
45 CallHomeChannelActivator activator, CloseCallback callback) {
47 this.nodeId = new NodeId(Preconditions.checkNotNull(nodeId, "nodeId"));
48 this.key = ContextKey.from(protocol.getRemoteAddress());
49 this.protocol = Preconditions.checkNotNull(protocol, "protocol");
50 this.activator = Preconditions.checkNotNull(activator, "activator");
51 this.onClose = Preconditions.checkNotNull(callback, "callback");
58 public ContextKey getContextKey() {
62 Node getConfigNode() {
63 NodeBuilder builder = new NodeBuilder();
65 return builder.setNodeId(getId()).addAugmentation(NetconfNode.class, configNetconfNode()).build();
69 private NetconfNode configNetconfNode() {
70 NetconfNodeBuilder node = new NetconfNodeBuilder();
71 node.setHost(new Host(key.getIpAddress()));
72 node.setPort(key.getPort());
73 node.setTcpOnly(Boolean.FALSE);
74 node.setCredentials(new LoginPasswordBuilder().setUsername("ommited").setPassword("ommited").build());
75 node.setSchemaless(Boolean.FALSE);
79 @SuppressWarnings("unchecked")
80 <V> Promise<V> activateNetconfChannel(NetconfClientSessionListener sessionListener) {
81 return (Promise<V>) activator.activate(wrap(sessionListener));
84 @SuppressWarnings("deprecation")
85 private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
86 return new NetconfClientSessionListener() {
89 public void onSessionUp(NetconfClientSession session) {
90 delegate.onSessionUp(session);
94 public void onSessionTerminated(NetconfClientSession session, NetconfTerminationReason reason) {
96 delegate.onSessionTerminated(session, reason);
103 public void onSessionDown(NetconfClientSession session, Exception e) {
107 delegate.onSessionDown(session, e);
112 public void onMessage(NetconfClientSession session, NetconfMessage message) {
113 delegate.onMessage(session, message);
118 private void removeSelf() {
119 onClose.onClosed(this);
122 InetSocketAddress getRemoteAddress() {
123 return protocol.getRemoteAddress();
126 PublicKey getRemoteServerKey() {
127 return protocol.getRemoteServerKey();