2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
18 import java.util.ArrayList;
19 import java.util.HashMap;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.core.env.Environment;
24 import org.springframework.stereotype.Component;
26 import static com.google.common.base.Splitter.on;
27 import static com.google.common.collect.Lists.newArrayList;
30 * Responsible for remapping IP/DNS names in URLs based on property file
33 public class IpMappingProvider implements InitializingBean {
34 public static final String IP_MAP = "ipMap";
35 private final Environment environment;
36 private final Map<String, String> ipMap = new HashMap<>();
39 IpMappingProvider(Environment environment) {
40 this.environment = environment;
44 * After the Bean has been initialized the IP mapping and the VMFM cache is initialized
45 * It is done in this phase because it requires the environment to be initialized
48 public void afterPropertiesSet() throws Exception {
49 on(",").trimResults().omitEmptyStrings().split(environment.getProperty(IP_MAP, String.class, "")).forEach(item -> {
50 ArrayList<String> ip = newArrayList(on("->").trimResults().split(item));
51 ipMap.put(ip.get(0), ip.get(1));
56 * Map IP addresses based on configuration parameter ipMap
58 * @param ip the original IP address
59 * @return the mapped IP address
61 public String mapPrivateIpToPublicIp(String ip) {
62 return ipMap.getOrDefault(ip, ip);