covered cases for NetconfAdaptor2
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / onap / appc / adapter / netconf / internal / NetconfMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.netconf.internal;
25
26 import java.io.ByteArrayOutputStream;
27
28 class NetconfMessage {
29
30     private static final String EOM = "]]>]]>";
31
32     private String text;
33     private MessageBuffer buffer = new MessageBuffer();
34     private int eomNotch;
35
36     NetconfMessage() {
37     }
38
39     NetconfMessage(String text) {
40         if(text == null) {
41             throw new NullPointerException("Netconf message payload is null");
42         }
43         append(text.getBytes(), 0, text.length());
44         if(this.text == null) {
45             this.text = text;
46         }
47     }
48
49     void append(byte[] bytes, int start, int finish) {
50         boolean eomFound = false;
51         int end = finish;
52         for(int i = start; i < end; i++) {
53             if(bytes[i] == EOM.charAt(eomNotch)) {
54                 // advance notch
55                 eomNotch++;
56             } else {
57                 // reset notch
58                 eomNotch = 0;
59             }
60             if(eomNotch == EOM.length()) {
61                 // end of message found
62                 eomFound = true;
63                 end = i + 1;
64                 break;
65             }
66         }
67         buffer.write(bytes, start, end);
68         if(eomFound) {
69             text = new String(buffer.getBytes(), 0, buffer.size() - EOM.length());
70             buffer.reset();
71         }
72     }
73
74     String getText() {
75         return text;
76     }
77
78     boolean isCompleted() {
79         return text != null;
80     }
81
82     byte[] getFrame() {
83         StringBuilder sb = new StringBuilder();
84         if(text != null) {
85             sb.append(text).append("\n");
86         }
87         sb.append(EOM);
88         return sb.toString().getBytes();
89     }
90
91     private class MessageBuffer extends ByteArrayOutputStream {
92
93         byte[] getBytes() {
94             return buf;
95         }
96     }
97 }