BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[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
9 package org.opendaylight.netconf.callhome.mount;
10
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;
28
29 class CallHomeMountSessionContext {
30
31     public interface CloseCallback {
32
33         void onClosed(CallHomeMountSessionContext deviceContext);
34
35     }
36
37     private final NodeId nodeId;
38     private final CallHomeChannelActivator activator;
39     private final CallHomeProtocolSessionContext protocol;
40     private final CloseCallback onClose;
41     // FIXME: Remove this
42     private final ContextKey key;
43
44     CallHomeMountSessionContext(String nodeId, CallHomeProtocolSessionContext protocol,
45             CallHomeChannelActivator activator, CloseCallback callback) {
46
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");
52     }
53
54     NodeId getId() {
55         return nodeId;
56     }
57
58     public ContextKey getContextKey() {
59         return key;
60     }
61
62     Node getConfigNode() {
63         NodeBuilder builder = new NodeBuilder();
64
65         return builder.setNodeId(getId()).addAugmentation(NetconfNode.class, configNetconfNode()).build();
66
67     }
68
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);
76         return node.build();
77     }
78
79     @SuppressWarnings("unchecked")
80      <V> Promise<V> activateNetconfChannel(NetconfClientSessionListener sessionListener) {
81         return (Promise<V>) activator.activate(wrap(sessionListener));
82     }
83
84     @SuppressWarnings("deprecation")
85     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
86         return new NetconfClientSessionListener() {
87
88             @Override
89             public void onSessionUp(NetconfClientSession session) {
90                 delegate.onSessionUp(session);
91             }
92
93             @Override
94             public void onSessionTerminated(NetconfClientSession session, NetconfTerminationReason reason) {
95                 try {
96                     delegate.onSessionTerminated(session, reason);
97                 } finally {
98                     removeSelf();
99                 }
100             }
101
102             @Override
103             public void onSessionDown(NetconfClientSession session, Exception e) {
104                 try {
105                     removeSelf();
106                 } finally {
107                     delegate.onSessionDown(session, e);
108                 }
109             }
110
111             @Override
112             public void onMessage(NetconfClientSession session, NetconfMessage message) {
113                 delegate.onMessage(session, message);
114             }
115         };
116     }
117
118     private void removeSelf() {
119         onClose.onClosed(this);
120     }
121
122     InetSocketAddress getRemoteAddress() {
123         return protocol.getRemoteAddress();
124     }
125
126     PublicKey getRemoteServerKey() {
127         return protocol.getRemoteServerKey();
128     }
129
130 }