First part of onap rename
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.netconf.internal;
26
27 import java.io.ByteArrayOutputStream;
28
29 class NetconfMessage {
30
31     private static final String EOM = "]]>]]>";
32
33     private String text;
34     private MessageBuffer buffer = new MessageBuffer();
35     private int eomNotch;
36
37     NetconfMessage() {
38     }
39
40     NetconfMessage(String text) {
41         if(text == null) {
42             throw new NullPointerException("Netconf message payload is null");
43         }
44         append(text.getBytes(), 0, text.length());
45         if(this.text == null) {
46             this.text = text;
47         }
48     }
49
50     void append(byte[] bytes, int start, int end) {
51         boolean eomFound = false;
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 }