2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.iaas.impl;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
31 * This class is used to parse the VM URL returned from OpenStack and extract all of the constituent parts.
36 * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
37 * various component parts of the URL.
39 private static Pattern pattern =
40 Pattern.compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/([^/]+)/servers/([^/]+)");
43 * The URL scheme or protocol, such as HTTP or HTTPS
45 private String scheme;
48 * The host name or ip address
53 * The path, or null if no path is defined
58 * The port number, or null if no port is defined
65 private String tenantId;
70 private String serverId;
73 * The version of the service
75 private String version;
78 * A private default constructor prevents instantiation by any method other than the factory method
80 * @see #parseURL(String)
87 * This static method is used to parse the provided server URL string and return a parse results object (VMURL)
88 * which represents the state of the parse.
90 * @param serverUrl The server URL to be parsed
91 * @return The VMURL parse results object, or null if the URL was not valid or null.
93 public static VMURL parseURL(String serverUrl) {
95 if (serverUrl != null) {
96 Matcher matcher = pattern.matcher(serverUrl.trim());
97 if (matcher.matches()) {
99 obj.scheme = matcher.group(1);
100 obj.host = matcher.group(2);
101 obj.port = matcher.group(3);
102 obj.path = matcher.group(4);
103 obj.version = matcher.group(5);
104 obj.tenantId = matcher.group(6);
105 obj.serverId = matcher.group(7);
113 * @return The URL scheme
115 public String getScheme() {
120 * @return The URL host
122 public String getHost() {
127 * @return THe URL path, or null if no path was defined
129 public String getPath() {
134 * @return The URL port, or null if no port was defined
136 public String getPort() {
141 * @return The tenant id
143 public String getTenantId() {
148 * @return The server ID
150 public String getServerId() {
155 * @return The version of the service
157 public String getVersion() {
162 public String toString() {
163 StringBuilder str = new StringBuilder();
164 str.append(scheme + "://" + host);
166 str.append(":" + port);
171 str.append("/" + version + "/" + tenantId + "/servers/" + serverId);
172 return str.toString();