2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.server;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
13 import com.google.common.net.InetAddresses;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.handler.codec.ByteToMessageDecoder;
17 import io.netty.handler.codec.MessageToByteEncoder;
18 import java.net.Inet4Address;
19 import java.net.InetAddress;
20 import java.time.Instant;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23 import java.time.format.DateTimeFormatter;
24 import java.util.regex.Pattern;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
27 import org.opendaylight.netconf.api.messages.NotificationMessage;
28 import org.opendaylight.netconf.nettyutil.AbstractNetconfSession;
29 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
30 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
31 import org.opendaylight.netconf.server.api.monitoring.NetconfManagementSession;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfSsh;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Transport;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionKey;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.monitoring.rev220718.NetconfTcp;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.monitoring.rev220718.Session1Builder;
45 import org.opendaylight.yangtools.yang.common.Uint32;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession,
50 NetconfServerSessionListener> implements NetconfManagementSession {
51 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSession.class);
52 private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
53 private static final Pattern DATE_TIME_PATTERN;
56 verify(DateAndTime.PATTERN_CONSTANTS.size() == 1);
57 DATE_TIME_PATTERN = Pattern.compile(DateAndTime.PATTERN_CONSTANTS.get(0));
60 private final NetconfHelloMessageAdditionalHeader header;
61 private final NetconfServerSessionListener sessionListener;
63 private ZonedDateTime loginTime;
64 private long inRpcSuccess;
65 private long inRpcFail;
66 private long outRpcError;
67 private long outNotification;
68 private volatile boolean delayedClose;
70 public NetconfServerSession(final NetconfServerSessionListener sessionListener, final Channel channel,
71 final long sessionId, final NetconfHelloMessageAdditionalHeader header) {
72 super(sessionListener, channel, sessionId);
74 this.sessionListener = sessionListener;
75 LOG.debug("Session {} created", this);
79 protected void sessionUp() {
80 checkState(loginTime == null, "Session is already up");
81 loginTime = Instant.now().atZone(ZoneId.systemDefault());
86 * Close this session after next message is sent.
87 * Suitable for close rpc that needs to send ok response before the session is closed.
89 public void delayedClose() {
94 public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
95 final ChannelFuture channelFuture = super.sendMessage(netconfMessage);
96 if (netconfMessage instanceof NotificationMessage notification) {
98 sessionListener.onNotification(this, notification);
100 // delayed close was set, close after the message was sent
102 channelFuture.addListener(future -> close());
104 return channelFuture;
107 public void onIncommingRpcSuccess() {
111 public void onIncommingRpcFail() {
115 public void onOutgoingRpcError() {
120 public Session toManagementSession() {
121 final SessionBuilder builder = new SessionBuilder()
122 .withKey(new SessionKey(Uint32.valueOf(getSessionId())));
123 final InetAddress address1 = InetAddresses.forString(header.getAddress());
124 final IpAddress address;
125 if (address1 instanceof Inet4Address) {
126 address = new IpAddress(new Ipv4Address(header.getAddress()));
128 address = new IpAddress(new Ipv6Address(header.getAddress()));
130 builder.setSourceHost(new Host(address));
132 final String formattedDateTime = DATE_FORMATTER.format(loginTime);
133 checkState(DATE_TIME_PATTERN.matcher(formattedDateTime).matches(),
134 "Formatted datetime %s does not match pattern %s", formattedDateTime, DATE_TIME_PATTERN);
137 .setLoginTime(new DateAndTime(formattedDateTime))
138 .setInBadRpcs(new ZeroBasedCounter32(Uint32.valueOf(inRpcFail)))
139 .setInRpcs(new ZeroBasedCounter32(Uint32.valueOf(inRpcSuccess)))
140 .setOutRpcErrors(new ZeroBasedCounter32(Uint32.valueOf(outRpcError)))
141 .setUsername(header.getUserName())
142 .setTransport(getTransportForString(header.getTransport()))
143 .setOutNotifications(new ZeroBasedCounter32(Uint32.valueOf(outNotification)))
144 .addAugmentation(new Session1Builder().setSessionIdentifier(header.getSessionIdentifier()).build())
148 private static Transport getTransportForString(final String transport) {
149 return switch (transport) {
150 case "ssh" -> NetconfSsh.VALUE;
151 case "tcp" -> NetconfTcp.VALUE;
152 default -> throw new IllegalArgumentException("Unknown transport type " + transport);
157 protected NetconfServerSession thisInstance() {
162 protected void addExiHandlers(final ByteToMessageDecoder decoder,
163 final MessageToByteEncoder<NetconfMessage> encoder) {
164 replaceMessageDecoder(decoder);
165 replaceMessageEncoderAfterNextMessage(encoder);
169 public void stopExiCommunication() {
170 replaceMessageDecoder(new NetconfXMLToMessageDecoder());
171 replaceMessageEncoderAfterNextMessage(new NetconfMessageToXMLEncoder());