e4d4ced6b45a1dcfef969f4d46975f320a56a70b
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / impl / IdentityURL.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.iaas.impl;
25
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 /**
30  * This class is used to parse the VM URL returned from OpenStack and extract all of the constituent parts.
31  */
32 public class IdentityURL {
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.compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/?");
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 path of the service, or null if no path is defined
51      */
52     private String path;
53
54     /**
55      * The port number, or null if no port is defined
56      */
57     private String port;
58
59     /**
60      * The version of the service
61      */
62     private String version;
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 IdentityURL() {
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 The server URL to be parsed
78      * @return The VMURL parse results object, or null if the URL was not valid or null.
79      */
80     public static IdentityURL parseURL(String identUrl) {
81         IdentityURL obj = null;
82         if (identUrl != null) {
83             Matcher matcher = pattern.matcher(identUrl.trim()); // http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3
84             if (matcher.matches()) { // (\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/?"
85                 obj = new IdentityURL();
86                 obj.scheme = matcher.group(1);
87                 obj.host = matcher.group(2);
88                 obj.port = matcher.group(3);
89                 obj.path = matcher.group(4);
90                 obj.version = matcher.group(5);
91             }
92         }
93
94         return obj;
95     }
96
97     /**
98      * @return The URL scheme
99      */
100     public String getScheme() {
101         return scheme;
102     }
103
104     /**
105      * @return The URL host
106      */
107     public String getHost() {
108         return host;
109     }
110
111     /**
112      * @return The URL path, or null if no path was defined
113      */
114     public String getPath() {
115         return path;
116     }
117
118     /**
119      * @return The URL port, or null if no port was defined
120      */
121     public String getPort() {
122         return port;
123     }
124
125     public String getVersion() {
126         return version;
127     }
128
129     @Override
130     public String toString() {
131         StringBuilder str = new StringBuilder();
132
133         str.append(scheme + "://" + host);
134         if (port != null) {
135             str.append(":" + port);
136         }
137         if (path != null) {
138             str.append(path);
139         }
140         str.append("/" + version);
141
142         return str.toString();
143     }
144
145 }