Merge "Fixed bug in cross-broker RPC routing"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionNegotiator.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.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import io.netty.channel.Channel;
14 import io.netty.util.Timer;
15 import io.netty.util.concurrent.Promise;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
18 import org.opendaylight.controller.netconf.util.AbstractNetconfSessionNegotiator;
19 import org.opendaylight.protocol.framework.SessionListener;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 public class NetconfServerSessionNegotiator extends
27         AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession> {
28
29     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
30
31     private static final AdditionalHeader DEFAULT_HEADER = new AdditionalHeader();
32
33     protected NetconfServerSessionNegotiator(NetconfServerSessionPreferences sessionPreferences,
34             Promise<NetconfServerSession> promise, Channel channel, Timer timer, SessionListener sessionListener) {
35         super(sessionPreferences, promise, channel, timer, sessionListener);
36     }
37
38     @Override
39     protected NetconfServerSession getSession(SessionListener sessionListener, Channel channel, NetconfMessage message) {
40         Optional<String> additionalHeader = message.getAdditionalHeader();
41
42         AdditionalHeader parsedHeader;
43         if (additionalHeader.isPresent()) {
44             parsedHeader = new AdditionalHeader(additionalHeader.get());
45         } else {
46             parsedHeader = DEFAULT_HEADER;
47         }
48         logger.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
49
50         return new NetconfServerSession(sessionListener, channel, sessionPreferences.getSessionId(), parsedHeader);
51     }
52
53     static class AdditionalHeader {
54
55         private static final Pattern pattern = Pattern
56                 .compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+)[^\\]]+\\]");
57         private final String username;
58         private final String address;
59         private final String transport;
60
61         public AdditionalHeader(String addHeaderAsString) {
62             addHeaderAsString = addHeaderAsString.trim();
63             Matcher matcher = pattern.matcher(addHeaderAsString);
64             Preconditions.checkArgument(matcher.matches(), "Additional header in wrong format %s, expected %s",
65                     addHeaderAsString, pattern);
66             this.username = matcher.group("username");
67             this.address = matcher.group("address");
68             this.transport = matcher.group("transport");
69         }
70
71         private AdditionalHeader() {
72             this.username = this.address = "unknown";
73             this.transport = "ssh";
74         }
75
76         String getUsername() {
77             return username;
78         }
79
80         String getAddress() {
81             return address;
82         }
83
84         String getTransport() {
85             return transport;
86         }
87
88         @Override
89         public String toString() {
90             final StringBuffer sb = new StringBuffer("AdditionalHeader{");
91             sb.append("username='").append(username).append('\'');
92             sb.append(", address='").append(address).append('\'');
93             sb.append(", transport='").append(transport).append('\'');
94             sb.append('}');
95             return sb.toString();
96         }
97     }
98 }