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