2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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;
23 import com.tailf.jnc.JNCException;
25 import java.io.IOException;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.netconfsimulator.netconfcore.configuration.NetconfConfigurationService;
29 import org.onap.netconfsimulator.netconfcore.model.LoadModelResponse;
30 import org.onap.netconfsimulator.netconfcore.model.NetconfModelLoaderService;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.DeleteMapping;
35 import org.springframework.web.bind.annotation.GetMapping;
36 import org.springframework.web.bind.annotation.PathVariable;
37 import org.springframework.web.bind.annotation.PostMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestPart;
41 import org.springframework.web.bind.annotation.ResponseStatus;
42 import org.springframework.web.bind.annotation.RestController;
43 import org.springframework.web.multipart.MultipartFile;
47 @RequestMapping("netconf")
48 class NetconfController {
50 private final NetconfConfigurationService netconfService;
51 private final NetconfModelLoaderService netconfModelLoaderService;
54 NetconfController(NetconfConfigurationService netconfService,
55 NetconfModelLoaderService netconfModelLoaderService) {
56 this.netconfService = netconfService;
57 this.netconfModelLoaderService = netconfModelLoaderService;
60 @GetMapping(value = "get", produces = "application/xml")
61 ResponseEntity<String> getNetconfConfiguration() throws IOException, JNCException {
62 return ResponseEntity.ok(netconfService.getCurrentConfiguration());
65 @GetMapping(value = "get/{model}/{container}", produces = "application/xml")
66 ResponseEntity<String> getNetconfConfiguration(@PathVariable String model,
67 @PathVariable String container)
69 ResponseEntity<String> entity;
71 entity = ResponseEntity.ok(netconfService.getCurrentConfiguration(model, container));
72 } catch (JNCException exception) {
73 log.error("Get configuration for model {} and container {} failed.", model, container,
75 entity = ResponseEntity.badRequest().body(exception.toString());
80 @PostMapping(value = "edit-config", produces = "application/xml")
81 @ResponseStatus(HttpStatus.ACCEPTED)
82 ResponseEntity<String> editConfig(@RequestPart("editConfigXml") MultipartFile editConfig)
83 throws IOException, JNCException {
84 log.info("Loading updated configuration");
85 if (editConfig == null || editConfig.isEmpty()) {
86 throw new IllegalArgumentException("No XML file with proper name: editConfigXml found.");
89 .status(HttpStatus.ACCEPTED)
90 .body(netconfService.editCurrentConfiguration(editConfig));
93 @PostMapping("model/{moduleName}")
94 ResponseEntity<String> loadNewYangModel(@RequestBody MultipartFile yangModel,
95 @RequestBody MultipartFile initialConfig, @PathVariable String moduleName)
97 LoadModelResponse response = netconfModelLoaderService.loadYangModel(yangModel, initialConfig, moduleName);
99 .status(response.getStatusCode())
100 .body(response.getMessage());
103 @DeleteMapping("model/{modelName}")
104 ResponseEntity<String> deleteYangModel(@PathVariable String modelName)
106 LoadModelResponse response = netconfModelLoaderService.deleteYangModel(modelName);
107 return ResponseEntity
108 .status(response.getStatusCode())
109 .body(response.getMessage());