2 * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.openflow.md.core.translator;
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.when;
13 import java.lang.reflect.Method;
14 import java.math.BigInteger;
15 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
24 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.BaseNodeErrorNotification;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
39 public class ErrorTranslatorTest {
41 private final ErrorTranslator errorTranslator = new ErrorTranslator();
42 private final ErrorMessageBuilder builder = new ErrorMessageBuilder();;
43 private static final BigInteger DATAPATH_ID = BigInteger.valueOf(0x7777L);
44 private static Logger LOG = LoggerFactory
45 .getLogger(ErrorTranslatorTest.class);
48 SwitchConnectionDistinguisher cookie;
52 GetFeaturesOutput features;
61 builder.setData(new byte[]{42});
63 MockitoAnnotations.initMocks(this);
64 when(sc.getFeatures()).thenReturn(features);
65 when(features.getDatapathId()).thenReturn(DATAPATH_ID);
70 public void testTranslate() {
72 List<DataObject> data = errorTranslator.translate(cookie, sc, builder.build());
74 Assert.assertEquals(1, data.size());
75 DataObject obj = data.get(0);
76 Assert.assertTrue(obj instanceof BaseNodeErrorNotification);
77 BaseNodeErrorNotification nodeError = (BaseNodeErrorNotification)obj;
78 NodeRef expectedNode = new NodeRef(
79 InventoryDataServiceUtil.identifierFromDatapathId(DATAPATH_ID));
80 Assert.assertEquals(expectedNode, nodeError.getNode());
84 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator#getGranularNodeErrors(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType, NodeRef)}.
89 public void testGetGranularNodeErrors() throws Exception {
90 BigInteger dpid = BigInteger.valueOf(0x1122334455667788L);
91 NodeRef node = new NodeRef(
92 InventoryDataServiceUtil.identifierFromDatapathId(dpid));
93 for (ErrorType eType : ErrorType.values()) {
94 builder.setType(eType.getIntValue());
95 ErrorMessage errorMessage = errorTranslator.getGranularNodeErrors(builder.build(), eType, node);
96 LOG.debug("translating errorMessage of type {}", eType);
97 assertNotNull("translated error is null", errorMessage);
98 Assert.assertEquals(21, errorMessage.getCode().intValue());
99 Assert.assertEquals(eType, errorMessage.getType());
100 Method getNode = errorMessage.getClass().getMethod("getNode");
101 getNode.setAccessible(true);
102 Assert.assertEquals(node, getNode.invoke(errorMessage));
103 Method getXid = errorMessage.getClass().getMethod("getTransactionId", new Class[0]);
104 getXid.setAccessible(true);
105 TransactionId xid = (TransactionId) getXid.invoke(errorMessage, new Object[0]);
106 Assert.assertEquals(42L, xid.getValue().longValue());
107 assertNotNull("data is null", errorMessage.getData());
112 * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator#decodeErrorType(int)}.
115 public void testDecodeErrorType() {
116 for (ErrorType eType : ErrorType.values()) {
117 ErrorType result = errorTranslator.decodeErrorType(eType.getIntValue());
118 Assert.assertEquals(eType, result);