Do not use toString() in looging messages
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSession.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.net.InetAddresses;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.handler.codec.ByteToMessageDecoder;
16 import io.netty.handler.codec.MessageToByteEncoder;
17 import java.net.Inet4Address;
18 import java.net.InetAddress;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.time.format.DateTimeFormatter;
23 import java.util.regex.Matcher;
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.monitoring.NetconfManagementSession;
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.notifications.NetconfNotification;
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.extension.rev131210.NetconfTcp;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.Session1;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.Session1Builder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfSsh;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Transport;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession,
50         NetconfServerSessionListener> implements NetconfManagementSession {
51
52     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSession.class);
53     private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
54     private static final String DATE_TIME_PATTERN_STRING = DateAndTime.PATTERN_CONSTANTS.get(0);
55     private static final Pattern DATE_TIME_PATTERN = Pattern.compile(DATE_TIME_PATTERN_STRING);
56
57     private final NetconfHelloMessageAdditionalHeader header;
58     private final NetconfServerSessionListener sessionListener;
59
60     private ZonedDateTime loginTime;
61     private long inRpcSuccess;
62     private long inRpcFail;
63     private long outRpcError;
64     private long outNotification;
65     private volatile boolean delayedClose;
66
67     public NetconfServerSession(final NetconfServerSessionListener sessionListener, final Channel channel,
68                                 final long sessionId, final NetconfHelloMessageAdditionalHeader header) {
69         super(sessionListener, channel, sessionId);
70         this.header = header;
71         this.sessionListener = sessionListener;
72         LOG.debug("Session {} created", this);
73     }
74
75     @Override
76     protected void sessionUp() {
77         Preconditions.checkState(loginTime == null, "Session is already up");
78         this.loginTime = Instant.now().atZone(ZoneId.systemDefault());
79         super.sessionUp();
80     }
81
82     /**
83      * Close this session after next message is sent.
84      * Suitable for close rpc that needs to send ok response before the session is closed.
85      */
86     public void delayedClose() {
87         this.delayedClose = true;
88     }
89
90     @Override
91     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
92         final ChannelFuture channelFuture = super.sendMessage(netconfMessage);
93         if (netconfMessage instanceof NetconfNotification) {
94             outNotification++;
95             sessionListener.onNotification(this, (NetconfNotification) netconfMessage);
96         }
97         // delayed close was set, close after the message was sent
98         if (delayedClose) {
99             channelFuture.addListener(future -> close());
100         }
101         return channelFuture;
102     }
103
104     public void onIncommingRpcSuccess() {
105         inRpcSuccess++;
106     }
107
108     public void onIncommingRpcFail() {
109         inRpcFail++;
110     }
111
112     public void onOutgoingRpcError() {
113         outRpcError++;
114     }
115
116     @Override
117     public Session toManagementSession() {
118         SessionBuilder builder = new SessionBuilder();
119
120         builder.setSessionId(getSessionId());
121         IpAddress address;
122         InetAddress address1 = InetAddresses.forString(header.getAddress());
123         if (address1 instanceof Inet4Address) {
124             address = new IpAddress(new Ipv4Address(header.getAddress()));
125         } else {
126             address = new IpAddress(new Ipv6Address(header.getAddress()));
127         }
128         builder.setSourceHost(new Host(address));
129
130         Preconditions.checkState(DateAndTime.PATTERN_CONSTANTS.size() == 1);
131         String formattedDateTime = DATE_FORMATTER.format(loginTime);
132
133         Matcher matcher = DATE_TIME_PATTERN.matcher(formattedDateTime);
134         Preconditions.checkState(matcher.matches(), "Formatted datetime %s does not match pattern %s",
135                 formattedDateTime, DATE_TIME_PATTERN);
136         builder.setLoginTime(new DateAndTime(formattedDateTime));
137
138         builder.setInBadRpcs(new ZeroBasedCounter32(inRpcFail));
139         builder.setInRpcs(new ZeroBasedCounter32(inRpcSuccess));
140         builder.setOutRpcErrors(new ZeroBasedCounter32(outRpcError));
141
142         builder.setUsername(header.getUserName());
143         builder.setTransport(getTransportForString(header.getTransport()));
144
145         builder.setOutNotifications(new ZeroBasedCounter32(outNotification));
146
147         builder.withKey(new SessionKey(getSessionId()));
148
149         Session1Builder builder1 = new Session1Builder();
150         builder1.setSessionIdentifier(header.getSessionIdentifier());
151         builder.addAugmentation(Session1.class, builder1.build());
152
153         return builder.build();
154     }
155
156     private static Class<? extends Transport> getTransportForString(final String transport) {
157         switch (transport) {
158             case "ssh":
159                 return NetconfSsh.class;
160             case "tcp":
161                 return NetconfTcp.class;
162             default:
163                 throw new IllegalArgumentException("Unknown transport type " + transport);
164         }
165     }
166
167     @Override
168     protected NetconfServerSession thisInstance() {
169         return this;
170     }
171
172     @Override
173     protected void addExiHandlers(final ByteToMessageDecoder decoder,
174                                   final MessageToByteEncoder<NetconfMessage> encoder) {
175         replaceMessageDecoder(decoder);
176         replaceMessageEncoderAfterNextMessage(encoder);
177     }
178
179     @Override
180     public void stopExiCommunication() {
181         replaceMessageDecoder(new NetconfXMLToMessageDecoder());
182         replaceMessageEncoderAfterNextMessage(new NetconfMessageToXMLEncoder());
183     }
184 }