68a302106ce506639280a2b166e04ca70ded9be8
[vfc/nfvo/driver/ems.git] /
1 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>AlarmTaskThread.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">vfc-nfvo-driver-ems-ems-boco</a> &gt; <a href="index.source.html" class="el_package">org.onap.vfc.nfvo.emsdriver.collector.alarm</a> &gt; <span class="el_source">AlarmTaskThread.java</span></div><h1>AlarmTaskThread.java</h1><pre class="source lang-java linenums">/**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
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 &quot;AS IS&quot; 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.vfc.nfvo.emsdriver.collector.alarm;
17
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;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
28 import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
29 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
30 import org.onap.vfc.nfvo.emsdriver.messagemgr.MessageChannel;
31 import org.onap.vfc.nfvo.emsdriver.messagemgr.MessageChannelFactory;
32
33
34 public class AlarmTaskThread extends Thread{
35 <span class="pc" id="L35">      public  Log log = LogFactory.getLog(AlarmTaskThread.class);</span>
36         
37 <span class="pc" id="L37">      private HeartBeat heartBeat = null;</span>
38         
39 <span class="pc" id="L39">      private boolean isStop = false;</span>
40 <span class="pc" id="L40">      private CollectVo collectVo = null;</span>
41 <span class="pc" id="L41">      private int read_timeout = Constant.READ_TIMEOUT_MILLISECOND;</span>
42         private int reqId;
43         
44 <span class="pc" id="L44">      private Socket socket = null;</span>
45 <span class="pc" id="L45">      private BufferedInputStream is = null;</span>
46 <span class="pc" id="L46">      private BufferedOutputStream dos = null;</span>
47         
48         private MessageChannel alarmChannel;
49         
50         
51         public AlarmTaskThread() {
52 <span class="nc" id="L52">              super();</span>
53 <span class="nc" id="L53">      }</span>
54
55 <span class="fc" id="L55">      public AlarmTaskThread(CollectVo collectVo) {</span>
56
57 <span class="fc" id="L57">              this.collectVo = collectVo;</span>
58 <span class="fc" id="L58">      }</span>
59
60         public void run() {
61 <span class="nc" id="L61">              alarmChannel = MessageChannelFactory.getMessageChannel(Constant.RESULT_CHANNEL_KEY);</span>
62                 try {
63 <span class="nc" id="L63">                      this.init();</span>
64 <span class="nc bnc" id="L64" title="All 2 branches missed.">                   while(!this.isStop){</span>
65                                 String body;
66                                 try {
67 <span class="nc" id="L67">                                      body = this.receive();</span>
68                                         try {
69 <span class="nc" id="L69">                                              alarmChannel.put(body);</span>
70 <span class="nc" id="L70">                                      } catch (InterruptedException e) {</span>
71 <span class="nc" id="L71">                                              log.error(StringUtil.getStackTrace(e));</span>
72 <span class="nc" id="L72">                                      }</span>
73 <span class="nc" id="L73">                              } catch (Exception e) {</span>
74 <span class="nc" id="L74">                                      e.printStackTrace();</span>
75 <span class="nc" id="L75">                                      reinit();</span>
76 <span class="nc" id="L76">                              }</span>
77                         }
78 <span class="nc" id="L78">              } catch (Exception e) {</span>
79 <span class="nc" id="L79">                      log.error(StringUtil.getStackTrace(e));</span>
80 <span class="nc" id="L80">              }</span>
81 <span class="nc" id="L81">      }</span>
82         
83         
84
85         public String receive() throws Exception {
86
87 <span class="fc" id="L87">              Msg msg =null;</span>
88 <span class="fc" id="L88">              String retString = null;</span>
89                 
90 <span class="pc bpc" id="L90" title="1 of 4 branches missed.">          while (retString == null &amp;&amp; !this.isStop) {</span>
91                         
92 <span class="fc" id="L92">                      msg = MessageUtil.readOneMsg(is);</span>
93 <span class="fc" id="L93">                      log.debug(&quot;msg = &quot;+msg.toString(true));</span>
94 <span class="fc" id="L94">                      log.info(&quot;msg.getMsgType().name = &quot;+msg.getMsgType().name);</span>
95 <span class="fc bfc" id="L95" title="All 2 branches covered.">                  if(&quot;ackLoginAlarm&quot;.equalsIgnoreCase(msg.getMsgType().name)){</span>
96 <span class="fc" id="L96">                              log.debug(&quot;receive login ack&quot;);</span>
97 <span class="fc" id="L97">                              boolean suc = this.ackLoginAlarm(msg);</span>
98 <span class="pc bpc" id="L98" title="1 of 2 branches missed.">                          if(suc){</span>
99                                         
100 <span class="pc bpc" id="L100" title="1 of 2 branches missed.">                                 if(reqId == Integer.MAX_VALUE){</span>
101 <span class="nc" id="L101">                                             reqId = 0;</span>
102                                         }
103 <span class="fc" id="L103">                                     reqId ++;</span>
104 <span class="fc" id="L104">                                     Msg  msgheart = MessageUtil.putHeartBeatMsg(reqId);</span>
105 <span class="fc" id="L105">                                     heartBeat =  new HeartBeat(socket,msgheart); </span>
106 <span class="fc" id="L106">                                     heartBeat.setName(&quot;CMCC_JT_HeartBeat&quot;);</span>
107                                         // start heartBeat
108 <span class="fc" id="L108">                                     heartBeat.start();</span>
109                                 }
110 <span class="fc" id="L110">                             retString = null;</span>
111                         }
112                         
113 <span class="pc bpc" id="L113" title="1 of 2 branches missed.">                 if(&quot;ackHeartBeat&quot;.equalsIgnoreCase(msg.getMsgType().name)){</span>
114 <span class="nc" id="L114">                             log.debug(&quot;received heartBeat message:&quot;+msg.getBody());</span>
115 <span class="nc" id="L115">                             retString = null;</span>
116                         }
117                         
118                         
119                         
120 <span class="fc bfc" id="L120" title="All 2 branches covered.">                 if(&quot;realTimeAlarm&quot;.equalsIgnoreCase(msg.getMsgType().name)){</span>
121 <span class="fc" id="L121">                             log.debug(&quot;received alarm message&quot;);</span>
122 <span class="fc" id="L122">                             retString =  msg.getBody();</span>
123                         }
124                         
125 <span class="fc bfc" id="L125" title="All 2 branches covered.">                 if(retString == null){</span>
126 <span class="fc" id="L126">                             Thread.sleep(100);</span>
127                         }
128                 }
129 <span class="fc" id="L129">             return retString;</span>
130         }
131         
132         public void init() throws Exception {
133 <span class="fc" id="L133">             isStop = false;</span>
134                 //host
135 <span class="fc" id="L135">             String host = collectVo.getIP();</span>
136                 //port
137 <span class="fc" id="L137">             String port = collectVo.getPort();</span>
138                 //user
139 <span class="fc" id="L139">             String user = collectVo.getUser();</span>
140                 //password
141 <span class="fc" id="L141">             String password = collectVo.getPassword();</span>
142                 
143 <span class="fc" id="L143">             String read_timeout = collectVo.getRead_timeout();</span>
144 <span class="pc bpc" id="L144" title="3 of 4 branches missed.">         if ((read_timeout != null) &amp;&amp; (read_timeout.trim().length() &gt; 0)) {</span>
145                       try {
146 <span class="nc" id="L146">                     this.read_timeout = Integer.parseInt(read_timeout);</span>
147 <span class="nc" id="L147">                   } catch (NumberFormatException e) {</span>
148 <span class="nc" id="L148">                     log.error(StringUtil.getStackTrace(e));</span>
149 <span class="nc" id="L149">                   }</span>
150                     }
151 <span class="fc" id="L151">             log.info(&quot;socket connect host=&quot; + host + &quot;, port=&quot; + port);</span>
152                 try {
153 <span class="fc" id="L153">                     int portInt = Integer.parseInt(port);</span>
154 <span class="fc" id="L154">                     socket = new Socket(host, portInt);</span>
155                         
156 <span class="nc" id="L156">             } catch (UnknownHostException e) {</span>
157 <span class="nc" id="L157">                     throw new Exception(&quot;remote host [&quot; + host + &quot;]connect fail&quot; + StringUtil.getStackTrace(e));</span>
158 <span class="nc" id="L158">             } catch (IOException e) {</span>
159 <span class="nc" id="L159">                     throw new Exception(&quot;create socket IOException &quot; + StringUtil.getStackTrace(e));</span>
160 <span class="fc" id="L160">             }</span>
161                 try {
162 <span class="fc" id="L162">                     socket.setSoTimeout(this.read_timeout);</span>
163 <span class="fc" id="L163">                     socket.setTcpNoDelay(true);</span>
164 <span class="fc" id="L164">                     socket.setKeepAlive(true);</span>
165 <span class="nc" id="L165">             } catch (SocketException e) {</span>
166 <span class="nc" id="L166">                     throw new Exception(&quot; SocketException &quot; + StringUtil.getStackTrace(e));</span>
167 <span class="fc" id="L167">             }</span>
168                 try {
169 <span class="fc" id="L169">                     dos = new BufferedOutputStream(socket.getOutputStream());</span>
170                         
171 <span class="fc" id="L171">                     Msg  msg = MessageUtil.putLoginMsg(user,password);</span>
172                         
173                         try {
174 <span class="fc" id="L174">                             log.debug(&quot;send login message &quot;+msg.toString(false));</span>
175 <span class="fc" id="L175">                             MessageUtil.writeMsg(msg,dos);</span>
176                                 
177 <span class="nc" id="L177">                     } catch (Exception e) {</span>
178 <span class="nc" id="L178">                             log.error(&quot;send login message is fail &quot;+StringUtil.getStackTrace(e));</span>
179 <span class="fc" id="L179">                     }</span>
180
181 <span class="fc" id="L181">                     is = new BufferedInputStream(socket.getInputStream());</span>
182                         
183 <span class="nc" id="L183">             } catch (SocketException e) {</span>
184 <span class="nc" id="L184">                     throw new Exception(StringUtil.getStackTrace(e));</span>
185 <span class="fc" id="L185">             }</span>
186 <span class="fc" id="L186">     }</span>
187         
188         private boolean ackLoginAlarm(Msg msg) throws Exception {
189                 
190 <span class="fc" id="L190">             boolean is_success = false;</span>
191                 try {
192 <span class="fc" id="L192">                     String loginres = msg.getBody();</span>
193                         //ackLoginAlarm; result=fail(succ); resDesc=username-error
194 <span class="fc" id="L194">                     String [] loginbody = loginres.split(&quot;;&quot;);</span>
195 <span class="pc bpc" id="L195" title="1 of 2 branches missed.">                 if(loginbody.length &gt; 1){</span>
196 <span class="fc bfc" id="L196" title="All 2 branches covered.">                         for(String str :loginbody){</span>
197 <span class="fc bfc" id="L197" title="All 2 branches covered.">                     if(str.contains(&quot;=&quot;)){</span>
198 <span class="fc" id="L198">                             String [] paras1 = str.split(&quot;=&quot;,-1);</span>
199 <span class="fc bfc" id="L199" title="All 2 branches covered.">                         if(&quot;result&quot;.equalsIgnoreCase(paras1[0].trim())){</span>
200 <span class="pc bpc" id="L200" title="1 of 2 branches missed.">                                                 if(&quot;succ&quot;.equalsIgnoreCase(paras1[1].trim())){</span>
201 <span class="fc" id="L201">                                                             is_success = true;</span>
202                                                         }else{
203 <span class="nc" id="L203">                                                             is_success = false;</span>
204                                                         }
205                                                 }
206                             }
207                                 }
208                         }else {
209 <span class="nc" id="L209">                             log.error(&quot;login ack body Incorrect formatbody=&quot; + loginres);</span>
210                         }
211                         
212                         
213 <span class="nc" id="L213">             } catch (Exception e) {</span>
214 <span class="nc" id="L214">                     log.error(&quot;pocess login ack fail&quot;+StringUtil.getStackTrace(e));</span>
215 <span class="fc" id="L215">             }</span>
216 <span class="pc bpc" id="L216" title="1 of 2 branches missed.">         if (is_success) {</span>
217 <span class="fc" id="L217">                     log.info(&quot;login sucess receive login ack &quot; + msg.getBody());</span>
218                 } else {
219 <span class="nc" id="L219">                     log.error(&quot;login fail receive login ack  &quot; + msg.getBody());</span>
220 <span class="nc" id="L220">                     this.close();</span>
221 <span class="nc" id="L221">                     this.isStop = true;</span>
222 <span class="nc" id="L222">                     throw new Exception(&quot;login fail quit&quot;);</span>
223                 }
224 <span class="fc" id="L224">             return is_success;</span>
225         }
226
227         public void close() {
228
229 <span class="nc bnc" id="L229" title="All 2 branches missed.">          if(heartBeat != null){</span>
230 <span class="nc" id="L230">                     heartBeat.setStop(true);</span>
231                 }
232                 
233 <span class="nc bnc" id="L233" title="All 2 branches missed.">          if (is != null) {</span>
234                         try {
235 <span class="nc" id="L235">                             is.close();</span>
236 <span class="nc" id="L236">                     } catch (IOException e) {</span>
237                         } finally {
238 <span class="nc" id="L238">                             is = null;</span>
239 <span class="nc" id="L239">                     }</span>
240                 }
241
242 <span class="nc bnc" id="L242" title="All 2 branches missed.">          if (dos != null) {</span>
243                         try {
244 <span class="nc" id="L244">                             dos.close();</span>
245 <span class="nc" id="L245">                     } catch (IOException e) {</span>
246                         } finally {
247 <span class="nc" id="L247">                             dos = null;</span>
248 <span class="nc" id="L248">                     }</span>
249                 }
250
251 <span class="nc bnc" id="L251" title="All 2 branches missed.">          if (socket != null) {</span>
252                         try {
253 <span class="nc" id="L253">                             socket.close();</span>
254 <span class="nc" id="L254">                     } catch (IOException e) {</span>
255                         } finally {
256 <span class="nc" id="L256">                             socket = null;</span>
257 <span class="nc" id="L257">                     }</span>
258
259                 }
260 <span class="nc" id="L260">     }</span>
261         
262         public void  reinit() {
263 <span class="nc" id="L263">             int time = 0;</span>
264 <span class="nc" id="L264">             close();</span>
265 <span class="nc bnc" id="L265" title="All 2 branches missed.">          while(!this.isStop) {</span>
266 <span class="nc" id="L266">                     close();</span>
267 <span class="nc" id="L267">                     time++;</span>
268                         try {
269 <span class="nc" id="L269">                             Thread.sleep(1000 * 30);</span>
270 <span class="nc" id="L270">                             init();</span>
271 <span class="nc" id="L271">                             return;</span>
272 <span class="nc" id="L272">                     } catch (Exception e) {</span>
273 <span class="nc" id="L273">                             log.error(&quot;Number [&quot;+time+&quot;]reconnect [&quot;+collectVo.getIP()+&quot;]fail&quot; );</span>
274 <span class="nc" id="L274">                     }</span>
275                 }
276 <span class="nc" id="L276">     }</span>
277
278         /**
279          * @param isStop the isStop to set
280          */
281         public void setStop(boolean isStop) {
282 <span class="nc" id="L282">             this.isStop = isStop;</span>
283 <span class="nc" id="L283">     }</span>
284
285         /**
286          * @return the heartBeat
287          */
288         public HeartBeat getHeartBeat() {
289 <span class="nc" id="L289">             return heartBeat;</span>
290         }
291         
292         
293         
294 }
295 </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>