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