2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.workarounds;
22 import java.net.MalformedURLException;
24 import java.net.URISyntaxException;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 public class LegacyURITransformer {
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>[^/]*?$)");
33 * Instantiates a new legacy URL transformer.
35 private LegacyURITransformer() {
39 private static class Helper {
40 private static final LegacyURITransformer INSTANCE = new LegacyURITransformer();
44 * Gets the single instance of LegacyURLTransformer.
46 * @return single instance of LegacyURLTransformer
48 public static LegacyURITransformer getInstance() {
49 return Helper.INSTANCE;
53 * Gets the legacy URL.
56 * @return the legacy URL
57 * @throws URISyntaxException
58 * @throws MalformedURLException the malformed URL exception
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);
65 result = m.replaceFirst(replacement);
69 return new URI(result);
74 * Gets the current URL.
77 * @return the current URL
78 * @throws URISyntaxException
79 * @throws MalformedURLException the malformed URL exception
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);
86 result = m.replaceFirst(replacement);
90 return new URI(result);