Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / main / java / org / commscope / tr069adapter / netconf / rpc / AddObjectOperation.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 java.io.StringReader;
22
23 import javax.xml.XMLConstants;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.commscope.tr069adapter.mapper.model.ErrorCodeDetails;
28 import org.commscope.tr069adapter.mapper.model.NetConfResponse;
29 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
30 import org.commscope.tr069adapter.netconf.config.NetConfServerProperties;
31 import org.opendaylight.netconf.api.DocumentedException;
32 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
33 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
34 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
35 import org.opendaylight.netconf.api.xml.XmlElement;
36 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
37 import org.opendaylight.netconf.mapping.api.HandlingPriority;
38 import org.opendaylight.netconf.mapping.api.NetconfOperation;
39 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
43 import org.xml.sax.InputSource;
44
45 public class AddObjectOperation implements NetconfOperation {
46   private static final Logger logger = LoggerFactory.getLogger(AddObjectOperation.class);
47   public static final String OP_NAMESPACE = "urn:tr069rpc:1.0";
48   public static final String OP_NAME = "add-object";
49   private String deviceID;
50   private String swVersion;
51   private String hwVersion;
52
53   public AddObjectOperation(String deviceID, String swVersion, String hwVersion) {
54     this.deviceID = deviceID;
55     this.swVersion = swVersion;
56     this.hwVersion = hwVersion;
57   }
58
59   @Override
60   public HandlingPriority canHandle(final Document message) throws DocumentedException {
61     OperationNameAndNamespace operationNameAndNamespace = null;
62     operationNameAndNamespace = new OperationNameAndNamespace(message);
63     return canHandle(operationNameAndNamespace.getOperationName(),
64         operationNameAndNamespace.getNamespace());
65   }
66
67   @Override
68   public Document handle(Document requestMessage,
69       NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
70     logger.debug("AddObject rpc is received in netconfserver");
71
72     final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
73
74     final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
75
76
77     String requestXml = XmlUtility.convertDocumentToString(requestElement);
78     logger.debug("AddObject rpc requestXml={}", requestXml);
79
80
81     NetConfServerProperties config =
82         NetConfServiceBooter.getApplicationContext().getBean(NetConfServerProperties.class);
83
84     final String baseUrl = config.getMapperPath() + "/addobject";
85     NetConfResponse restResponse =
86         XmlUtility.invokeMapperCall(baseUrl, requestXml, deviceID, swVersion, hwVersion);
87     Document document = null;
88
89     ErrorCodeDetails errorCode = restResponse.getErrorCode();
90     if (errorCode != null && errorCode.getFaultCode() != null
91         && !errorCode.getFaultCode().equalsIgnoreCase("0")) {
92       logger.error("Error recevied : {}", errorCode);
93       throw new DocumentedException(errorCode.getErrorMessage(),
94           ErrorType.from(errorCode.getErrorType()), ErrorTag.from(errorCode.getErrorTag()),
95           ErrorSeverity.from(errorCode.getErrorSeverity()));
96     } else if (restResponse.getNetconfResponseXml() != null) {
97       logger.debug("addobject rpc response received from mapper {}",
98           restResponse.getNetconfResponseXml());
99       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
100       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
101       factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
102       DocumentBuilder builder;
103       try {
104         builder = factory.newDocumentBuilder();
105         document =
106             builder.parse(new InputSource(new StringReader(restResponse.getNetconfResponseXml())));
107         document.getDocumentElement().setAttribute("xmlns:ns1", getOperationNamespace());
108         document.getDocumentElement().setAttribute("xmlns",
109             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
110         document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
111       } catch (Exception e) {
112         logger.error("while contruscting the response {}", e.getMessage());
113         throw new DocumentedException("Operation Aborted", ErrorType.from("application"),
114             ErrorTag.from("operation-failed"), ErrorSeverity.from("ERROR"));
115       }
116     }
117
118     return document;
119   }
120
121   protected HandlingPriority canHandle(final String operationName,
122       final String operationNamespace) {
123     return operationName.equals(getOperationName())
124         && operationNamespace.equals(getOperationNamespace())
125             ? HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1100)
126             : HandlingPriority.CANNOT_HANDLE;
127   }
128
129   public static final class OperationNameAndNamespace {
130     private final String operationName;
131     private final String namespace;
132
133     private final XmlElement operationElement;
134
135     public OperationNameAndNamespace(final Document message) throws DocumentedException {
136       XmlElement requestElement = null;
137       requestElement = getRequestElementWithCheck(message);
138       operationElement = requestElement.getOnlyChildElement();
139       operationName = operationElement.getName();
140       namespace = operationElement.getNamespace();
141     }
142
143     public String getOperationName() {
144       return operationName;
145     }
146
147     public String getNamespace() {
148       return namespace;
149     }
150
151     public XmlElement getOperationElement() {
152       return operationElement;
153     }
154
155   }
156
157   protected static XmlElement getRequestElementWithCheck(final Document message)
158       throws DocumentedException {
159     return XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
160         XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
161   }
162
163   protected String getOperationNamespace() {
164     return OP_NAMESPACE;
165   }
166
167   protected String getOperationName() {
168     return OP_NAME;
169   }
170
171 }