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;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
16 * Additional header can be used with hello message to carry information about
17 * session's connection. Provided information can be reported via netconf
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
24 * transport - tcp, ssh
25 * session-identifier - persister, client
26 * Session-identifier is optional, others mandatory.
28 * This header is inserted in front of a netconf hello message followed by a newline.
30 public class NetconfHelloMessageAdditionalHeader {
32 private static final String SC = ";";
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;
40 public NetconfHelloMessageAdditionalHeader(String userName, String hostAddress, String port, String transport, String sessionIdentifier) {
41 this.userName = userName;
42 this.hostAddress = hostAddress;
44 this.transport = transport;
45 this.sessionIdentifier = sessionIdentifier;
48 public String getUserName() {
52 public String getAddress() {
56 public String getPort() {
60 public String getTransport() {
64 public String getSessionIdentifier() {
65 return sessionIdentifier;
69 * Format additional header into a string suitable as a prefix for netconf hello message
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();
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('\'');
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]+)[^\\]]+\\]");
100 * Parse additional header from a formatted string
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);
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");
117 return new NetconfHelloMessageAdditionalHeader(username, address, port, transport, sessionIdentifier);