2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.netconf.internal;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
33 * Provides basic methods for exchanging netconf messages.
35 public class NetconfAdapter2 {
37 private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter2.class);
40 private final PipedOutputStream pipedOutIn = new PipedOutputStream();
41 private PipedInputStream in;
43 private final PipedInputStream pipedInOut = new PipedInputStream();
44 private PipedOutputStream out;
51 public NetconfAdapter2() throws IOException {
52 in = new PipedInputStream(pipedOutIn);
53 out = new PipedOutputStream(pipedInOut);
57 * @return InputStream this instance will read netconf messages from.
59 public InputStream getIn() {
64 * @return OutputStream this instance will write netconf messages to.
66 public OutputStream getOut() {
71 * Receives netconf message from InputStream and return it's text (without netconf frame characters).
73 * @return text of message received from netconf device
76 public String receiveMessage() throws IOException {
77 NetconfMessage message = new NetconfMessage();
78 byte[] buf = new byte[1024];
80 while((c = pipedInOut.read(buf)) > 0) {
81 message.append(buf, 0, c);
82 if (message.isCompleted()) {
86 String text = message.getText();
87 if(LOG.isDebugEnabled()) {
88 LOG.debug("Received message from netconf device:\n" + text);
94 * Sends netconf message with provided text (adds netconf frame characters and sends the message).
96 * @param text text of message to be sent to netconf device
99 public void sendMessage(final String text) throws IOException {
100 if(LOG.isDebugEnabled()) {
101 LOG.debug("Sending message to netconf device:\n" + text);
103 pipedOutIn.write(new NetconfMessage(text).getFrame());