2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.netconf.internal;
26 import org.onap.appc.configuration.ConfigurationFactory;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.concurrent.*;
36 * Provides basic methods for exchanging netconf messages.
38 public class NetconfAdapter {
40 private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter.class);
41 private static final long MAX_WAITING_TIME = 1800000;
42 private static ExecutorService executor = Executors.newFixedThreadPool(5);
44 // device input stream
45 private InputStream in;
46 // device output stream
47 private OutputStream out;
48 private long maxWaitingTime = ConfigurationFactory.getConfiguration().getLongProperty("org.onap.appc.netconf.recv.timeout", MAX_WAITING_TIME);
53 * @param in InputStream this instance will read netconf messages from
54 * @param out OutputStream this instance will write netconf messages to
57 public NetconfAdapter(InputStream in, OutputStream out) throws IOException {
63 * Receives netconf message from InputStream and return it's text (without netconf frame characters).
65 * @return text of message received from netconf device
68 public String receiveMessage() throws IOException {
70 final NetconfMessage message = new NetconfMessage();
71 final byte[] buf = new byte[1024];
73 // Read data with timeout
74 Callable<Boolean> readTask = () -> {
76 while ((c = in.read(buf)) > 0) {
77 message.append(buf, 0, c);
78 if (message.isCompleted()) {
86 Future<Boolean> future = executor.submit(readTask);
89 status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
90 } catch (Exception e) {
91 throw new IOException(e);
95 throw new IOException("Failed to read netconf message");
99 String text = message.getText();
103 if (LOG.isDebugEnabled()) {
104 LOG.debug("Received message from netconf device:\n" + text);
110 * Sends netconf message with provided text (adds netconf frame characters and sends the message).
112 * @param text text of message to be sent to netconf device
113 * @throws IOException
115 public void sendMessage(final String text) throws IOException {
116 if (LOG.isDebugEnabled()) {
117 LOG.debug("Sending message to netconf device:\n" + text);
119 out.write(new NetconfMessage(text).getFrame());