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