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