cdb4a8f971794640bfa3842b81872e42247f9908
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * NETCONF-CONTROLLER
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
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;
22
23 import com.tailf.jnc.JNCException;
24
25 import java.io.IOException;
26
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;
44
45 @Slf4j
46 @RestController
47 @RequestMapping("netconf")
48 class NetconfController {
49
50     private final NetconfConfigurationService netconfService;
51     private final NetconfModelLoaderService netconfModelLoaderService;
52
53     @Autowired
54     NetconfController(NetconfConfigurationService netconfService,
55                       NetconfModelLoaderService netconfModelLoaderService) {
56         this.netconfService = netconfService;
57         this.netconfModelLoaderService = netconfModelLoaderService;
58     }
59
60     @GetMapping(value = "get", produces = "application/xml")
61     ResponseEntity<String> getNetconfConfiguration() throws IOException, JNCException {
62         return ResponseEntity.ok(netconfService.getCurrentConfiguration());
63     }
64
65     @GetMapping(value = "get/{model}/{container}", produces = "application/xml")
66     ResponseEntity<String> getNetconfConfiguration(@PathVariable String model,
67                                                    @PathVariable String container)
68             throws IOException {
69         ResponseEntity<String> entity;
70         try {
71             entity = ResponseEntity.ok(netconfService.getCurrentConfiguration(model, container));
72         } catch (JNCException exception) {
73             log.error("Get configuration for model {} and container {} failed.", model, container,
74                     exception);
75             entity = ResponseEntity.badRequest().body(exception.toString());
76         }
77         return entity;
78     }
79
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.");
87         }
88         return ResponseEntity
89                 .status(HttpStatus.ACCEPTED)
90                 .body(netconfService.editCurrentConfiguration(editConfig));
91     }
92
93     @PostMapping("model/{moduleName}")
94     ResponseEntity<String> loadNewYangModel(@RequestBody MultipartFile yangModel,
95                                             @RequestBody MultipartFile initialConfig, @PathVariable String moduleName)
96             throws IOException {
97         LoadModelResponse response = netconfModelLoaderService.loadYangModel(yangModel, initialConfig, moduleName);
98         return ResponseEntity
99                 .status(response.getStatusCode())
100                 .body(response.getMessage());
101     }
102
103     @DeleteMapping("model/{modelName}")
104     ResponseEntity<String> deleteYangModel(@PathVariable String modelName)
105             throws IOException {
106         LoadModelResponse response = netconfModelLoaderService.deleteYangModel(modelName);
107         return ResponseEntity
108                 .status(response.getStatusCode())
109                 .body(response.getMessage());
110     }
111 }