Test coverage in netconf-oldconnector
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / onap / appc / adapter / netconf / internal / NetconfAdapter2.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.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.io.*;
30
31 /**
32  * Provides basic methods for exchanging netconf messages.
33  */
34 public class NetconfAdapter2 {
35
36     private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter2.class);
37
38     // device input pipe
39     private final PipedOutputStream pipedOutIn = new PipedOutputStream();
40     private PipedInputStream in;
41     // device output pipe
42     private final PipedInputStream pipedInOut = new PipedInputStream();
43     private PipedOutputStream out;
44
45     /**
46      * Constructor.
47      *
48      * @throws IOException
49      */
50     public NetconfAdapter2() throws IOException {
51         in = new PipedInputStream(pipedOutIn);
52         out = new PipedOutputStream(pipedInOut);
53     }
54
55     /**
56      * Constructor.
57      *
58      * @param in  InputStream this instance will read netconf messages from
59      * @param out OutputStream this instance will write netconf messages to
60      * @throws IOException
61      */
62     public NetconfAdapter2(PipedInputStream in, PipedOutputStream out) throws IOException {
63         this.in = in;
64         this.out = out;
65
66     }
67
68     /**
69      * @return InputStream this instance will read netconf messages from.
70      */
71     public InputStream getIn() {
72         return in;
73     }
74
75     /**
76      * @return OutputStream this instance will write netconf messages to.
77      */
78     public OutputStream getOut() {
79         return out;
80     }
81
82     /**
83      * Receives netconf message from InputStream and return it's text (without netconf frame characters).
84      *
85      * @return text of message received from netconf device
86      * @throws IOException
87      */
88     public String receiveMessage() throws IOException {
89         NetconfMessage message = new NetconfMessage();
90         byte[] buf = new byte[1024];
91         int c;
92         while((c = pipedInOut.read(buf)) > 0) {
93                 message.append(buf, 0, c);
94                 if (message.isCompleted()) {
95                     break;
96                 }
97         }
98         String text = message.getText();
99         if(LOG.isDebugEnabled()) {
100             LOG.debug("Received message from netconf device:\n" + text);
101         }
102         return text;
103     }
104
105     /**
106      * Sends netconf message with provided text (adds netconf frame characters and sends the message).
107      *
108      * @param text text of message to be sent to netconf device
109      * @throws IOException
110      */
111     public void sendMessage(final String text) throws IOException {
112         if(LOG.isDebugEnabled()) {
113             LOG.debug("Sending message to netconf device:\n" + text);
114         }
115         pipedOutIn.write(new NetconfMessage(text).getFrame());
116     }
117 }