2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
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.vfc.nfvo.emsdriver.collector.alarm;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.IOException;
21 import java.net.Socket;
22 import java.net.SocketException;
23 import java.net.UnknownHostException;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
29 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
30 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
31 import org.onap.vfc.nfvo.emsdriver.messagemgr.MessageChannel;
32 import org.onap.vfc.nfvo.emsdriver.messagemgr.MessageChannelFactory;
34 import com.alibaba.fastjson.JSONObject;
37 public class AlarmTaskThread extends Thread{
38 public Log log = LogFactory.getLog(AlarmTaskThread.class);
40 private HeartBeat heartBeat = null;
42 private boolean isStop = false;
43 private CollectVo collectVo = null;
44 private int read_timeout = Constant.READ_TIMEOUT_MILLISECOND;
47 private Socket socket = null;
48 private BufferedInputStream is = null;
49 private BufferedOutputStream dos = null;
51 private MessageChannel alarmChannel;
54 public AlarmTaskThread() {
58 public AlarmTaskThread(CollectVo collectVo) {
60 this.collectVo = collectVo;
64 alarmChannel = MessageChannelFactory.getMessageChannel(Constant.ALARM_CHANNEL_KEY);
70 body = this.receive();
71 String alarm120 = this.build120Alarm(body);
73 this.send120Alarm(alarm120);
74 } catch (Exception e) {
78 } catch (Exception e) {
79 log.error(StringUtil.getStackTrace(e));
83 private void send120Alarm(String alarm120) {
86 alarmChannel.put(alarm120);
87 } catch (InterruptedException e) {
88 log.error(StringUtil.getStackTrace(e));
92 public String build120Alarm(String body) {
93 StringBuilder content = new StringBuilder(
94 "<?xml version='1.0' encoding='iso-8859-1'?>\n")
95 .append("<WholeMsg MsgMark='120' Priority='2' FieldNum='5'><FM_ALARM_MSG>\n");
98 JSONObject reagobj = JSONObject.parseObject(body);
100 Set<String> keys = reagobj.keySet();
102 for (String key : keys) {
104 String value = reagobj.get(key).toString();
105 content.append("<").append(key).append(">");
106 content.append(value);
107 content.append("</").append(key).append(">\n");
109 content.append("</FM_ALARM_MSG></WholeMsg>");
111 return content.toString();
115 public String receive() throws Exception {
118 String retString = null;
120 while (retString == null && !this.isStop) {
122 msg = MessageUtil.readOneMsg(is);
124 if("ackLoginAlarm".equalsIgnoreCase(msg.getMsgType().name)){
125 log.debug("receive login ack");
126 boolean suc = this.ackLoginAlarm(msg);
129 if(reqId == Integer.MAX_VALUE){
133 Msg msgheart = MessageUtil.putHeartBeatMsg(reqId);
134 heartBeat = new HeartBeat(socket,msgheart);
135 heartBeat.setName("CMCC_JT_HeartBeat");
142 if("ackHeartBeat".equalsIgnoreCase(msg.getMsgType().name)){
143 log.debug("received heartBeat message:"+msg.getBody());
149 if("realTimeAlarm".equalsIgnoreCase(msg.getMsgType().name)){
150 log.debug("received alarm message");
151 retString = msg.getBody();
157 public void init() throws Exception {
160 String host = collectVo.getIP();
162 String port = collectVo.getPort();
164 String user = collectVo.getUser();
166 String password = collectVo.getPassword();
168 String read_timeout = collectVo.getRead_timeout();
169 if ((read_timeout != null) && (read_timeout.trim().length() > 0)) {
171 this.read_timeout = Integer.parseInt(read_timeout);
172 } catch (NumberFormatException e) {
173 log.error(StringUtil.getStackTrace(e));
176 log.debug("socket connect host=" + host + ", port=" + port);
178 int portInt = Integer.parseInt(port);
179 socket = new Socket(host, portInt);
181 } catch (UnknownHostException e) {
182 throw new Exception("remote host [" + host + "]connect fail" + StringUtil.getStackTrace(e));
183 } catch (IOException e) {
184 throw new Exception("create socket IOException " + StringUtil.getStackTrace(e));
187 socket.setSoTimeout(this.read_timeout);
188 socket.setTcpNoDelay(true);
189 socket.setKeepAlive(true);
190 } catch (SocketException e) {
191 throw new Exception(" SocketException " + StringUtil.getStackTrace(e));
194 dos = new BufferedOutputStream(socket.getOutputStream());
196 Msg msg = MessageUtil.putLoginMsg(user,password);
199 log.debug("send login message "+msg.toString(false));
200 MessageUtil.writeMsg(msg,dos);
202 } catch (Exception e) {
203 log.error("send login message is fail "+StringUtil.getStackTrace(e));
206 is = new BufferedInputStream(socket.getInputStream());
208 } catch (SocketException e) {
209 throw new Exception(StringUtil.getStackTrace(e));
213 private boolean ackLoginAlarm(Msg msg) throws Exception {
215 boolean is_success = false;
217 String loginres = msg.getBody();
218 //ackLoginAlarm; result=fail(succ); resDesc=username-error
219 String [] loginbody = loginres.split(";");
220 if(loginbody.length > 1){
221 for(String str :loginbody){
222 if(str.contains("=")){
223 String [] paras1 = str.split("=",-1);
224 if("result".equalsIgnoreCase(paras1[0].trim())){
225 if("succ".equalsIgnoreCase(paras1[1].trim())){
234 log.error("login ack body Incorrect formatbody=" + loginres);
238 } catch (Exception e) {
239 log.error("pocess login ack fail"+StringUtil.getStackTrace(e));
242 log.info("login sucess receive login ack " + msg.getBody());
244 log.error("login fail receive login ack " + msg.getBody());
247 throw new Exception("login fail quit");
252 public void close() {
254 if(heartBeat != null){
255 heartBeat.setStop(true);
261 } catch (IOException e) {
270 } catch (IOException e) {
276 if (socket != null) {
279 } catch (IOException e) {
287 public void reinit() {
290 while(!this.isStop) {
294 Thread.sleep(1000 * 10);
297 } catch (Exception e) {
298 log.error("Number ["+time+"]reconnect ["+collectVo.getIP()+"]fail" );