ec6db5e442dec42f9dd7afa19767bc1389656fd0
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / IpMappingProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
17
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;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import static com.google.common.base.Splitter.on;
28 import static com.google.common.collect.Lists.newArrayList;
29
30 /**
31  * Responsible for remapping IP/DNS names in URLs based on property file
32  */
33 @Component
34 public class IpMappingProvider implements InitializingBean {
35     public static final String IP_MAP = "ipMap";
36     private final Environment environment;
37     private final Map<String, String> ipMap = new HashMap<>();
38
39     @Autowired
40     IpMappingProvider(Environment environment) {
41         this.environment = environment;
42     }
43
44     /**
45      * After the Bean has been initialized the IP mapping and the VMFM cache is initialized
46      * It is done in this phase because it requires the environment to be initialized
47      */
48     @Override
49     public void afterPropertiesSet() throws Exception {
50         on(",").trimResults().omitEmptyStrings().split(environment.getProperty(IP_MAP, String.class, "")).forEach(item -> {
51             ArrayList<String> ip = newArrayList(on("->").trimResults().split(item));
52             ipMap.put(ip.get(0), ip.get(1));
53         });
54     }
55
56     /**
57      * Map IP addresses based on configuration parameter ipMap
58      *
59      * @param ip the original IP address
60      * @return the mapped IP address
61      */
62     public String mapPrivateIpToPublicIp(String ip) {
63         return ipMap.getOrDefault(ip, ip);
64     }
65 }