First part of onap rename
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.iaas.impl;
26
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 /**
31  * This class is used to parse the VM URL returned from OpenStack and extract all of the constituent parts.
32  */
33 public class IdentityURL {
34     /**
35      * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
36      * various component parts of the URL.
37      */
38     private static Pattern pattern = Pattern.compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/?");
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 path of the service, or null if no path is defined
52      */
53     private String path;
54
55     /**
56      * The port number, or null if no port is defined
57      */
58     private String port;
59
60     /**
61      * The version of the service
62      */
63     private String version;
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 IdentityURL() {
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 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 IdentityURL parseURL(String identUrl) {
82         IdentityURL obj = null;
83         if (identUrl != null) {
84             Matcher matcher = pattern.matcher(identUrl.trim()); // http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3
85             if (matcher.matches()) { // (\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/?"
86                 obj = new IdentityURL();
87                 obj.scheme = matcher.group(1);
88                 obj.host = matcher.group(2);
89                 obj.port = matcher.group(3);
90                 obj.path = matcher.group(4);
91                 obj.version = 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 path, or null if no path was defined
114      */
115     public String getPath() {
116         return path;
117     }
118
119     /**
120      * @return The URL port, or null if no port was defined
121      */
122     public String getPort() {
123         return port;
124     }
125
126     public String getVersion() {
127         return version;
128     }
129
130     @Override
131     public String toString() {
132         StringBuilder str = new StringBuilder();
133
134         str.append(scheme + "://" + host);
135         if (port != null) {
136             str.append(":" + port);
137         }
138         if (path != null) {
139             str.append(path);
140         }
141         str.append("/" + version);
142
143         return str.toString();
144     }
145
146 }