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