7e0739579de470f50511437deb6a6cbb4c4262f5
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.netconfsimulator.netconfcore.model;
22
23 import java.io.IOException;
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.client.methods.HttpDelete;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.entity.ContentType;
30 import org.apache.http.entity.mime.MultipartEntityBuilder;
31 import org.apache.http.util.EntityUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.stereotype.Service;
38 import org.springframework.web.multipart.MultipartFile;
39
40 @Service
41 public class NetconfModelLoaderService {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(NetconfModelLoaderService.class);
44
45     @Value("${netconf.address}")
46     private String netconfIp;
47
48     @Value("${netconf.model-loader.port}")
49     private String modelLoaderPort;
50
51     private final HttpClient httpClient;
52
53     @Autowired
54     public NetconfModelLoaderService(HttpClient httpClient) {
55         this.httpClient = httpClient;
56     }
57
58     public LoadModelResponse deleteYangModel(String yangModelName) throws IOException {
59         String uri = getDeleteAddress(yangModelName);
60         HttpDelete httpDelete = new HttpDelete(uri);
61         HttpResponse httpResponse = httpClient.execute(httpDelete);
62         return parseResponse(httpResponse);
63     }
64
65     public LoadModelResponse loadYangModel(MultipartFile yangModel, MultipartFile initialConfig, String moduleName)
66         throws IOException {
67         HttpPost httpPost = new HttpPost(getBackendAddress());
68         HttpEntity httpEntity = MultipartEntityBuilder.create()
69             .addBinaryBody("yangModel", yangModel.getInputStream(), ContentType.MULTIPART_FORM_DATA,
70                 yangModel.getOriginalFilename())
71             .addBinaryBody("initialConfig", initialConfig.getInputStream(), ContentType.MULTIPART_FORM_DATA,
72                 initialConfig.getOriginalFilename())
73             .addTextBody("moduleName", moduleName)
74             .build();
75         httpPost.setEntity(httpEntity);
76         HttpResponse response = httpClient.execute(httpPost);
77         return parseResponse(response);
78     }
79
80     String getBackendAddress() {
81         return String.format("http://%s:%s/model", netconfIp, modelLoaderPort);
82     }
83
84     String getDeleteAddress(String yangModelName) {
85         return String.format("%s?yangModelName=%s", getBackendAddress(), yangModelName);
86     }
87
88
89     private LoadModelResponse parseResponse(HttpResponse response) throws IOException {
90         int statusCode = response.getStatusLine().getStatusCode();
91         String responseBody = EntityUtils.toString(response.getEntity());
92
93         logResponse(statusCode, responseBody);
94         return new LoadModelResponse(statusCode, responseBody);
95     }
96
97     private void logResponse(int statusCode, String responseBody) {
98         if (statusCode >= HttpStatus.BAD_REQUEST.value()) {
99             LOGGER.error(responseBody);
100         } else {
101             LOGGER.info(responseBody);
102         }
103     }
104 }