aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/openecomp/appc/adapter/netconf/internal/NetconfAdapter2.java
blob: e0f1664834e9e99616849cd0957b986514e8ae29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.adapter.netconf.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Provides basic methods for exchanging netconf messages.
 */
public class NetconfAdapter2 {

    private static final Logger LOG = LoggerFactory.getLogger(NetconfAdapter2.class);

    // device input pipe
    private final PipedOutputStream pipedOutIn = new PipedOutputStream();
    private PipedInputStream in;
    // device output pipe
    private final PipedInputStream pipedInOut = new PipedInputStream();
    private PipedOutputStream out;

    /**
     * Constructor.
     *
     * @throws IOException
     */
    public NetconfAdapter2() throws IOException {
        in = new PipedInputStream(pipedOutIn);
        out = new PipedOutputStream(pipedInOut);
    }

    /**
     * @return InputStream this instance will read netconf messages from.
     */
    public InputStream getIn() {
        return in;
    }

    /**
     * @return OutputStream this instance will write netconf messages to.
     */
    public OutputStream getOut() {
        return out;
    }

    /**
     * Receives netconf message from InputStream and return it's text (without netconf frame characters).
     *
     * @return text of message received from netconf device
     * @throws IOException
     */
    public String receiveMessage() throws IOException {
        NetconfMessage message = new NetconfMessage();
        byte[] buf = new byte[1024];
        int c;
        while((c = pipedInOut.read(buf)) > 0) {
                message.append(buf, 0, c);
                if (message.isCompleted()) {
                    break;
                }
        }
        String text = message.getText();
        if(LOG.isDebugEnabled()) {
            LOG.debug("Received message from netconf device:\n" + text);
        }
        return text;
    }

    /**
     * Sends netconf message with provided text (adds netconf frame characters and sends the message).
     *
     * @param text text of message to be sent to netconf device
     * @throws IOException
     */
    public void sendMessage(final String text) throws IOException {
        if(LOG.isDebugEnabled()) {
            LOG.debug("Sending message to netconf device:\n" + text);
        }
        pipedOutIn.write(new NetconfMessage(text).getFrame());
//        pipedOutIn.flush();
    }
}