a59c996274a5245b5e9ed8931ace6a1e07d7ae0d
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.iaas.impl;
24
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 /**
29  * This class is used to parse the VM URL returned from OpenStack and extract all of the constituent parts.
30  */
31 public class VMURL {
32
33     /**
34      * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
35      * various component parts of the URL.
36      */
37     private static Pattern pattern = Pattern
38         .compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?/v2/([^/]+)/servers/([^/]+)");
39
40     /**
41      * The URL scheme or protocol, such as HTTP or HTTPS
42      */
43     private String scheme;
44
45     /**
46      * The host name or ip address
47      */
48     private String host;
49
50     /**
51      * The port number, or null if no port is defined
52      */
53     private String port;
54
55     /**
56      * The tenant UUID
57      */
58     private String tenantId;
59
60     /**
61      * The server UUID
62      */
63     private String serverId;
64
65     /**
66      * A private default constructor prevents instantiation by any method other than the factory method
67      * 
68      * @see #parseURL(String)
69      */
70     private VMURL() {
71
72     }
73
74     /**
75      * This static method is used to parse the provided server URL string and return a parse results object (VMURL)
76      * which represents the state of the parse.
77      * 
78      * @param serverUrl
79      *            The server URL to be parsed
80      * @return The VMURL parse results object, or null if the URL was not valid or null.
81      */
82     public static VMURL parseURL(String serverUrl) {
83         VMURL obj = null;
84         if (serverUrl != null) {
85             Matcher matcher = pattern.matcher(serverUrl.trim());
86             if (matcher.matches()) {
87                 obj = new VMURL();
88                 obj.scheme = matcher.group(1);
89                 obj.host = matcher.group(2);
90                 obj.port = matcher.group(3);
91                 obj.tenantId = matcher.group(4);
92                 obj.serverId = matcher.group(5);
93             }
94         }
95
96         return obj;
97     }
98
99     /**
100      * @return The URL scheme
101      */
102     public String getScheme() {
103         return scheme;
104     }
105
106     /**
107      * @return The URL host
108      */
109     public String getHost() {
110         return host;
111     }
112
113     /**
114      * @return The URL port, or null if no port was defined
115      */
116     public String getPort() {
117         return port;
118     }
119
120     /**
121      * @return The tenant id
122      */
123     public String getTenantId() {
124         return tenantId;
125     }
126
127     /**
128      * @return The server ID
129      */
130     public String getServerId() {
131         return serverId;
132     }
133
134     @Override
135     public String toString() {
136         return String.format("%s://%s:%s/%s/servers/%s", scheme, host, port, tenantId, serverId);
137     }
138
139 }