Migrate netconf-topology to new transport
[netconf.git] / apps / 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 package org.opendaylight.netconf.callhome.mount;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.netconf.api.NetconfTerminationReason;
15 import org.opendaylight.netconf.api.messages.NetconfMessage;
16 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
17 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
18 import org.opendaylight.netconf.client.NetconfClientSession;
19 import org.opendaylight.netconf.client.NetconfClientSessionListener;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.connection.parameters.ProtocolBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.credentials.credentials.LoginPwUnencryptedBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231025.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
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 import org.opendaylight.yangtools.yang.common.Decimal64;
29 import org.opendaylight.yangtools.yang.common.Uint16;
30 import org.opendaylight.yangtools.yang.common.Uint32;
31
32 // Non-final to allow mocking
33 class CallHomeMountSessionContext {
34     @FunctionalInterface
35     interface CloseCallback {
36         void onClosed(CallHomeMountSessionContext deviceContext);
37     }
38
39     private final NodeId nodeId;
40     private final CallHomeChannelActivator activator;
41     private final CallHomeProtocolSessionContext protocol;
42     private final CloseCallback onClose;
43     // FIXME: Remove this
44     private final ContextKey key;
45
46     CallHomeMountSessionContext(final String nodeId, final CallHomeProtocolSessionContext protocol,
47                                 final CallHomeChannelActivator activator, final CloseCallback callback) {
48
49         this.nodeId = new NodeId(requireNonNull(nodeId, "nodeId"));
50         key = ContextKey.from(protocol.getRemoteAddress());
51         this.protocol = requireNonNull(protocol, "protocol");
52         this.activator = requireNonNull(activator, "activator");
53         onClose = requireNonNull(callback, "callback");
54     }
55
56     CallHomeProtocolSessionContext getProtocol() {
57         return protocol;
58     }
59
60     NodeId getId() {
61         return nodeId;
62     }
63
64     ContextKey getContextKey() {
65         return key;
66     }
67
68     /**
69      * Create device default configuration.
70      *
71      * <p>
72      * This configuration is a replacement of configuration device data
73      * which is normally stored in configuration datastore but is absent for call-home devices.
74      *
75      * @return {@link Node} containing the default device configuration
76      */
77     // FIXME make these defaults tune-able in odl-netconf-callhome-server
78     Node getConfigNode() {
79         return new NodeBuilder()
80                 .setNodeId(getId())
81                 .addAugmentation(new NetconfNodeBuilder()
82                         .setHost(new Host(key.getIpAddress()))
83                         .setPort(key.getPort())
84                         .setTcpOnly(false)
85                         .setProtocol(new ProtocolBuilder()
86                                 .setName(protocol.getTransportType())
87                                 .build())
88                         .setSchemaless(false)
89                         .setReconnectOnChangedSchema(false)
90                         .setConnectionTimeoutMillis(Uint32.valueOf(20000))
91                         .setDefaultRequestTimeoutMillis(Uint32.valueOf(60000))
92                         .setMaxConnectionAttempts(Uint32.ZERO)
93                         .setBetweenAttemptsTimeoutMillis(Uint16.valueOf(2000))
94                         .setSleepFactor(Decimal64.valueOf("1.5"))
95                         .setKeepaliveDelay(Uint32.valueOf(120))
96                         .setConcurrentRpcLimit(Uint16.ZERO)
97                         .setActorResponseWaitTime(Uint16.valueOf(5))
98                         // the real call-home device credentials are applied in CallHomeAuthProviderImpl
99                         .setCredentials(new LoginPwUnencryptedBuilder()
100                             .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
101                                 .setUsername("omitted")
102                                 .setPassword("omitted")
103                                 .build())
104                             .build())
105                         .setLockDatastore(true)
106                     .build())
107                 .build();
108     }
109
110     ListenableFuture<NetconfClientSession> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
111         return activator.activate(wrap(sessionListener));
112     }
113
114     @Override
115     public String toString() {
116         return MoreObjects.toStringHelper(this)
117                 .add("address", protocol.getRemoteAddress())
118                 .add("hostKey", protocol.getRemoteServerKey())
119                 .toString();
120     }
121
122     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
123         return new NetconfClientSessionListener() {
124             @Override
125             public void onSessionUp(final NetconfClientSession session) {
126                 delegate.onSessionUp(session);
127             }
128
129             @Override
130             public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
131                 try {
132                     delegate.onSessionTerminated(session, reason);
133                 } finally {
134                     removeSelf();
135                 }
136             }
137
138             @Override
139             public void onSessionDown(final NetconfClientSession session, final Exception exc) {
140                 try {
141                     removeSelf();
142                 } finally {
143                     delegate.onSessionDown(session, exc);
144                 }
145             }
146
147             @Override
148             public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
149                 delegate.onMessage(session, message);
150             }
151
152             @Override
153             public void onError(final NetconfClientSession session, final Exception failure) {
154                 delegate.onError(session, failure);
155             }
156         };
157     }
158
159     private void removeSelf() {
160         onClose.onClosed(this);
161     }
162 }