67024f910b005a9c8d4c28742c87761a4516c454
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfCommandUtils.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.netconf.console.commands;
10
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import com.google.common.base.Strings;
15
16 public class NetconfCommandUtils {
17
18     private static final Pattern IP_PATTERN = Pattern.compile(
19             "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
20                     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
21                     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
22                     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
23
24     public static boolean isPortValid(final String devicePort) {
25         if (Strings.isNullOrEmpty(devicePort)) {
26             return false;
27         }
28         Integer port = Integer.parseInt(devicePort);
29         if (port != null && port >= 0 && port <= 65535) {
30             return true;
31         }
32         return false;
33     }
34
35     public static boolean isIpValid(final String deviceIp) {
36         if (Strings.isNullOrEmpty(deviceIp)) {
37             return false;
38         }
39         Matcher matcher = IP_PATTERN.matcher(deviceIp);
40         return matcher.matches();
41     }
42 }