First part of onap rename
[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  * 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         //int readByte = 1;
75         // Read data with timeout
76         Callable<Boolean> readTask = new Callable<Boolean>() {
77             @Override
78             public Boolean call() throws Exception {
79                 int c;
80                 while ((c = in.read(buf)) > 0) {
81                     if (c > 0) {
82                         message.append(buf, 0, c);
83                         if (message.isCompleted()) {
84                             break;
85                         }
86                     }
87                 }
88
89                 if (c < 0) {
90                     return false;
91                 }
92                 return true;
93             }
94         };
95
96         Future<Boolean> future = executor.submit(readTask);
97         Boolean status;
98         try {
99             status = future.get(maxWaitingTime, TimeUnit.MILLISECONDS);
100         } catch (Exception e) {
101             throw new IOException(e);
102         }
103
104         if (status == false) {
105             throw new IOException("Failed to read netconf message");
106         }
107
108
109         String text = message.getText();
110         if (text != null) {
111             text = text.trim();
112         }
113         if (LOG.isDebugEnabled()) {
114             LOG.debug("Received message from netconf device:\n" + text);
115         }
116         return text;
117     }
118
119     /**
120      * Sends netconf message with provided text (adds netconf frame characters and sends the message).
121      *
122      * @param text text of message to be sent to netconf device
123      * @throws IOException
124      */
125     public void sendMessage(final String text) throws IOException {
126         if (LOG.isDebugEnabled()) {
127             LOG.debug("Sending message to netconf device:\n" + text);
128         }
129         out.write(new NetconfMessage(text).getFrame());
130         out.flush();
131     }
132 }