Merge "Bug 8873 - Bundle based reconciliation to enable bundling of messages"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / AbstractErrorTranslator.java
index b71da457032f6e3f4a13bbc79ac066e0bb63e0d5..a03c093b7c419d2d7172bdc536d3acc3cc95b6b7 100644 (file)
@@ -1,45 +1,35 @@
 /**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+
 package org.opendaylight.openflowplugin.openflow.md.core.translator;
 
-import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
-import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
-import org.opendaylight.openflowplugin.openflow.md.core.session.TransactionKey;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
+import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotificationBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.node.error.reference.ObjectReference;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.node.error.reference.object.reference.FlowRefBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.node.error.reference.object.reference.GroupRefBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.node.error.reference.object.reference.MeterRefBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionMetadata;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.NodeGroup;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.NodeMeter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.concurrent.CopyOnWriteArrayList;
-
+/**
+ * general support for errorMessage OF-API to MD-SAL translation
+ */
 public abstract class AbstractErrorTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
 
-    protected static final Logger LOG = LoggerFactory.getLogger(AbstractErrorTranslator.class);
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractErrorTranslator.class);
 
     @Override
     public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
@@ -57,41 +47,12 @@ public abstract class AbstractErrorTranslator implements IMDMessageTranslator<Of
 
             }
 
-            // create a Node Error Notification event builder
-            NodeErrorNotificationBuilder nodeErrBuilder = new NodeErrorNotificationBuilder();
-
-            // Fill in the Node Error Notification Builder object from the Error
-            // Message
-
-            nodeErrBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(message.getXid())));
-
-            Object object = sc.getbulkTransactionCache().getIfPresent(new TransactionKey(message.getXid()));
-
-            Uri uri = null;
-            ObjectReference objRef = null;
-            if(object != null) {
-                uri = ((TransactionMetadata) object).getTransactionUri();
-                objRef = getReference(object);
-            }
-
-            nodeErrBuilder.setTransactionUri(uri);
-            nodeErrBuilder.setObjectReference(objRef);
-
-            ErrorType type = decodeErrorType(message.getType());
-            nodeErrBuilder.setType(type);
-            nodeErrBuilder.setCode(message.getCode());
-
-            if (message.getData() != null) {
-                nodeErrBuilder.setData(new String(message.getData()));
-            }
-
             // TODO -- Augmentation is not handled
-
-            NodeErrorNotification nodeErrorEvent = nodeErrBuilder.build();
-            list.add(nodeErrorEvent);
-
-            list.addAll(getGranularNodeErrors(message, type, uri, objRef));
-
+            ErrorType type = decodeErrorType(message.getType());
+            NodeRef node = new NodeRef(
+                InventoryDataServiceUtil.identifierFromDatapathId(
+                    sc.getFeatures().getDatapathId()));
+            list.add(getGranularNodeErrors(message, type, node));
             return list;
         } else {
             LOG.error("Message is not of Error Message ");
@@ -99,33 +60,17 @@ public abstract class AbstractErrorTranslator implements IMDMessageTranslator<Of
         }
     }
 
-    private ObjectReference getReference(Object object) {
-        if (object instanceof NodeFlow) {
-            FlowEntityData flowEntry = new FlowEntityData();
-            FlowRefBuilder flowRef = flowEntry.getBuilder(object);
-            return flowRef.build();
-
-        } else if (object instanceof NodeGroup) {
-            GroupEntityData groupEntry = new GroupEntityData();
-            GroupRefBuilder groupRef = groupEntry.getBuilder(object);
-            return groupRef.build();
-
-        } else if (object instanceof NodeMeter) {
-            MeterEntityData meterEntry = new MeterEntityData();
-            MeterRefBuilder meterRef = meterEntry.getBuilder(object);
-            return meterRef.build();
-
-        }
-        return null;
-    }
-
-    protected List<DataObject> getGranularNodeErrors(ErrorMessage message, ErrorType errorType, Uri uri, ObjectReference objRef){
-        // this is the impl for V1.0
-        return new ArrayList<>();
-    }
+    /**
+     * @param message error message
+     * @param errorType error type
+     * @param node node ref
+     * @return error message
+     */
+    protected abstract org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage getGranularNodeErrors(ErrorMessage message, ErrorType errorType, NodeRef node);
 
     /**
      * @param type error type in source message
+     * @return enum for errorType
      */
     public abstract ErrorType decodeErrorType(int type);