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