94d46c18cc676a586e1579f923848713aa1a5354
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / monitoring / MonitoringSession.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 package org.opendaylight.netconf.test.tool.monitoring;
9
10 import com.google.common.base.Joiner;
11 import javax.xml.bind.annotation.XmlElement;
12 import javax.xml.bind.annotation.XmlTransient;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.Session1;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
16 import org.opendaylight.yangtools.yang.common.QName;
17
18 final class MonitoringSession {
19
20     @XmlTransient
21     private Session managementSession;
22
23     MonitoringSession(final Session managementSession) {
24         this.managementSession = managementSession;
25     }
26
27     MonitoringSession() {
28     }
29
30     public void setManagementSession(final Session managementSession) {
31         this.managementSession = managementSession;
32     }
33
34     @XmlElement(name = "session-id")
35     public long getId() {
36         return managementSession.getSessionId().longValue();
37     }
38
39     @XmlElement(name = "source-host")
40     public String getSourceHost() {
41         final IpAddress ipAddress = managementSession.getSourceHost().getIpAddress();
42         if (ipAddress.getIpv4Address() != null) {
43             return ipAddress.getIpv4Address().getValue();
44         } else {
45             return ipAddress.getIpv6Address().getValue();
46         }
47     }
48
49     @XmlElement(name = "login-time")
50     public String getLoginTime() {
51         return managementSession.getLoginTime().getValue();
52     }
53
54     @XmlElement(name = "in-bad-rpcs")
55     public Long getInBadRpcs() {
56         return managementSession.getInBadRpcs().getValue().longValue();
57     }
58
59     @XmlElement(name = "in-rpcs")
60     public Long getInRpcs() {
61         return managementSession.getInRpcs().getValue().longValue();
62     }
63
64     @XmlElement(name = "out-notifications")
65     public Long getOutNotifications() {
66         return managementSession.getOutNotifications().getValue().longValue();
67     }
68
69     @XmlElement(name = "out-rpc-errors")
70     public Long getOutRpcErrors() {
71         return managementSession.getOutRpcErrors().getValue().longValue();
72     }
73
74     @XmlElement(name = "transport")
75     public String getTransport() {
76         try {
77             final QName qualifiedName = (QName) managementSession.getTransport().getField("QNAME").get(null);
78             // Add extension prefix if transport type is from extension yang module
79             if (qualifiedName.getNamespace().toString().equals(MonitoringConstants.EXTENSION_NAMESPACE)) {
80                 return Joiner.on(':').join(MonitoringConstants.EXTENSION_NAMESPACE_PREFIX,
81                         qualifiedName.getLocalName());
82             } else {
83                 return qualifiedName.getLocalName();
84             }
85         } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
86             throw new IllegalArgumentException("Unknown transport type " + managementSession.getTransport(), e);
87         }
88     }
89
90     @XmlElement(name = "session-identifier", namespace = MonitoringConstants.EXTENSION_NAMESPACE)
91     public String getSessionType() {
92         return managementSession.augmentation(Session1.class).getSessionIdentifier();
93     }
94
95     @XmlElement(name = "username")
96     public String getUsername() {
97         return managementSession.getUsername();
98     }
99 }