248aec46acb85f28d1b10d6cb72e59ce026e3c08
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
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.configuration;
22
23 import com.tailf.jnc.Element;
24 import com.tailf.jnc.JNCException;
25 import com.tailf.jnc.XMLParser;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.multipart.MultipartFile;
35 import org.xml.sax.InputSource;
36
37 @Service
38 public class NetconfConfigurationService {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(NetconfConfigurationService.class);
41     private static final String CONFIGURATION_HAS_BEEN_ACTIVATED = "New configuration has been activated";
42
43     private final NetconfConfigurationReader netconfConfigurationReader;
44     private NetconfConfigurationEditor configurationEditor;
45     private XMLParser parser;
46
47     @Autowired
48     public NetconfConfigurationService(NetconfConfigurationReader netconfConfigurationReader,
49                                        NetconfConfigurationEditor netconfConfigurationEditor) throws JNCException {
50         this.netconfConfigurationReader = netconfConfigurationReader;
51         this.configurationEditor = netconfConfigurationEditor;
52         this.parser = new XMLParser();
53     }
54
55     public String getCurrentConfiguration() throws IOException, JNCException {
56         return netconfConfigurationReader.getRunningConfig();
57     }
58
59     public String getCurrentConfiguration(String model, String container) throws IOException, JNCException {
60         String path = String.format("/%s:%s", model, container);
61         return netconfConfigurationReader.getRunningConfig(path);
62     }
63
64     public String editCurrentConfiguration(MultipartFile newConfiguration) throws IOException, JNCException {
65         Element configurationElement = convertMultipartToXmlElement(newConfiguration);
66         configurationEditor.editConfig(configurationElement);
67
68         LOGGER.debug("Loading new configuration: \n{}", configurationElement.toXMLString());
69         return CONFIGURATION_HAS_BEEN_ACTIVATED;
70     }
71
72     private Element convertMultipartToXmlElement(MultipartFile editConfig) throws IOException, JNCException {
73         InputSource inputSourceUpdateConfig = new InputSource(new ByteArrayInputStream(editConfig.getBytes()));
74         return parser.parse(inputSourceUpdateConfig);
75     }
76 }