implementation of NodeConfigService 50/17150/4
authorMartin Bobak <mbobak@cisco.com>
Thu, 26 Mar 2015 09:03:21 +0000 (10:03 +0100)
committerMartin Bobak <mbobak@cisco.com>
Thu, 26 Mar 2015 17:22:49 +0000 (18:22 +0100)
- introduced RpcResultConvertor for converting OFJava future to  requestContext's future
- RpcResultConvertor used in SalFlowServiceImpl

Change-Id: I11dc0e6f15920dab3323128749a877532a0486f8
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/NodeConfigServiceImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/RpcResultConvertor.java [new file with mode: 0644]
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java

index d9f29861c2194dc336b4f46b5d411043d0291fe8..e028febe16ff49916da9c173744634734270897d 100644 (file)
@@ -1,38 +1,50 @@
 /**
  * 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.services;
 
-import java.util.concurrent.Future;
-import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import com.google.common.util.concurrent.JdkFutureAdapters;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import java.util.concurrent.Future;
 
 /**
  * @author joe
- * 
  */
 // TODO: implement this
 public class NodeConfigServiceImpl extends CommonService implements NodeConfigService {
 
 
-    /*
-         * (non-Javadoc)
-         *
-         * @see
-         * org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService#setConfig(org.opendaylight
-         * .yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput)
-         */
     @Override
     public Future<RpcResult<SetConfigOutput>> setConfig(final SetConfigInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        final RequestContext requestContext = rpcContext.createRequestContext();
+        final SettableFuture<RpcResult<SetConfigOutput>> result = rpcContext.storeOrFail(requestContext);
+        if (!result.isDone()) {
+            SetConfigInputBuilder builder = new SetConfigInputBuilder();
+            SwitchConfigFlag flag = SwitchConfigFlag.valueOf(input.getFlag());
+            final Xid xid = deviceContext.getNextXid();
+            builder.setXid(xid.getValue());
+            builder.setFlags(flag);
+            builder.setMissSendLen(input.getMissSearchLength());
+            builder.setVersion(version);
+            ListenableFuture<RpcResult<Void>> futureResultFromOfLib = JdkFutureAdapters.listenInPoolThread(deviceContext.getPrimaryConnectionContext().getConnectionAdapter().setConfig(builder.build()));
+            RpcResultConvertor<SetConfigOutput> rpcResultConvertor = new RpcResultConvertor<>(requestContext);
+            rpcResultConvertor.processResultFromOfJava(futureResultFromOfLib, getWaitTime());
+        } else {
+            requestContext.close();
+        }
+        return result;
     }
-
 }
diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/RpcResultConvertor.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/RpcResultConvertor.java
new file mode 100644 (file)
index 0000000..0247efc
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.services;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Created by Martin Bobak <mbobak@cisco.com> on 26.3.2015.
+ */
+public class RpcResultConvertor<T extends DataObject> {
+
+    private final RequestContext requestContext;
+
+    public RpcResultConvertor(RequestContext requestContext) {
+        this.requestContext = requestContext;
+    }
+
+    public void processResultFromOfJava(final Future<RpcResult<Void>> futureResultFromOfLib,
+                                        final long waitTime) {
+        try {
+            final RpcResult<Void> rpcResult = futureResultFromOfLib.get(waitTime, TimeUnit.MILLISECONDS);
+            if (!rpcResult.isSuccessful()) {
+                requestContext.getFuture().set(RpcResultBuilder.<T>failed().withRpcErrors(rpcResult.getErrors()).build());
+                requestContext.close();
+            }
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            requestContext.getFuture().set(RpcResultBuilder
+                    .<T>failed()
+                    .withError(RpcError.ErrorType.APPLICATION, "",
+                            "Flow modification on device wasn't successfull.").build());
+            requestContext.close();
+        } catch (final Exception e) {
+            requestContext.getFuture().set(RpcResultBuilder.<T>failed()
+                    .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.").build());
+            requestContext.close();
+        }
+    }
+
+}
index e8dd4d1543bacdd18f8f932777b4f59c6e8ebe38..dbaf5e9786fe2915a19929324285f08cafda08c2 100644 (file)
@@ -36,6 +36,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.Upda
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.common.RpcError;
@@ -125,24 +126,10 @@ public class SalFlowServiceImpl extends CommonService implements SalFlowService
         final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
 
         if (!result.isDone()) {
-            try {
                 final Future<RpcResult<Void>> resultFromOFLib = function.apply(connectionID);
-                final RpcResult<Void> rpcResult = resultFromOFLib.get(getWaitTime(), TimeUnit.MILLISECONDS);
-                if (!rpcResult.isSuccessful()) {
-                    result.set(RpcResultBuilder.<T> failed().withRpcErrors(rpcResult.getErrors()).build());
-                    requestContext.close();
-                }
-            } catch (InterruptedException | ExecutionException | TimeoutException e) {
-                result.set(RpcResultBuilder
-                        .<T> failed()
-                        .withError(RpcError.ErrorType.APPLICATION, "",
-                                "Flow modification on device wasn't successfull.").build());
-                requestContext.close();
-            } catch (final Exception e) {
-                result.set(RpcResultBuilder.<T> failed()
-                        .withError(RpcError.ErrorType.APPLICATION, "", "Flow translation to OF JAVA failed.").build());
-                requestContext.close();
-            }
+
+                RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext);
+                rpcResultConvertor.processResultFromOfJava(resultFromOFLib, getWaitTime());
 
         } else {
             requestContext.close();