2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.iaas.impl;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * This class is used to parse the VM URL returned from OpenStack and extract all of the constituent parts.
33 * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
34 * various component parts of the URL.
36 private static Pattern pattern = Pattern
37 .compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?/v2/([^/]+)/servers/([^/]+)");
40 * The URL scheme or protocol, such as HTTP or HTTPS
42 private String scheme;
45 * The host name or ip address
50 * The port number, or null if no port is defined
57 private String tenantId;
62 private String serverId;
65 * A private default constructor prevents instantiation by any method other than the factory method
67 * @see #parseURL(String)
74 * This static method is used to parse the provided server URL string and return a parse results object (VMURL)
75 * which represents the state of the parse.
78 * The server URL to be parsed
79 * @return The VMURL parse results object, or null if the URL was not valid or null.
81 public static VMURL parseURL(String serverUrl) {
83 if (serverUrl != null) {
84 Matcher matcher = pattern.matcher(serverUrl.trim());
85 if (matcher.matches()) {
87 obj.scheme = matcher.group(1);
88 obj.host = matcher.group(2);
89 obj.port = matcher.group(3);
90 obj.tenantId = matcher.group(4);
91 obj.serverId = matcher.group(5);
99 * @return The URL scheme
101 public String getScheme() {
106 * @return The URL host
108 public String getHost() {
113 * @return The URL port, or null if no port was defined
115 public String getPort() {
120 * @return The tenant id
122 public String getTenantId() {
127 * @return The server ID
129 public String getServerId() {
134 public String toString() {
135 return String.format("%s://%s:%s/%s/servers/%s", scheme, host, port, tenantId, serverId);