Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / workarounds / LegacyURITransformer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.aai.workarounds;
22
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 public class LegacyURITransformer {
30
31         private final Pattern legacyUriPattern = Pattern.compile("/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)");
32         private final Pattern currentUriPattern = Pattern.compile("/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/vserver/(?<vserverKey>[^/]*?$)");
33         /**
34          * Instantiates a new legacy URL transformer.
35          */
36         private LegacyURITransformer() {
37                 
38         }
39         
40         private static class Helper {
41                 private static final LegacyURITransformer INSTANCE = new LegacyURITransformer();
42         }
43         
44         /**
45          * Gets the single instance of LegacyURLTransformer.
46          *
47          * @return single instance of LegacyURLTransformer
48          */
49         public static LegacyURITransformer getInstance() {
50                 return Helper.INSTANCE;
51         }
52         
53         /**
54          * Gets the legacy URL.
55          *
56          * @param url the url
57          * @return the legacy URL
58          * @throws URISyntaxException 
59          * @throws MalformedURLException the malformed URL exception
60          */
61         public URI getLegacyURI(URI url) throws URISyntaxException  {
62                 String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}";
63                 String result = url.toString();
64                 Matcher m = currentUriPattern.matcher(result);
65                 if (m.find()) {
66                         result = m.replaceFirst(replacement);
67
68                 }
69                 
70                 return new URI(result);
71                 
72         }
73         
74         /**
75          * Gets the current URL.
76          *
77          * @param url the url
78          * @return the current URL
79          * @throws URISyntaxException 
80          * @throws MalformedURLException the malformed URL exception
81          */
82         public URI getCurrentURI(URI url) throws URISyntaxException  {
83                 String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}";
84                 String result = url.toString();
85                 Matcher m = legacyUriPattern.matcher(result);
86                 if (m.find()) {
87                         result = m.replaceFirst(replacement);
88
89                 }
90                 
91                 return new URI(result);
92         }
93 }