Initial source code
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / SetConfigOperation.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.netconf.rpc;\r
20 \r
21 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;\r
22 import org.commscope.tr069adapter.mapper.model.NetConfResponse;\r
23 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
24 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;\r
25 import org.opendaylight.netconf.api.DocumentedException;\r
26 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;\r
27 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;\r
28 import org.opendaylight.netconf.api.DocumentedException.ErrorType;\r
29 import org.opendaylight.netconf.api.xml.XmlElement;\r
30 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;\r
31 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 import org.w3c.dom.Attr;\r
35 import org.w3c.dom.Document;\r
36 import org.w3c.dom.Element;\r
37 \r
38 public class SetConfigOperation extends AbstractLastNetconfOperation {\r
39   private static final Logger logger = LoggerFactory.getLogger(SetConfigOperation.class);\r
40 \r
41   private static final String DELETE_EDIT_CONFIG = "delete";\r
42   private static final String OPERATION = "operation";\r
43   private static final String REMOVE_EDIT_CONFIG = "remove";\r
44   private String deviceID;\r
45 \r
46   public SetConfigOperation(final String netconfSessionIdForReporting, String deviceID) {\r
47     super(netconfSessionIdForReporting);\r
48     this.deviceID = deviceID;\r
49   }\r
50 \r
51   @Override\r
52   protected Element handleWithNoSubsequentOperations(final Document document,\r
53       final XmlElement operationElement) throws DocumentedException {\r
54     final XmlElement configElementData =\r
55         operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);\r
56     String delOrEditUrl = "setConfig";\r
57     if (containsDelete(configElementData)) {\r
58       delOrEditUrl = "delConfig";\r
59     }\r
60 \r
61     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
62     logger.debug("netconf request recevied : {}", requestXml);\r
63     NetConfServerProperties config =\r
64         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
65 \r
66     final String baseUrl = config.getMapperPath() + "/" + delOrEditUrl;\r
67     NetConfResponse restResponse = XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID);\r
68 \r
69     if (restResponse != null) {\r
70       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
71       if (errorCode != null) {\r
72         if (errorCode.getFaultCode() != null && errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
73           return document.createElement(XmlNetconfConstants.OK);\r
74         } else {\r
75           logger.error("Error received : {}", errorCode);\r
76           throw new DocumentedException(errorCode.getErrorMessage(),\r
77               ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
78               ErrorSeverity.from(errorCode.getErrorSeverity()));\r
79         }\r
80       } else {\r
81         return document.createElement(XmlNetconfConstants.OK);\r
82       }\r
83     } else {\r
84       logger.error("received the null response from mapper ");\r
85       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
86           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
87     }\r
88 \r
89   }\r
90 \r
91   @Override\r
92   protected String getOperationName() {\r
93     return "edit-config";\r
94   }\r
95 \r
96   private boolean containsDelete(final XmlElement element) {\r
97     for (final Attr o : element.getAttributes().values()) {\r
98       if (o.getLocalName().equals(OPERATION)\r
99           && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {\r
100         return true;\r
101       }\r
102 \r
103     }\r
104 \r
105     for (final XmlElement xmlElement : element.getChildElements()) {\r
106       if (containsDelete(xmlElement)) {\r
107         return true;\r
108       }\r
109 \r
110     }\r
111 \r
112     return false;\r
113   }\r
114 }\r