I am trying to access the Opendaylight API from my sample project and get javax.xml.bind.UnmarshalException when I execute the code.
I realized that this exception occurs when trying to instantiate an abstract class.
But this is sample code from an official source, and I do not control the API. I tried replacing classes from the library with classes generated using XSD, and the result will be the same. Can someone tell me what is wrong or what can be done?
Here is sample code and output. Please note that the server is configured correctly and I get a response.
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.codec.binary.Base64;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.statistics.northbound.AllFlowStatistics;
import org.opendaylight.controller.statistics.northbound.FlowStatistics;
public class JAXBStatisticsClient {
public static void main(String[] args) {
System.out.println("Starting Statistics JAXB client.");
String user = "admin";
String password = "admin";
String containerName = "default";
String baseURL = "http://172.18.2.95:8080/controller/nb/v2/statistics";
try {
URL url = new java.net.URL(baseURL + "/" + containerName + "/flow");
String authString = user + ":" + password;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URLConnection connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Accept", "application/xml");
connection.connect();
InputStream inputStream = connection.getInputStream();
JAXBContext context = JAXBContext.newInstance(AllFlowStatistics.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
AllFlowStatistics result = (AllFlowStatistics) unmarshaller.unmarshal(inputStream);
System.out.println("We have these statistics:");
for (FlowStatistics statistics : result.getFlowStatistics()) {
System.out.println("Node ID : " + statistics.getNode().getNodeIDString());
System.out.println("Node Type : " + statistics.getNode().getType());
if (null != statistics.getFlowStats()) {
for (FlowOnNode flowOnNode : statistics.getFlowStats()) {
System.out.println("\t" + flowOnNode.getByteCount());
System.out.println("\t" + flowOnNode.getDurationNanoseconds());
System.out.println("\t" + flowOnNode.getDurationSeconds());
System.out.println("\t" + flowOnNode.getPacketCount());
System.out.println("\t" + flowOnNode.getTableId());
System.out.println("\t" + flowOnNode.getFlow());
}
}
}
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
e.printStackTrace();
}
}
}
Output
Starting Statistics JAXB client.
Unable to create an instance of org.opendaylight.controller.sal.action.Action
javax.xml.bind.UnmarshalException: Unable to create an instance of org.opendaylight.controller.sal.action.Action
- with linked exception:
[java.lang.InstantiationException]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at com.odl.JAXBStatisticsClient.main(JAXBStatisticsClient.java:42)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(Unknown Source)
... 21 more
XML output stream
<?xml version="1.0" encoding="UTF-8"?>
<list>
<flowStatistics>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<flowStatistic>
<flow>
<match>
<matchField>
<type>DL_TYPE</type>
<value>2048</value>
</matchField>
<matchField>
<mask>255.255.255.255</mask>
<type>NW_DST</type>
<value>10.0.1.1</value>
</matchField>
</match>
<actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="output">
<type>OUTPUT</type>
<port>
<node>
<id>00:00:00:00:00:00:00:01</id>
<type>OF</type>
</node>
<id>3</id>
<type>OF</type>
</port>
</actions>
<actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="setDlDst">
<type>SET_DL_DST</type>
<address>000000000001</address>
</actions>
<priority>500</priority>
<idleTimeout>0</idleTimeout>
<hardTimeout>0</hardTimeout>
<id>0</id>
</flow>
<tableId>0</tableId>
<durationSeconds>8292</durationSeconds>
<durationNanoseconds>952000000</durationNanoseconds>
<packetCount>0</packetCount>
<byteCount>0</byteCount>
</flowStatistic>
<flowStatistic>
<flow>
<match>
<matchField>
<type>DL_TYPE</type>
<value>2048</value>
</matchField>
<matchField>
<type>IN_PORT</type>
<value>OF|3@OF|00:00:00:00:00:00:00:01</value>
</matchField>
</match>
<actions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="controller">
<type>CONTROLLER</type>
</actions>
<priority>500</priority>
<idleTimeout>0</idleTimeout>
<hardTimeout>0</hardTimeout>
<id>0</id>
</flow>
<tableId>0</tableId>
<durationSeconds>8274</durationSeconds>
<durationNanoseconds>701000000</durationNanoseconds>
<packetCount>0</packetCount>
<byteCount>0</byteCount>
</flowStatistic>
</flowStatistics>
</list>