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