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