2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.netconfsimulator.netconfcore.model;
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;
41 public class NetconfModelLoaderService {
43 private static final Logger LOGGER = LoggerFactory.getLogger(NetconfModelLoaderService.class);
45 @Value("${netconf.address}")
46 private String netconfIp;
48 @Value("${netconf.model-loader.port}")
49 private String modelLoaderPort;
51 private final HttpClient httpClient;
54 public NetconfModelLoaderService(HttpClient httpClient) {
55 this.httpClient = httpClient;
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);
65 public LoadModelResponse loadYangModel(MultipartFile yangModel, MultipartFile initialConfig, String moduleName)
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)
75 httpPost.setEntity(httpEntity);
76 HttpResponse response = httpClient.execute(httpPost);
77 return parseResponse(response);
80 String getBackendAddress() {
81 return String.format("http://%s:%s/model", netconfIp, modelLoaderPort);
84 String getDeleteAddress(String yangModelName) {
85 return String.format("%s?yangModelName=%s", getBackendAddress(), yangModelName);
89 private LoadModelResponse parseResponse(HttpResponse response) throws IOException {
90 int statusCode = response.getStatusLine().getStatusCode();
91 String responseBody = EntityUtils.toString(response.getEntity());
93 logResponse(statusCode, responseBody);
94 return new LoadModelResponse(statusCode, responseBody);
97 private void logResponse(int statusCode, String responseBody) {
98 if (statusCode >= HttpStatus.BAD_REQUEST.value()) {
99 LOGGER.error(responseBody);
101 LOGGER.info(responseBody);