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 / IdentityURL.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 IdentityURL {
31     /**
32      * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
33      * various component parts of the URL.
34      */
35     private static Pattern pattern = Pattern.compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?/(v[0-9\\.]+)/?");
36
37     /**
38      * The URL scheme or protocol, such as HTTP or HTTPS
39      */
40     private String scheme;
41
42     /**
43      * The host name or ip address
44      */
45     private String host;
46
47     /**
48      * The port number, or null if no port is defined
49      */
50     private String port;
51
52     /**
53      * The version of the service
54      */
55     private String version;
56
57     /**
58      * A private default constructor prevents instantiation by any method other than the factory method
59      * 
60      * @see #parseURL(String)
61      */
62     private IdentityURL() {
63
64     }
65
66     /**
67      * This static method is used to parse the provided server URL string and return a parse results object (VMURL)
68      * which represents the state of the parse.
69      * 
70      * @param serverUrl
71      *            The server URL to be parsed
72      * @return The VMURL parse results object, or null if the URL was not valid or null.
73      */
74     public static IdentityURL parseURL(String serverUrl) {
75         IdentityURL obj = null;
76         if (serverUrl != null) {
77             Matcher matcher = pattern.matcher(serverUrl.trim());
78             if (matcher.matches()) {
79                 obj = new IdentityURL();
80                 obj.scheme = matcher.group(1);
81                 obj.host = matcher.group(2);
82                 obj.port = matcher.group(3);
83                 obj.version = matcher.group(4);
84             }
85         }
86
87         return obj;
88     }
89
90     /**
91      * @return The URL scheme
92      */
93     public String getScheme() {
94         return scheme;
95     }
96
97     /**
98      * @return The URL host
99      */
100     public String getHost() {
101         return host;
102     }
103
104     /**
105      * @return The URL port, or null if no port was defined
106      */
107     public String getPort() {
108         return port;
109     }
110
111     public String getVersion() {
112         return version;
113     }
114
115     @Override
116     public String toString() {
117         return String.format("%s://%s:%s/%s", scheme, host, port, version);
118     }
119
120 }