2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.adapter.netconf.internal;
24 import org.openecomp.appc.configuration.ConfigurationFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.util.concurrent.*;
34 * Provides basic methods for exchanging netconf messages.
36 public class NetconfAdapter {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter.class);
39 private static final long MAX_WAITING_TIME = 1800000;
40 private static ExecutorService executor = Executors.newFixedThreadPool(5);
42 // device input stream
43 private InputStream in;
44 // device output stream
45 private OutputStream out;
46 private long maxWaitingTime = ConfigurationFactory.getConfiguration().getLongProperty("org.openecomp.appc.netconf.recv.timeout", MAX_WAITING_TIME);
51 * @param in InputStream this instance will read netconf messages from
52 * @param out OutputStream this instance will write netconf messages to
55 public NetconfAdapter(InputStream in, OutputStream out) throws IOException {
61 * Receives netconf message from InputStream and return it's text (without netconf frame characters).
63 * @return text of message received from netconf device
66 public String receiveMessage() throws IOException {
68 final NetconfMessage message = new NetconfMessage();
69 final byte[] buf = new byte[1024];
72 // Read data with timeout
73 Callable<Boolean> readTask = new Callable<Boolean>() {
75 public Boolean call() throws Exception {
77 while ((c = in.read(buf)) > 0) {
79 message.append(buf, 0, c);
80 if (message.isCompleted()) {
93 Future<Boolean> future = executor.submit(readTask);
96 status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
97 } catch (Exception e) {
98 throw new IOException(e);
101 if (status == false) {
102 throw new IOException("Failed to read netconf message");
106 String text = message.getText();
110 if (LOG.isDebugEnabled()) {
111 LOG.debug("Received message from netconf device:\n" + text);
117 * Sends netconf message with provided text (adds netconf frame characters and sends the message).
119 * @param text text of message to be sent to netconf device
120 * @throws IOException
122 public void sendMessage(final String text) throws IOException {
123 if (LOG.isDebugEnabled()) {
124 LOG.debug("Sending message to netconf device:\n" + text);
126 out.write(new NetconfMessage(text).getFrame());