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.openo.sfc.utils;
18 import com.google.gson.Gson;
19 import org.openo.sfc.entity.ChainParameter;
20 import org.openo.sfc.entity.MsbRegisterEntity;
21 import org.openo.sfc.entity.NodeEntity;
22 import org.openo.sfc.entity.portpair.ServiceFunctionParameter;
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;
32 public class SfcDriverUtil {
34 public static String generateUuid() {
35 return UUID.randomUUID().toString();
39 public static ArrayList<ServiceFunctionParameter> generateSfParams(Map sfHashParams) {
40 ArrayList<ServiceFunctionParameter> serviceParameters = new ArrayList<ServiceFunctionParameter>();
41 if (sfHashParams != null) {
42 //serviceParameters.putAll(portPairReq4N.getSfParam());
43 Set keySet = sfHashParams.keySet();
44 Iterator iter = keySet.iterator();
45 while (iter.hasNext()) {
46 String keyName = iter.next().toString();
47 ServiceFunctionParameter sfParam = new ServiceFunctionParameter();
48 sfParam.setServiceFunctionParamter(keyName);
49 sfParam.setGetServiceFunctionParamterValue(sfHashParams.get(keyName).toString());
50 serviceParameters.add(sfParam);
53 return serviceParameters;
56 public static ChainParameter generateChainParam(String key, String value) {
57 ChainParameter cp = new ChainParameter();
58 cp.setChainParameter(key);
59 cp.setChainParamValue(value);
63 public static String toJson(Object obj) {
64 Gson gson = new Gson();
65 return gson.toJson(obj);
68 public static String generateAuthorization() {
69 String userName = "admin";
70 String password = "admin";
71 String usernameAndPassword = userName + ":" + password;
72 // String headName = "Authorization";
73 return "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(usernameAndPassword.getBytes());
76 public static MsbRegisterEntity getMsbRegisterInfo() {
77 MsbRegisterEntity entity = new MsbRegisterEntity();
78 entity.setServiceName(SfcConst.SERVICE_NAME);
79 entity.setVersion("v1");
80 //entity.setUrl(SfcConst.SERVICE_URL);
81 entity.setUrl("/openoapi/ztesdncdriver/v1");
82 entity.setProtocol("REST");
83 entity.setVisualRange("1");
84 ArrayList<NodeEntity> nodes = new ArrayList<NodeEntity>();
85 NodeEntity node = new NodeEntity();
86 node.setIp(getLocalIp());
90 entity.setNodes(nodes);
94 public static String getLocalIp()
97 InetAddress addr = InetAddress.getLocalHost();
98 return addr.getHostAddress().toString();
99 } catch (UnknownHostException e) {