Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / workarounds / LegacyURITransformer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.workarounds;
21
22 import java.net.MalformedURLException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 public class LegacyURITransformer {
29
30         private final Pattern legacyUriPattern = Pattern.compile("/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)");
31         private final Pattern currentUriPattern = Pattern.compile("/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/vserver/(?<vserverKey>[^/]*?$)");
32         /**
33          * Instantiates a new legacy URL transformer.
34          */
35         private LegacyURITransformer() {
36                 
37         }
38         
39         private static class Helper {
40                 private static final LegacyURITransformer INSTANCE = new LegacyURITransformer();
41         }
42         
43         /**
44          * Gets the single instance of LegacyURLTransformer.
45          *
46          * @return single instance of LegacyURLTransformer
47          */
48         public static LegacyURITransformer getInstance() {
49                 return Helper.INSTANCE;
50         }
51         
52         /**
53          * Gets the legacy URL.
54          *
55          * @param url the url
56          * @return the legacy URL
57          * @throws URISyntaxException 
58          * @throws MalformedURLException the malformed URL exception
59          */
60         public URI getLegacyURI(URI url) throws URISyntaxException  {
61                 String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}";
62                 String result = url.toString();
63                 Matcher m = currentUriPattern.matcher(result);
64                 if (m.find()) {
65                         result = m.replaceFirst(replacement);
66
67                 }
68                 
69                 return new URI(result);
70                 
71         }
72         
73         /**
74          * Gets the current URL.
75          *
76          * @param url the url
77          * @return the current URL
78          * @throws URISyntaxException 
79          * @throws MalformedURLException the malformed URL exception
80          */
81         public URI getCurrentURI(URI url) throws URISyntaxException  {
82                 String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}";
83                 String result = url.toString();
84                 Matcher m = legacyUriPattern.matcher(result);
85                 if (m.find()) {
86                         result = m.replaceFirst(replacement);
87
88                 }
89                 
90                 return new URI(result);
91         }
92 }