Fix formatting in callhome-provider
[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         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(String nodeId, CallHomeProtocolSessionContext protocol,
43                                 CallHomeChannelActivator activator, CloseCallback callback) {
44
45         this.nodeId = new NodeId(Preconditions.checkNotNull(nodeId, "nodeId"));
46         this.key = ContextKey.from(protocol.getRemoteAddress());
47         this.protocol = Preconditions.checkNotNull(protocol, "protocol");
48         this.activator = Preconditions.checkNotNull(activator, "activator");
49         this.onClose = Preconditions.checkNotNull(callback, "callback");
50     }
51
52     NodeId getId() {
53         return nodeId;
54     }
55
56     public ContextKey getContextKey() {
57         return key;
58     }
59
60     Node getConfigNode() {
61         NodeBuilder builder = new NodeBuilder();
62         return builder.setNodeId(getId()).addAugmentation(NetconfNode.class, configNetconfNode()).build();
63     }
64
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);
72         return node.build();
73     }
74
75     @SuppressWarnings("unchecked")
76     <V> Promise<V> activateNetconfChannel(NetconfClientSessionListener sessionListener) {
77         return (Promise<V>) activator.activate(wrap(sessionListener));
78     }
79
80     @SuppressWarnings("deprecation")
81     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
82         return new NetconfClientSessionListener() {
83             @Override
84             public void onSessionUp(NetconfClientSession session) {
85                 delegate.onSessionUp(session);
86             }
87
88             @Override
89             public void onSessionTerminated(NetconfClientSession session, NetconfTerminationReason reason) {
90                 try {
91                     delegate.onSessionTerminated(session, reason);
92                 } finally {
93                     removeSelf();
94                 }
95             }
96
97             @Override
98             public void onSessionDown(NetconfClientSession session, Exception e) {
99                 try {
100                     removeSelf();
101                 } finally {
102                     delegate.onSessionDown(session, e);
103                 }
104             }
105
106             @Override
107             public void onMessage(NetconfClientSession session, NetconfMessage message) {
108                 delegate.onMessage(session, message);
109             }
110         };
111     }
112
113     private void removeSelf() {
114         onClose.onClosed(this);
115     }
116
117     InetSocketAddress getRemoteAddress() {
118         return protocol.getRemoteAddress();
119     }
120
121     PublicKey getRemoteServerKey() {
122         return protocol.getRemoteServerKey();
123     }
124 }