Bug-835 - Reserve Ports should be logical ports
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / AbstractErrorTranslator.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.util.Collections;
11 import java.util.List;
12 import java.util.concurrent.CopyOnWriteArrayList;
13
14 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
15 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
16 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
17 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * general support for errorMessage OF-API to MD-SAL translation 
27  */
28 public abstract class AbstractErrorTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
29
30     protected static final Logger LOG = LoggerFactory.getLogger(AbstractErrorTranslator.class);
31
32     @Override
33     public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
34         if (msg instanceof ErrorMessage) {
35             ErrorMessage message = (ErrorMessage) msg;
36             List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
37             if (LOG.isDebugEnabled()) {
38                 String hexData = "n/a";
39                 if (message.getData() != null) {
40                     hexData = ByteUtil.bytesToHexstring(message.getData(), " ");
41                 }
42                 LOG.debug(" Error Message received: type={}[{}], code={}[{}], data=[{}] ", message.getType(),
43                         message.getTypeString(), message.getCode(), message.getCodeString(),
44                         hexData);
45
46             }
47
48             // TODO -- Augmentation is not handled
49             ErrorType type = decodeErrorType(message.getType());
50             
51             list.add(getGranularNodeErrors(message, type));
52             return list;
53         } else {
54             LOG.error("Message is not of Error Message ");
55             return Collections.emptyList();
56         }
57     }
58     
59     /**
60      * @param message
61      * @param errorType
62      * @return 
63      */
64     protected abstract org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage getGranularNodeErrors(ErrorMessage message, ErrorType errorType);
65
66     /**
67      * @param type error type in source message
68      * @return enum for errorType
69      */
70     public abstract ErrorType decodeErrorType(int type);
71
72 }