2b53a53c3d4775d8211f9b3a791be5edf44d69e6
[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
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
32 public class SfcDriverUtil {
33
34     public static String generateUuid() {
35         return UUID.randomUUID().toString();
36
37     }
38
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);
51             }
52         }
53         return serviceParameters;
54     }
55
56     public static ChainParameter generateChainParam(String key, String value) {
57         ChainParameter cp = new ChainParameter();
58         cp.setChainParameter(key);
59         cp.setChainParamValue(value);
60         return cp;
61     }
62
63     public static String toJson(Object obj) {
64         Gson gson = new Gson();
65         return gson.toJson(obj);
66     }
67
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());
74     }
75
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("/api/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());
87         node.setPort("8411");
88         node.setTtl("1");
89         nodes.add(node);
90         entity.setNodes(nodes);
91         return entity;
92     }
93
94     public static String getLocalIp()
95     {
96         try {
97             InetAddress addr = InetAddress.getLocalHost();
98             return addr.getHostAddress().toString();
99         } catch (UnknownHostException e) {
100             e.printStackTrace();
101         }
102         return "";
103     }
104
105 }