b011420271dd4d85383ada4ad0aea8eb9d0ac560
[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   private String swVersion;\r
46   private String hwVersion;\r
47 \r
48   public SetConfigOperation(final String netconfSessionIdForReporting, String deviceID,\r
49       String swVersion, String hwVersion) {\r
50     super(netconfSessionIdForReporting);\r
51     this.deviceID = deviceID;\r
52     this.swVersion = swVersion;\r
53     this.hwVersion = hwVersion;\r
54   }\r
55 \r
56   @Override\r
57   protected Element handleWithNoSubsequentOperations(final Document document,\r
58       final XmlElement operationElement) throws DocumentedException {\r
59     final XmlElement configElementData =\r
60         operationElement.getOnlyChildElement(XmlNetconfConstants.CONFIG_KEY);\r
61     String delOrEditUrl = "setConfig";\r
62     if (containsDelete(configElementData)) {\r
63       delOrEditUrl = "delConfig";\r
64     }\r
65 \r
66     String requestXml = XmlUtility.convertDocumentToString(operationElement);\r
67     logger.debug("netconf request recevied : {}", requestXml);\r
68     NetConfServerProperties config =\r
69         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);\r
70 \r
71     final String baseUrl = config.getMapperPath() + "/" + delOrEditUrl;\r
72     NetConfResponse restResponse =\r
73         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);\r
74 \r
75     if (restResponse != null) {\r
76       ErrorCodeDetails errorCode = restResponse.getErrorCode();\r
77       if (errorCode != null) {\r
78         if (errorCode.getFaultCode() != null && errorCode.getFaultCode().equalsIgnoreCase("0")) {\r
79           return document.createElement(XmlNetconfConstants.OK);\r
80         } else {\r
81           logger.error("Error received : {}", errorCode);\r
82           throw new DocumentedException(errorCode.getErrorMessage(),\r
83               ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),\r
84               ErrorSeverity.from(errorCode.getErrorSeverity()));\r
85         }\r
86       } else {\r
87         return document.createElement(XmlNetconfConstants.OK);\r
88       }\r
89     } else {\r
90       logger.error("received the null response from mapper ");\r
91       throw new DocumentedException("Unable to perform Operation", ErrorType.from("application"),\r
92           ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));\r
93     }\r
94 \r
95   }\r
96 \r
97   @Override\r
98   protected String getOperationName() {\r
99     return "edit-config";\r
100   }\r
101 \r
102   private boolean containsDelete(final XmlElement element) {\r
103     for (final Attr o : element.getAttributes().values()) {\r
104       if (o.getLocalName().equals(OPERATION)\r
105           && (o.getValue().equals(DELETE_EDIT_CONFIG) || o.getValue().equals(REMOVE_EDIT_CONFIG))) {\r
106         return true;\r
107       }\r
108 \r
109     }\r
110 \r
111     for (final XmlElement xmlElement : element.getChildElements()) {\r
112       if (containsDelete(xmlElement)) {\r
113         return true;\r
114       }\r
115 \r
116     }\r
117 \r
118     return false;\r
119   }\r
120 }\r