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