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