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
9 package org.opendaylight.controller.netconf.util.messages;
11 import com.google.common.base.Preconditions;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
17 * Additional header can be used with hello message to carry information about
18 * session's connection. Provided information can be reported via netconf
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
25 * transport - tcp, ssh
26 * session-identifier - persister, client
27 * Session-identifier is optional, others mandatory.
29 * This header is inserted in front of a netconf hello message followed by a newline.
31 public class NetconfHelloMessageAdditionalHeader {
33 private static final String SC = ";";
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;
41 public NetconfHelloMessageAdditionalHeader(String userName, String hostAddress, String port, String transport, String sessionIdentifier) {
42 this.userName = userName;
43 this.hostAddress = hostAddress;
45 this.transport = transport;
46 this.sessionIdentifier = sessionIdentifier;
49 public String getUserName() {
53 public String getAddress() {
57 public String getPort() {
61 public String getTransport() {
65 public String getSessionIdentifier() {
66 return sessionIdentifier;
70 * Format additional header into a string suitable as a prefix for netconf hello message
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();
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('\'');
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]+)[^\\]]+\\]");
101 * Parse additional header from a formatted string
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);
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");
118 return new NetconfHelloMessageAdditionalHeader(username, address, port, transport, sessionIdentifier);