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.onap.appc.configuration.ConfigurationFactory;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.util.concurrent.*;
37 * Provides basic methods for exchanging netconf messages.
39 public class NetconfAdapter {
41 private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter.class);
42 private static final long MAX_WAITING_TIME = 1800000;
43 private static ExecutorService executor = Executors.newFixedThreadPool(5);
45 // device input stream
46 private InputStream in;
47 // device output stream
48 private OutputStream out;
49 private long maxWaitingTime = ConfigurationFactory.getConfiguration().getLongProperty("org.onap.appc.netconf.recv.timeout", MAX_WAITING_TIME);
54 * @param in InputStream this instance will read netconf messages from
55 * @param out OutputStream this instance will write netconf messages to
58 public NetconfAdapter(InputStream in, OutputStream out) throws IOException {
64 * Receives netconf message from InputStream and return it's text (without netconf frame characters).
66 * @return text of message received from netconf device
69 public String receiveMessage() throws IOException {
71 final NetconfMessage message = new NetconfMessage();
72 final byte[] buf = new byte[1024];
75 // Read data with timeout
76 Callable<Boolean> readTask = new Callable<Boolean>() {
78 public Boolean call() throws Exception {
80 while ((c = in.read(buf)) > 0) {
82 message.append(buf, 0, c);
83 if (message.isCompleted()) {
96 Future<Boolean> future = executor.submit(readTask);
99 status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
100 } catch (Exception e) {
101 throw new IOException(e);
104 if (status == false) {
105 throw new IOException("Failed to read netconf message");
109 String text = message.getText();
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("Received message from netconf device:\n" + text);
120 * Sends netconf message with provided text (adds netconf frame characters and sends the message).
122 * @param text text of message to be sent to netconf device
123 * @throws IOException
125 public void sendMessage(final String text) throws IOException {
126 if (LOG.isDebugEnabled()) {
127 LOG.debug("Sending message to netconf device:\n" + text);
129 out.write(new NetconfMessage(text).getFrame());