CommonService publishes its methods and properties
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / CommonService.java
index 441889699498ca6282e90451f0c703053f049741..c72a41742e7c886dd0fcd8ab2d5de11f3505dcf6 100644 (file)
@@ -7,25 +7,91 @@
  */
 package org.opendaylight.openflowplugin.impl.services;
 
+import com.google.common.base.Function;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import com.google.common.util.concurrent.Futures;
+import java.math.BigInteger;
 import java.util.concurrent.Future;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.slf4j.Logger;
 
-public class CommonService {
-    protected OFRpcTaskContext rpcTaskContext;
+public abstract class CommonService {
+    private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
+    private static final long WAIT_TIME = 2000;
+    protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
+            .<Void> failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
+    protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
+
+    // protected OFRpcTaskContext rpcTaskContext;
+    protected short version;
+    protected BigInteger datapathId;
     protected RpcContext rpcContext;
-    protected final static Future<RpcResult<Void>> errorRpcResult = Futures.immediateFuture(RpcResultBuilder
-            .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
+    protected DeviceContext deviceContext;
+    private ConnectionAdapter primaryConnectionAdapter;
+
+    public CommonService() {
+    }
 
-    /**
-     * 
-     */
     public CommonService(final RpcContext rpcContext) {
         this.rpcContext = rpcContext;
+
+        this.deviceContext = rpcContext.getDeviceContext();
+        final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
+        this.datapathId = features.getDatapathId();
+        this.version = features.getVersion();
+        this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
+    }
+
+    protected long provideWaitTime() {
+        return WAIT_TIME;
+    }
+
+    protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
+        if (connectionID == null) {
+            return primaryConnectionAdapter;
+        }
+        if (connectionID.equals(PRIMARY_CONNECTION)) {
+            return primaryConnectionAdapter;
+        }
+
+        // TODO uncomment when getAuxiali.... will be merged to APIs
+        // final ConnectionContext auxiliaryConnectionContext =
+        // deviceContext.getAuxiliaryConnectionContext(connectionID);
+        final ConnectionContext auxiliaryConnectionContext = null;
+        if (auxiliaryConnectionContext != null) {
+            return auxiliaryConnectionContext.getConnectionAdapter();
+        }
+
+        return primaryConnectionAdapter;
+    }
+
+    <T extends DataObject, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
+            final Function<DataCrate<T>, Future<RpcResult<F>>> function) {
+        LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
+
+        final RequestContext<T> requestContext = rpcContext.createRequestContext();
+        final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
+        final DataCrate<T> dataCrate = DataCrateBuilder.<T> builder().setiDConnection(connectionID)
+                .setRequestContext(requestContext).build();
+        if (!result.isDone()) {
+            final Future<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
+
+            final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
+            rpcResultConvertor.processResultFromOfJava(resultFromOFLib);
+
+        } else {
+            RequestContextUtil.closeRequstContext(requestContext);
+        }
+        return result;
     }
 
 }