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 org.springframework.beans.factory.InitializingBean;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.core.env.Environment;
21 import org.springframework.stereotype.Component;
23 import java.util.ArrayList;
24 import java.util.HashMap;
26 import java.util.function.Consumer;
28 import static com.google.common.base.Splitter.on;
29 import static com.google.common.collect.Lists.newArrayList;
32 * Responsible for remapping IP/DNS names in URLs based on property file
35 public class IpMappingProvider implements InitializingBean {
36 public static final String IP_MAP = "ipMap";
37 private final Environment environment;
38 private final Map<String, String> ipMap = new HashMap<>();
41 IpMappingProvider(Environment environment) {
42 this.environment = environment;
46 * After the Bean has been initialized the IP mapping and the VMFM cache is initialized
47 * It is done in this phase because it requires the environment to be initialized
50 public void afterPropertiesSet() throws Exception {
51 on(",").trimResults().omitEmptyStrings().split(environment.getProperty(IP_MAP, String.class, "")).forEach(new Consumer<String>() {
53 public void accept(String item) {
54 ArrayList<String> ip = newArrayList(on("->").trimResults().split(item));
55 ipMap.put(ip.get(0), ip.get(1));
61 * Map IP addresses based on configuration parameter ipMap
63 * @param ip the original IP address
64 * @return the mapped IP address
66 public String mapPrivateIpToPublicIp(String ip) {
67 return ipMap.getOrDefault(ip, ip);