copyright header added
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / ErrorTranslator.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.translator;
9
10 import java.math.BigInteger;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.concurrent.CopyOnWriteArrayList;
14
15 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
16 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
18 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotificationBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class ErrorTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
30
31     protected static final Logger LOG = LoggerFactory.getLogger(ErrorTranslator.class);
32
33     @Override
34     public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
35         if (msg instanceof ErrorMessage) {
36             ErrorMessage message = (ErrorMessage) msg;
37             List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
38             LOG.error(" Error Message received: type={}[{}], code={}[{}], data={}[{}] ", message.getType(),
39                     message.getTypeString(), message.getCode(), message.getCodeString(), new String(message.getData()),
40                     ByteUtil.bytesToHexstring(message.getData(), " "));
41
42             // create a Node Error Notification event builder
43             NodeErrorNotificationBuilder nodeErrBuilder = new NodeErrorNotificationBuilder();
44
45             // Fill in the Node Error Notification Builder object from the Error
46             // Message
47
48             nodeErrBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(message.getXid())));
49
50             nodeErrBuilder.setType(ErrorType.forValue(message.getType()));
51
52             nodeErrBuilder.setCode(message.getCode());
53
54             nodeErrBuilder.setData(new String(message.getData()));
55
56             // TODO -- Augmentation is not handled
57
58             // Note Error_TypeV10 is not handled.
59
60             NodeErrorNotification nodeErrorEvent = nodeErrBuilder.build();
61             list.add(nodeErrorEvent);
62             return list;
63         } else {
64             LOG.error("Message is not of Error Message ");
65             return Collections.emptyList();
66         }
67     }
68
69 }