aboutsummaryrefslogtreecommitdiffstats
path: root/data-migrator/src/main/java/org/onap/sdnc/oam/datamigrator/common/RestconfClient.java
blob: b7722b6dea743d9d1cc1205be99599a15968fdd4 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * ============LICENSE_START=======================================================
 * ONAP : SDNC
 * ================================================================================
 * Copyright 2019 AMDOCS
 *=================================================================================
 * 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.onap.sdnc.oam.datamigrator.common;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.onap.sdnc.oam.datamigrator.exceptions.RestconfException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Base64;

public class RestconfClient {

    private HttpURLConnection httpConn = null;
    private final String host ;
    private final String user ;
    private final String password ;
    private static final String CONFIG_PATH = "/restconf/config/";
    private static final String CONTENT_TYPE_JSON = "application/json";
    private final Logger log = LoggerFactory.getLogger(RestconfClient.class);

    public RestconfClient (String host , String user , String password){
        this.host = host;
        this.user = user;
        this.password = password;
    }

    private class SdncAuthenticator extends Authenticator {

        private final String user;
        private final String passwd;

        SdncAuthenticator(String user, String passwd) {
            this.user = user;
            this.passwd = passwd;
        }
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, passwd.toCharArray());
        }
    }

    public JsonObject get(String path) throws RestconfException {
            String getResponse = send(path,"GET",CONTENT_TYPE_JSON,"");
            JsonParser parser = new JsonParser();
            return parser.parse(getResponse).getAsJsonObject();
    }

    public void put(String path, String data) throws RestconfException {
            send(path,"PUT",CONTENT_TYPE_JSON, data );
    }

    private String send(String path,String method, String contentType, String msg) throws RestconfException {
        Authenticator.setDefault(new SdncAuthenticator(user, password));
        String url = host + CONFIG_PATH + path;
        try {
            URL sdncUrl = new URL(url);
            log.info("SDNC url: " + url);
            log.info("Method: " + method);
            this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
            String authStr = user + ":" + password;
            String encodedAuthStr = new String(Base64.getEncoder().encode(authStr.getBytes()));
            httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);

            httpConn.setRequestMethod(method);
            httpConn.setRequestProperty("Content-Type", contentType);
            httpConn.setRequestProperty("Accept", contentType);

            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);

            if (httpConn instanceof HttpsURLConnection) {
                HostnameVerifier hostnameVerifier = (hostname, session) -> true;
                ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
            }
            if (!method.equals("GET")) {
                log.info("Request payload: " + msg);
                httpConn.setRequestProperty("Content-Length", "" + msg.length());
                DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
                outStr.write(msg.getBytes());
                outStr.close();
            }

            BufferedReader respRdr;
            log.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());

            if (httpConn.getResponseCode() < 300) {
                respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            } else {
                respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
                log.error("Error during restconf operation: "+ method + ". URL:" + sdncUrl.toString()+". Response:"+respRdr);
                throw new RestconfException(httpConn.getResponseCode(),"Error during restconf operation: "+ method +". Response:"+respRdr);
            }

            StringBuilder respBuff = new StringBuilder();
            String respLn;
            while ((respLn = respRdr.readLine()) != null) {
                respBuff.append(respLn).append("\n");
            }
            respRdr.close();
            String respString = respBuff.toString();

            log.info("Response body :\n" + respString);
            return respString;
        }catch (IOException e){
            throw new RestconfException(500,e.getMessage(),e);
        }finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
        }
    }


}