Merge "CHange log level from warn to debug in ProtocolSessionPromise when connection...
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfHelloMessageAdditionalHeader.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.controller.netconf.util.messages;
10
11 import com.google.common.base.Preconditions;
12
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16 /**
17  * Additional header can be used with hello message to carry information about
18  * session's connection. Provided information can be reported via netconf
19  * monitoring.
20  * <pre>
21  * It has pattern "[username; host-address:port; transport; session-identifier;]"
22  * username - name of account on a remote
23  * host-address - client's IP address
24  * port - port number
25  * transport - tcp, ssh
26  * session-identifier - persister, client
27  * Session-identifier is optional, others mandatory.
28  * </pre>
29  * This header is inserted in front of a netconf hello message followed by a newline.
30  */
31 public class NetconfHelloMessageAdditionalHeader {
32
33     private static final String SC = ";";
34
35     private final String userName;
36     private final String hostAddress;
37     private final String port;
38     private final String transport;
39     private final String sessionIdentifier;
40
41     public NetconfHelloMessageAdditionalHeader(String userName, String hostAddress, String port, String transport, String sessionIdentifier) {
42         this.userName = userName;
43         this.hostAddress = hostAddress;
44         this.port = port;
45         this.transport = transport;
46         this.sessionIdentifier = sessionIdentifier;
47     }
48
49     public String getUserName() {
50         return userName;
51     }
52
53     public String getAddress() {
54         return hostAddress;
55     }
56
57     public String getPort() {
58         return port;
59     }
60
61     public String getTransport() {
62         return transport;
63     }
64
65     public String getSessionIdentifier() {
66         return sessionIdentifier;
67     }
68
69     /**
70      * Format additional header into a string suitable as a prefix for netconf hello message
71      */
72     public String toFormattedString() {
73         Preconditions.checkNotNull(userName);
74         Preconditions.checkNotNull(hostAddress);
75         Preconditions.checkNotNull(port);
76         Preconditions.checkNotNull(transport);
77         Preconditions.checkNotNull(sessionIdentifier);
78         return "[" + userName + SC + hostAddress + ":" + port + SC + transport + SC + sessionIdentifier + SC + "]"
79                 + System.lineSeparator();
80     }
81
82     @Override
83     public String toString() {
84         final StringBuffer sb = new StringBuffer("NetconfHelloMessageAdditionalHeader{");
85         sb.append("userName='").append(userName).append('\'');
86         sb.append(", hostAddress='").append(hostAddress).append('\'');
87         sb.append(", port='").append(port).append('\'');
88         sb.append(", transport='").append(transport).append('\'');
89         sb.append(", sessionIdentifier='").append(sessionIdentifier).append('\'');
90         sb.append('}');
91         return sb.toString();
92     }
93
94     // TODO IPv6
95     private static final Pattern pattern = Pattern
96             .compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+)[^\\]]+\\]");
97     private static final Pattern customHeaderPattern = Pattern
98             .compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+);(?<sessionIdentifier>[a-z]+)[^\\]]+\\]");
99
100     /**
101      * Parse additional header from a formatted string
102      */
103     public static NetconfHelloMessageAdditionalHeader fromString(String additionalHeader) {
104         additionalHeader = additionalHeader.trim();
105         Matcher matcher = pattern.matcher(additionalHeader);
106         Matcher matcher2 = customHeaderPattern.matcher(additionalHeader);
107         Preconditions.checkArgument(matcher.matches(), "Additional header in wrong format %s, expected %s",
108                 additionalHeader, pattern);
109
110         String username = matcher.group("username");
111         String address = matcher.group("address");
112         String port = matcher.group("port");
113         String transport = matcher.group("transport");
114         String sessionIdentifier = "client";
115         if (matcher2.matches()) {
116             sessionIdentifier = matcher2.group("sessionIdentifier");
117         }
118         return new NetconfHelloMessageAdditionalHeader(username, address, port, transport, sessionIdentifier);
119     }
120
121 }