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