4e8b6bd38585e3784a59cdb3709eb6c5a13976e6
[vnfsdk/refrepo.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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
17 package org.openo.gso.gui.servicegateway.util.register;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.openo.baseservice.remoteservice.exception.ServiceException;
28 import org.openo.baseservice.roa.util.restclient.RestfulFactory;
29 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
30 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
31 import org.openo.gso.commsvc.common.constant.Constant;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Register Tool <br/>
37  * 
38  * @author
39  * @since GSO 0.5, 2016-8-9
40  */
41 public class RegisterUtil {
42
43     /**
44      * Logger
45      */
46     private static final Logger LOGGER = LoggerFactory.getLogger(RegisterUtil.class);
47
48     private RegisterUtil() {
49     }
50     
51     /**
52      * register the service to M-Bus by the parameter<br/>
53      * 
54      * @author
55      * @param jsonInfo
56      *            register body Data
57      * @since GSO 0.5, 2016-8-9
58      */
59     public static void registerService(final String jsonInfo) {
60         // check the parameter
61         if (StringUtils.isEmpty(jsonInfo)) {
62             LOGGER.error("RegisterUtil registerService jsonInfo is null");
63             return;
64         }
65
66         // replace the remote IP Address by the jsonInfo
67         String bodyData = jsonInfo;
68         boolean isIPExist = bodyData.indexOf(Constant.SERVICE_KEY_IP) > Constant.ZERO;
69         try {
70             // get the local IP address
71             String localIP = InetAddress.getLocalHost().getHostAddress();
72
73             // if the jsonInfo have the getInputIP string,start to replace the
74             // local IP
75             if (isIPExist) {
76                 if (!StringUtils.isEmpty(localIP)) {
77                     bodyData = bodyData.replace(Constant.SERVICE_KEY_IP, localIP);
78                 } else {
79                     LOGGER.error("RegisterUtil registerService localIP is null");
80                     return;
81                 }
82             }
83         } catch (UnknownHostException e) {
84             LOGGER.error("RegisterUtil registerService getHostAddress fail:", e);
85             if (isIPExist) {
86                 // if get local IP failed In the isIPExist is true ,operation is
87                 // stopped.
88                 return;
89             }
90         }
91
92         // register the service to M-bus by the restful Interface
93         try {
94             RestfulResponse restfulRsp = RestfulFactory.getRestInstance("http").post(Constant.M_BUS_REGISTER_URL,
95                     getRestfulParameters(bodyData));
96             if (null != restfulRsp) {
97                 // Record the result of registration
98                 // (201:success;415:Invalid Parameter;500:Internal Server Error)
99                 LOGGER.info("RegisterUtil registerService register result:", restfulRsp.getStatus());
100             }
101         } catch (ServiceException e) {
102             LOGGER.error("RegisterUtil registerService post fail:", e);
103         }
104     }
105
106     /**
107      * get the parameters for restful<br/>
108      * 
109      * @author
110      * @param bodyData
111      *            Json Body
112      * @return the RestfulParametes Instance
113      * @since GSO 0.5, 2016-8-9
114      */
115     private static RestfulParametes getRestfulParameters(final String bodyData) {
116         RestfulParametes param = new RestfulParametes();
117         param.putHttpContextHeader(Constant.HEAD_ERMAP_TYPE, Constant.HEAD_ERMAP_VALUE);
118         param.setRawData(bodyData);
119         return param;
120     }
121
122     /**
123      * read the service file<br/>
124      * 
125      * @param path
126      *            the service File Path
127      * @return jsonString
128      * @since GSO 0.5, 2016-8-9
129      */
130     public static String readFile(String path) {
131         // check parameter
132         if (StringUtils.isEmpty(path)) {
133             return null;
134         }
135
136         File file = new File(path);
137         BufferedReader reader = null;
138         String laststr = "";
139         try {
140             reader = new BufferedReader(new FileReader(file));
141             String tempString = null;
142             // Read one line at a time until the end of the null file.
143             while ((tempString = reader.readLine()) != null) {
144                 // add the line
145                 laststr = laststr + tempString;
146             }
147             reader.close();
148         } catch (IOException e) {
149             LOGGER.error("GSO ReadFile fail.", e);
150         } finally {
151             if (reader != null) {
152                 try {
153                     reader.close();
154                 } catch (IOException e1) {
155                     LOGGER.error("GSO ReadFile reader close fail.", e1);
156                 }
157             }
158         }
159         return laststr;
160     }
161 }