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