DeviceContext proposal 61/17161/1
authorMartin Bobak <mbobak@cisco.com>
Tue, 24 Mar 2015 13:26:21 +0000 (14:26 +0100)
committerMartin Bobak <mbobak@cisco.com>
Thu, 26 Mar 2015 14:17:20 +0000 (15:17 +0100)
 - initial test

Change-Id: I21c7d49bc2ac6a41fbdef2c7f994195a7141cee0
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java [new file with mode: 0644]
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java [new file with mode: 0644]
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImplTest.java [new file with mode: 0644]

diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
new file mode 100644 (file)
index 0000000..a421938
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 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.impl.device;
+
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableFeatures;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+
+/**
+ * 
+ */
+public class DeviceContextImpl implements DeviceContext {
+
+    @Override
+    public <M extends ChildOf<DataObject>> void onMessage(M message, RequestContext requestContext) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public void addAuxiliaryConenctionContext(ConnectionContext connectionContext) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public void removeAuxiliaryConenctionContext(ConnectionContext connectionContext) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public DeviceState getDeviceState() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void setTransactionChain(TransactionChain transactionChain) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public TransactionChain getTransactionChain() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public TableFeatures getCapabilities() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java
new file mode 100644 (file)
index 0000000..0a5dc67
--- /dev/null
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 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.impl.device;
+
+import java.util.concurrent.ExecutionException;
+
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextReadyHandler;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+
+/**
+ * 
+ */
+public class DeviceManagerImpl implements DeviceManager {
+    
+    private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
+    
+    private Xid xid;
+
+    @Override
+    public void deviceConnected(ConnectionContext connectionContext) {
+        xid = new Xid(0L);
+        DeviceContextImpl deviceContextImpl = new DeviceContextImpl();
+
+        try {
+            FlowCapableNode description = queryDescription(connectionContext, xid.getNextValue()).get();
+            
+        } catch (InterruptedException | ExecutionException e) {
+            // TODO Auto-generated catch block
+            LOG.info("Failed to retrieve node static info: {}", e.getMessage());
+        }
+    }
+
+    /**
+     * @param connectionContext 
+     * @param nextXid 
+     */
+    private static ListenableFuture<FlowCapableNode> queryDescription(ConnectionContext connectionContext, long nextXid) {
+        MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
+        builder.setType(MultipartType.OFPMPDESC);
+        builder.setVersion(connectionContext.getFeatures().getVersion());
+        builder.setFlags(new MultipartRequestFlags(false));
+        builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder()
+                .build());
+        builder.setXid(nextXid);
+        connectionContext.getConnectionAdapter().multipartRequest(builder.build());
+        
+        //TODO: involve general wait-for-answer mechanism and return future with complete value
+        //TODO: translate message
+        return Futures.immediateFuture(null); 
+    }
+
+    @Override
+    public void sendMessage(DataObject dataObject, RequestContext requestContext) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public Xid sendRequest(DataObject dataObject, RequestContext requestContext) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    public void addRequestContextReadyHandler(DeviceContextReadyHandler deviceContextReadyHandler) {
+        // TODO Auto-generated method stub
+
+    }
+
+}
diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImplTest.java
new file mode 100644 (file)
index 0000000..2bd09ec
--- /dev/null
@@ -0,0 +1,95 @@
+/**
+ * Copyright (c) 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.impl.device;
+
+import static org.junit.Assert.fail;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCase;
+//import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupDescCase;
+
+/**
+ * test of {@link DeviceManagerImpl} - lightweight version, using basic ways (TDD)
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceManagerImplTest {
+
+    private DeviceManagerImpl deviceManager;
+    @Mock
+    private ConnectionContext connectionContext;
+    @Mock
+    private ConnectionAdapter connectionAdapter;
+    @Mock
+    private FeaturesReply features;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
+        Mockito.when(connectionContext.getFeatures()).thenReturn(features);
+        Mockito.when(features.getVersion()).thenReturn((short) 42);
+        deviceManager = new DeviceManagerImpl();
+    }
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl#deviceConnected(org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext)}.
+     */
+    @Test
+    public void testDeviceConnected() {
+        deviceManager.deviceConnected(connectionContext);
+
+        ArgumentCaptor<MultipartRequestInput> mpInputCaptor = ArgumentCaptor.forClass(MultipartRequestInput.class);
+        Mockito.verify(connectionAdapter).multipartRequest(mpInputCaptor.capture());
+
+        Assert.assertTrue(mpInputCaptor.getAllValues().get(0).getMultipartRequestBody() instanceof MultipartRequestDescCase);
+        //Assert.assertTrue(mpInputCaptor.getAllValues().get(1).getMultipartRequestBody() instanceof MultipartRequestGroupDescCase);
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl#sendMessage(org.opendaylight.yangtools.yang.binding.DataObject, org.opendaylight.openflowplugin.api.openflow.device.RequestContext)}.
+     */
+    @Test
+    public void testSendMessage() {
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl#sendRequest(org.opendaylight.yangtools.yang.binding.DataObject, org.opendaylight.openflowplugin.api.openflow.device.RequestContext)}.
+     */
+    @Test
+    public void testSendRequest() {
+    }
+
+    /**
+     * Test method for {@link org.opendaylight.openflowplugin.impl.device.DeviceManagerImpl#addRequestContextReadyHandler(org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextReadyHandler)}.
+     */
+    @Test
+    public void testAddRequestContextReadyHandler() {
+    }
+
+}