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.RESULT_CHANNEL_KEY);
70 body = this.receive();
72 alarmChannel.put(body);
73 } catch (InterruptedException e) {
74 log.error(StringUtil.getStackTrace(e));
76 } catch (Exception e) {
81 } catch (Exception e) {
82 log.error(StringUtil.getStackTrace(e));
88 public String receive() throws Exception {
91 String retString = null;
93 while (retString == null && !this.isStop) {
95 msg = MessageUtil.readOneMsg(is);
97 if("ackLoginAlarm".equalsIgnoreCase(msg.getMsgType().name)){
98 log.debug("receive login ack");
99 boolean suc = this.ackLoginAlarm(msg);
102 if(reqId == Integer.MAX_VALUE){
106 Msg msgheart = MessageUtil.putHeartBeatMsg(reqId);
107 heartBeat = new HeartBeat(socket,msgheart);
108 heartBeat.setName("CMCC_JT_HeartBeat");
115 if("ackHeartBeat".equalsIgnoreCase(msg.getMsgType().name)){
116 log.debug("received heartBeat message:"+msg.getBody());
122 if("realTimeAlarm".equalsIgnoreCase(msg.getMsgType().name)){
123 log.debug("received alarm message");
124 retString = msg.getBody();
127 if(retString == null){
134 public void init() throws Exception {
137 String host = collectVo.getIP();
139 String port = collectVo.getPort();
141 String user = collectVo.getUser();
143 String password = collectVo.getPassword();
145 String read_timeout = collectVo.getRead_timeout();
146 if ((read_timeout != null) && (read_timeout.trim().length() > 0)) {
148 this.read_timeout = Integer.parseInt(read_timeout);
149 } catch (NumberFormatException e) {
150 log.error(StringUtil.getStackTrace(e));
153 log.debug("socket connect host=" + host + ", port=" + port);
155 int portInt = Integer.parseInt(port);
156 socket = new Socket(host, portInt);
158 } catch (UnknownHostException e) {
159 throw new Exception("remote host [" + host + "]connect fail" + StringUtil.getStackTrace(e));
160 } catch (IOException e) {
161 throw new Exception("create socket IOException " + StringUtil.getStackTrace(e));
164 socket.setSoTimeout(this.read_timeout);
165 socket.setTcpNoDelay(true);
166 socket.setKeepAlive(true);
167 } catch (SocketException e) {
168 throw new Exception(" SocketException " + StringUtil.getStackTrace(e));
171 dos = new BufferedOutputStream(socket.getOutputStream());
173 Msg msg = MessageUtil.putLoginMsg(user,password);
176 log.debug("send login message "+msg.toString(false));
177 MessageUtil.writeMsg(msg,dos);
179 } catch (Exception e) {
180 log.error("send login message is fail "+StringUtil.getStackTrace(e));
183 is = new BufferedInputStream(socket.getInputStream());
185 } catch (SocketException e) {
186 throw new Exception(StringUtil.getStackTrace(e));
190 private boolean ackLoginAlarm(Msg msg) throws Exception {
192 boolean is_success = false;
194 String loginres = msg.getBody();
195 //ackLoginAlarm; result=fail(succ); resDesc=username-error
196 String [] loginbody = loginres.split(";");
197 if(loginbody.length > 1){
198 for(String str :loginbody){
199 if(str.contains("=")){
200 String [] paras1 = str.split("=",-1);
201 if("result".equalsIgnoreCase(paras1[0].trim())){
202 if("succ".equalsIgnoreCase(paras1[1].trim())){
211 log.error("login ack body Incorrect formatbody=" + loginres);
215 } catch (Exception e) {
216 log.error("pocess login ack fail"+StringUtil.getStackTrace(e));
219 log.info("login sucess receive login ack " + msg.getBody());
221 log.error("login fail receive login ack " + msg.getBody());
224 throw new Exception("login fail quit");
229 public void close() {
231 if(heartBeat != null){
232 heartBeat.setStop(true);
238 } catch (IOException e) {
247 } catch (IOException e) {
253 if (socket != null) {
256 } catch (IOException e) {
264 public void reinit() {
267 while(!this.isStop) {
271 Thread.sleep(1000 * 10);
274 } catch (Exception e) {
275 log.error("Number ["+time+"]reconnect ["+collectVo.getIP()+"]fail" );
281 * @param isStop the isStop to set
283 public void setStop(boolean isStop) {
284 this.isStop = isStop;
288 * @return the heartBeat
290 public HeartBeat getHeartBeat() {