sonar fix:Exception handling in emsdriver
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / collector / alarm / MessageUtil.java
1 /*
2  * Copyright 2017 BOCO Corporation.  CMCC 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 package org.onap.vfc.nfvo.emsdriver.collector.alarm;
17
18 import java.io.*;
19
20
21 public class MessageUtil {
22     public static final String MSG_BODY_ENCODE_CHARSET = "UTF-8";
23     public static final int MSG_BUF_SIZE = 8096;
24
25     public static Msg putLoginMsg(String user, String passwd) {
26         String body = String.format(Msg.reqLoginAlarm, user, passwd, "msg");
27         return new Msg(body, MsgType.reqLoginAlarm);
28
29     }
30
31     public static Msg putLoginFtp(String user, String passwd) {
32         String body = String.format(Msg.reqLoginAlarm, user, passwd, "ftp");
33         return new Msg(body, MsgType.reqLoginAlarm);
34
35     }
36
37     public static Msg putSyncMsg(int reqId, int alarmSeq) {
38         String body = String.format(Msg.syncAlarmMessageMsg, reqId, alarmSeq);
39         return new Msg(body, MsgType.reqSyncAlarmMsg);
40
41     }
42
43     public static Msg putHeartBeatMsg(int reqId) {
44         String body = String.format(Msg.reqHeartBeat, reqId);
45         return new Msg(body, MsgType.reqHeartBeat);
46
47     }
48
49     public static Msg reqSyncAlarmFile(int reqId, String startTime, String endTime) {
50         String body = String.format(Msg.syncActiveAlarmFileMsg, reqId, startTime, endTime);
51         return new Msg(body, MsgType.reqSyncAlarmFile);
52     }
53
54     public static Msg reqSyncAlarmFileByAlarmSeq(int reqId, int alarmSeq) {
55         String body = String.format(Msg.syncAlarmMessageByalarmSeq, reqId, alarmSeq);
56         return new Msg(body, MsgType.reqSyncAlarmFile);
57     }
58
59     public static Msg reqSyncAlarmFileByTime(int reqId, String startTime, String endTime) {
60         String body = String.format(Msg.syncAlarmFileMsg, reqId, startTime, endTime);
61         return new Msg(body, MsgType.reqSyncAlarmFile);
62     }
63
64     public static Msg closeConnAlarmMsg() {
65         String body = String.format(Msg.disconnectMsg);
66         return new Msg(body, MsgType.closeConnAlarm);
67     }
68
69     public static Msg readOneMsg(BufferedInputStream is) throws IOException {
70         byte[] inputB = new byte[9];
71
72         Msg msg = new Msg();
73         try( 
74             DataInputStream dis = new DataInputStream(is);
75             ByteArrayInputStream bais = new ByteArrayInputStream(inputB);
76             DataInputStream ois = new DataInputStream(bais)){
77             dis.readFully(inputB);
78             short startSign = ois.readShort();
79             if (startSign != Msg.StartSign) {
80                 throw new IOException("start sign is [" + Msg.StartSign
81                         + "],not is [" + startSign + "]");
82             }
83             int msgType = ois.readByte();
84             msg.setMsgType(MsgType.getMsgTypeValue(msgType));
85             int timeStamp = ois.readInt();
86             msg.setTimeStamp(timeStamp);
87             int bodylength = ois.readShort();
88             msg.setLenOfBody(bodylength);
89             byte[] b = new byte[bodylength];
90             dis.readFully(b);
91             msg.newBodyfromBytes(b);
92         } catch (Exception e) {
93             throw new IOException("readOneMsg",e);
94         } 
95
96         return msg;
97     }
98
99     public static void writeMsg(Msg msg, BufferedOutputStream dout) throws IOException {
100         try( 
101             ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(9);
102             DataOutputStream oos = new DataOutputStream(byteOutStream)){
103             oos.writeShort(Msg.StartSign);
104             oos.writeByte(msg.getMsgType().value);
105             oos.writeInt(Msg.creatMsgTimeStamp());
106             oos.writeShort(msg.getBodyLenNow());
107
108             dout.write(byteOutStream.toByteArray());
109
110             dout.write(msg.getBodyBytes());
111             dout.flush();
112         } catch (Exception e) {
113             throw new IOException("writeMsg",e);
114         } 
115
116     }
117
118 }