Modify groupId for ems driver
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / 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.nfvo.emsdriver.collector.alarm;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24
25
26
27 public class MessageUtil
28 {
29         public static String MSG_BODY_ENCODE_CHARSET="UTF-8";
30         public static int MSG_BUF_SIZE=8096 ;
31         
32         public static Msg putLoginMsg(String user,String passwd)
33         {
34                 String body = String.format(Msg.reqLoginAlarm, user,passwd,"msg");
35                 Msg msg = new Msg(body,MsgType.reqLoginAlarm);
36                 
37
38                 return msg;
39                 
40         }
41         public static Msg putLoginFtp(String user,String passwd)
42         {
43                 String body = String.format(Msg.reqLoginAlarm, user,passwd,"ftp");
44                 Msg msg = new Msg(body,MsgType.reqLoginAlarm);
45                 
46
47                 return msg;
48                 
49         }
50         
51         public static Msg putSyncMsg(int reqId,int alarmSeq)
52         {
53                 String body = String.format(Msg.syncAlarmMessageMsg, reqId,alarmSeq);
54                 Msg msg = new Msg(body,MsgType.reqSyncAlarmMsg);
55                 
56
57                 return msg;
58                 
59         }
60         
61         public static Msg putHeartBeatMsg(int reqId)
62         {
63                 String body = String.format(Msg.reqHeartBeat, reqId);
64                 Msg msg = new Msg(body,MsgType.reqHeartBeat);
65                 return msg;
66                 
67         }
68         
69         public static Msg reqSyncAlarmFile(int reqId, String startTime,String endTime) {
70                 String body = String.format(Msg.syncActiveAlarmFileMsg, reqId,startTime,endTime);
71                 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
72                 return msg;
73         }
74         
75         public static Msg reqSyncAlarmFileByAlarmSeq(int reqId, int alarmSeq) {
76                 String body = String.format(Msg.syncAlarmMessageByalarmSeq, reqId,alarmSeq);
77                 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
78                 return msg;
79         }
80         
81         public static Msg reqSyncAlarmFileByTime(int reqId, String startTime,String endTime) {
82                 String body = String.format(Msg.syncAlarmFileMsg, reqId,startTime,endTime);
83                 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
84                 return msg;
85         }
86         
87         public static Msg closeConnAlarmMsg()
88         {
89                 String body = String.format(Msg.disconnectMsg);
90                 Msg msg = new Msg(body,MsgType.closeConnAlarm);
91                 return msg;
92         }
93
94         public static Msg readOneMsg(BufferedInputStream is) throws Exception
95         {
96                 byte[] inputB = new byte[9];
97                 
98                 ByteArrayInputStream bais = null;
99                 DataInputStream ois = null;
100                 
101                 Msg msg = new Msg();
102                 try {
103                         DataInputStream dis = new DataInputStream(is);
104                         dis.readFully(inputB);
105                         bais = new ByteArrayInputStream(inputB);
106                         ois = new DataInputStream(bais);
107                         short StartSign = ois.readShort();
108                         if (StartSign != Msg.StartSign) {
109                                 throw new Exception("start sign is [" + Msg.StartSign
110                                                 + "],not is [" + StartSign + "]");
111                         }
112                         int msgType = ois.readByte();
113                         msg.setMsgType(MsgType.getMsgTypeValue(msgType));
114                         int timeStamp = ois.readInt();
115                         msg.setTimeStamp(timeStamp);
116                         int bodylength = ois.readShort();
117                         msg.setLenOfBody(bodylength);
118                         byte b[] = new byte[bodylength];
119                         dis.readFully(b);
120                         msg.newBodyfromBytes(b);
121                 } catch (Exception e) {
122                         throw new Exception(e);
123                 }finally{
124                         if(bais != null){
125                                 bais.close();
126                         }
127                         if(ois != null){
128                                 ois.close();
129                         }
130                 }
131                 
132                 return msg;
133         }
134         
135         public static void writeMsg(Msg msg,BufferedOutputStream dout) throws Exception{
136                 
137                 ByteArrayOutputStream byteOutStream = null;
138                 DataOutputStream oos = null;
139                 try {
140                         byteOutStream = new ByteArrayOutputStream(9);
141                         oos = new DataOutputStream(byteOutStream);
142                         oos.writeShort(Msg.StartSign);
143                         oos.writeByte(msg.getMsgType().value);
144                         oos.writeInt(Msg.creatMsgTimeStamp());
145                         oos.writeShort(msg.getBodyLenNow());
146                         
147                         dout.write(byteOutStream.toByteArray());
148                         
149                         dout.write(msg.getBodyBytes());
150                         dout.flush();
151                 } catch (Exception e) {
152                         throw new Exception(e);
153                 }finally{
154                         if(oos != null){
155                                 oos.close();
156                         }
157                         if(byteOutStream != null){
158                                 byteOutStream.close();
159                         }
160                 }
161                 
162         }
163         
164 }