567a45a967ea817750e9439417a9a19378a9c001
[appc.git] /
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      * @return InputStream this instance will read netconf messages from.
57      */
58     public InputStream getIn() {
59         return in;
60     }
61
62     /**
63      * @return OutputStream this instance will write netconf messages to.
64      */
65     public OutputStream getOut() {
66         return out;
67     }
68
69     /**
70      * Receives netconf message from InputStream and return it's text (without netconf frame characters).
71      *
72      * @return text of message received from netconf device
73      * @throws IOException
74      */
75     public String receiveMessage() throws IOException {
76         NetconfMessage message = new NetconfMessage();
77         byte[] buf = new byte[1024];
78         int c;
79         while((c = pipedInOut.read(buf)) > 0) {
80                 message.append(buf, 0, c);
81                 if (message.isCompleted()) {
82                     break;
83                 }
84         }
85         String text = message.getText();
86         if(LOG.isDebugEnabled()) {
87             LOG.debug("Received message from netconf device:\n" + text);
88         }
89         return text;
90     }
91
92     /**
93      * Sends netconf message with provided text (adds netconf frame characters and sends the message).
94      *
95      * @param text text of message to be sent to netconf device
96      * @throws IOException
97      */
98     public void sendMessage(final String text) throws IOException {
99         if(LOG.isDebugEnabled()) {
100             LOG.debug("Sending message to netconf device:\n" + text);
101         }
102         pipedOutIn.write(new NetconfMessage(text).getFrame());
103     }
104 }