Replace the orginal node ip with SERVICE_IP
[vfc/nfvo/driver/sfc.git] / zte / sfc-driver / sfc-driver / src / main / java / org / onap / sfc / utils / SfcDriverUtil.java
1 /**
2  * Copyright 2016 ZTE 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.sfc.utils;
17
18 import com.google.gson.Gson;
19 import org.onap.sfc.entity.ChainParameter;
20 import org.onap.sfc.entity.MsbRegisterEntity;
21 import org.onap.sfc.entity.NodeEntity;
22 import org.onap.sfc.entity.portpair.ServiceFunctionParameter;
23 import org.onap.sfc.service.ConfigInfo;
24 import java.net.InetAddress;
25 import java.net.UnknownHostException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 public class SfcDriverUtil {
35     private static final Log log = LogFactory.getLog(SfcDriverUtil.class);
36     public static String generateUuid() {
37         return UUID.randomUUID().toString();
38
39     }
40
41     public static ArrayList<ServiceFunctionParameter> generateSfParams(Map sfHashParams) {
42         ArrayList<ServiceFunctionParameter> serviceParameters = new ArrayList<ServiceFunctionParameter>();
43         if (sfHashParams != null) {
44             //serviceParameters.putAll(portPairReq4N.getSfParam());
45             Set keySet = sfHashParams.keySet();
46             Iterator iter = keySet.iterator();
47             while (iter.hasNext()) {
48                 String keyName = iter.next().toString();
49                 ServiceFunctionParameter sfParam = new ServiceFunctionParameter();
50                 sfParam.setServiceFunctionParamter(keyName);
51                 sfParam.setGetServiceFunctionParamterValue(sfHashParams.get(keyName).toString());
52                 serviceParameters.add(sfParam);
53             }
54         }
55         return serviceParameters;
56     }
57
58     public static ChainParameter generateChainParam(String key, String value) {
59         ChainParameter cp = new ChainParameter();
60         cp.setChainParameter(key);
61         cp.setChainParamValue(value);
62         return cp;
63     }
64
65     public static String toJson(Object obj) {
66         Gson gson = new Gson();
67         return gson.toJson(obj);
68     }
69
70     public static String generateAuthorization() {
71         String userName = "admin";
72         String authCred = "admin"; //Password string: Sonar will raise concern
73         String usernameAndPassword = userName + ":" + authCred;
74 //        String headName = "Authorization";
75         return "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(usernameAndPassword.getBytes());
76     }
77
78     public static MsbRegisterEntity getMsbRegisterInfo() {
79         MsbRegisterEntity entity = new MsbRegisterEntity();
80         entity.setServiceName(SfcConst.SERVICE_NAME);
81         entity.setVersion("v1");
82         //entity.setUrl(SfcConst.SERVICE_URL);
83         entity.setUrl("/api/ztesdncdriver/v1");
84         entity.setProtocol("REST");
85         entity.setVisualRange("1");
86         ArrayList<NodeEntity> nodes = new ArrayList<NodeEntity>();
87         NodeEntity node = new NodeEntity();
88         node.setIp(ConfigInfo.getConfig().getServiceIp());
89         node.setPort("8411");
90         node.setTtl("1");
91         nodes.add(node);
92         entity.setNodes(nodes);
93         return entity;
94     }
95
96     public static String getLocalIp()
97     {
98         try {
99             InetAddress addr = InetAddress.getLocalHost();
100             return addr.getHostAddress();
101         } catch (UnknownHostException e) {
102             //e.printStackTrace();
103             log.error("getLocalIp StackTrace:", e);
104         }
105         return "";
106     }
107
108 }