2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.netconf.internal;
25 import org.openecomp.appc.configuration.ConfigurationFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.util.concurrent.*;
35 * Provides basic methods for exchanging netconf messages.
37 public class NetconfAdapter {
39 private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter.class);
40 private static final long MAX_WAITING_TIME = 1800000;
41 private static ExecutorService executor = Executors.newFixedThreadPool(5);
43 // device input stream
44 private InputStream in;
45 // device output stream
46 private OutputStream out;
47 private long maxWaitingTime = ConfigurationFactory.getConfiguration().getLongProperty("org.openecomp.appc.netconf.recv.timeout", MAX_WAITING_TIME);
52 * @param in InputStream this instance will read netconf messages from
53 * @param out OutputStream this instance will write netconf messages to
56 public NetconfAdapter(InputStream in, OutputStream out) throws IOException {
62 * Receives netconf message from InputStream and return it's text (without netconf frame characters).
64 * @return text of message received from netconf device
67 public String receiveMessage() throws IOException {
69 final NetconfMessage message = new NetconfMessage();
70 final byte[] buf = new byte[1024];
73 // Read data with timeout
74 Callable<Boolean> readTask = new Callable<Boolean>() {
76 public Boolean call() throws Exception {
78 while ((c = in.read(buf)) > 0) {
80 message.append(buf, 0, c);
81 if (message.isCompleted()) {
94 Future<Boolean> future = executor.submit(readTask);
97 status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
98 } catch (Exception e) {
99 throw new IOException(e);
102 if (status == false) {
103 throw new IOException("Failed to read netconf message");
107 String text = message.getText();
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("Received message from netconf device:\n" + text);
118 * Sends netconf message with provided text (adds netconf frame characters and sends the message).
120 * @param text text of message to be sent to netconf device
121 * @throws IOException
123 public void sendMessage(final String text) throws IOException {
124 if (LOG.isDebugEnabled()) {
125 LOG.debug("Sending message to netconf device:\n" + text);
127 out.write(new NetconfMessage(text).getFrame());