Updating Nokia driver
[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 import java.util.function.Consumer;
27
28 import static com.google.common.base.Splitter.on;
29 import static com.google.common.collect.Lists.newArrayList;
30
31 /**
32  * Responsible for remapping IP/DNS names in URLs based on property file
33  */
34 @Component
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<>();
39
40     @Autowired
41     IpMappingProvider(Environment environment) {
42         this.environment = environment;
43     }
44
45     /**
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
48      */
49     @Override
50     public void afterPropertiesSet() throws Exception {
51         on(",").trimResults().omitEmptyStrings().split(environment.getProperty(IP_MAP, String.class, "")).forEach(new Consumer<String>() {
52             @Override
53             public void accept(String item) {
54                 ArrayList<String> ip = newArrayList(on("->").trimResults().split(item));
55                 ipMap.put(ip.get(0), ip.get(1));
56             }
57         });
58     }
59
60     /**
61      * Map IP addresses based on configuration parameter ipMap
62      *
63      * @param ip the original IP address
64      * @return the mapped IP address
65      */
66     public String mapPrivateIpToPublicIp(String ip) {
67         return ipMap.getOrDefault(ip, ip);
68     }
69 }