Update VMURL to use a range for the version
[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  * 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.openecomp.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 VMURL {
34
35     /**
36      * The regular expression pattern used to parse the URL. Capturing groups are used to identify and extract the
37      * various component parts of the URL.
38      */
39     private static Pattern pattern = Pattern
40         .compile("(\\p{Alnum}+)://([^/:]+)(?::([0-9]+))?(/.*)?/(v[0-9\\.]+)/([^/]+)/servers/([^/]+)");
41
42     /**
43      * The URL scheme or protocol, such as HTTP or HTTPS
44      */
45     private String scheme;
46
47     /**
48      * The host name or ip address
49      */
50     private String host;
51
52     /**
53      * The path, or null if no path is defined
54      */
55     private String path;
56
57     /**
58      * The port number, or null if no port is defined
59      */
60     private String port;
61
62     /**
63      * The tenant UUID
64      */
65     private String tenantId;
66
67     /**
68      * The server UUID
69      */
70     private String serverId;
71
72     /**
73      * The version of the service
74      */
75     private String version;
76
77     /**
78      * A private default constructor prevents instantiation by any method other than the factory method
79      * 
80      * @see #parseURL(String)
81      */
82     private VMURL() {
83
84     }
85
86     /**
87      * This static method is used to parse the provided server URL string and return a parse results object (VMURL)
88      * which represents the state of the parse.
89      * 
90      * @param serverUrl
91      *            The server URL to be parsed
92      * @return The VMURL parse results object, or null if the URL was not valid or null.
93      */
94     public static VMURL parseURL(String serverUrl) {
95         VMURL obj = null;
96         if (serverUrl != null) {
97             Matcher matcher = pattern.matcher(serverUrl.trim());
98             if (matcher.matches()) {
99                 obj = new VMURL();
100                 obj.scheme = matcher.group(1);
101                 obj.host = matcher.group(2);
102                 obj.port = matcher.group(3);
103                 obj.path = matcher.group(4);
104                 obj.version = matcher.group(5);
105                 obj.tenantId = matcher.group(6);
106                 obj.serverId = matcher.group(7);
107             }
108         }
109
110         return obj;
111     }
112
113     /**
114      * @return The URL scheme
115      */
116     public String getScheme() {
117         return scheme;
118     }
119
120     /**
121      * @return The URL host
122      */
123     public String getHost() {
124         return host;
125     }
126
127     /**
128      * @return THe URL path, or null if no path was defined
129      */
130     public String getPath() {
131         return path;
132     }
133
134     /**
135      * @return The URL port, or null if no port was defined
136      */
137     public String getPort() {
138         return port;
139     }
140
141     /**
142      * @return The tenant id
143      */
144     public String getTenantId() {
145         return tenantId;
146     }
147
148     /**
149      * @return The server ID
150      */
151     public String getServerId() {
152         return serverId;
153     }
154
155     /**
156      * @return The version of the service 
157      */
158     public String getVersion() {
159         return version;
160     }
161
162     @Override
163     public String toString() {
164         StringBuilder str = new StringBuilder();
165         str.append(scheme + "://" + host);
166         if (port != null) {
167             str.append(":" + port);
168         }
169         if (path != null) {
170             str.append(path);
171         }
172         str.append("/" + version + "/" + tenantId + "/servers/" + serverId);
173         return str.toString();
174     }
175
176 }