bb2a333ea47dd36181f50ab760b932f716f3517e
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.netconf.internal;
26
27 import org.onap.appc.configuration.ConfigurationFactory;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.util.concurrent.*;
35
36 /**
37  * Provides basic methods for exchanging netconf messages.
38  */
39 public class NetconfAdapter {
40
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);
44
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);
50
51     /**
52      * Constructor.
53      *
54      * @param in  InputStream this instance will read netconf messages from
55      * @param out OutputStream this instance will write netconf messages to
56      * @throws IOException
57      */
58     public NetconfAdapter(InputStream in, OutputStream out) throws IOException {
59         this.in = in;
60         this.out = out;
61     }
62
63     /**
64      * Receives netconf message from InputStream and return it's text (without netconf frame characters).
65      *
66      * @return text of message received from netconf device
67      * @throws IOException
68      */
69     public String receiveMessage() throws IOException {
70
71         final NetconfMessage message = new NetconfMessage();
72         final byte[] buf = new byte[1024];
73
74         // Read data with timeout
75         Callable<Boolean> readTask = () -> {
76             int c;
77             while ((c = in.read(buf)) > 0) {
78                 message.append(buf, 0, c);
79                 if (message.isCompleted()) {
80                     break;
81                 }
82             }
83
84             return c >= 0;
85         };
86
87         Future<Boolean> future = executor.submit(readTask);
88         Boolean status;
89         try {
90             status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
91         } catch (Exception e) {
92             throw new IOException(e);
93         }
94
95         if (!status) {
96             throw new IOException("Failed to read netconf message");
97         }
98
99
100         String text = message.getText();
101         if (text != null) {
102             text = text.trim();
103         }
104         if (LOG.isDebugEnabled()) {
105             LOG.debug("Received message from netconf device:\n" + text);
106         }
107         return text;
108     }
109
110     /**
111      * Sends netconf message with provided text (adds netconf frame characters and sends the message).
112      *
113      * @param text text of message to be sent to netconf device
114      * @throws IOException
115      */
116     public void sendMessage(final String text) throws IOException {
117         if (LOG.isDebugEnabled()) {
118             LOG.debug("Sending message to netconf device:\n" + text);
119         }
120         out.write(new NetconfMessage(text).getFrame());
121         out.flush();
122     }
123 }