2 * Copyright 2016 ZTE 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.sfc.utils;
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;
30 import java.util.UUID;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
34 public class SfcDriverUtil {
35 private static final Log log = LogFactory.getLog(SfcDriverUtil.class);
36 public static String generateUuid() {
37 return UUID.randomUUID().toString();
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);
55 return serviceParameters;
58 public static ChainParameter generateChainParam(String key, String value) {
59 ChainParameter cp = new ChainParameter();
60 cp.setChainParameter(key);
61 cp.setChainParamValue(value);
65 public static String toJson(Object obj) {
66 Gson gson = new Gson();
67 return gson.toJson(obj);
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());
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());
92 entity.setNodes(nodes);
96 public static String getLocalIp()
99 InetAddress addr = InetAddress.getLocalHost();
100 return addr.getHostAddress();
101 } catch (UnknownHostException e) {
102 //e.printStackTrace();
103 log.error("getLocalIp StackTrace:", e);