Rpc Registration for path.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.java
index f6b85ae7f69fcc0c50e15655262a1e6875976e92..eedcf3ab57e20662b51d7308b6884689ad8138d0 100644 (file)
@@ -1,75 +1,81 @@
 /**
  * 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.rpc;
 
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.Future;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.RpcService;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 public class RpcContextImpl implements RpcContext {
 
     final ProviderContext providerContext;
 
     // TODO: add private Sal salBroker
-    private final List<RequestContext> requestContexts = new ArrayList<>();
-    private DeviceContext deviceContext;
+    private final List<RequestContext<? extends DataObject>> requestContexts = new ArrayList<>();
+    private final DeviceContext deviceContext;
     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
+    private final List<RequestContext<?>> synchronizedRequestsList = Collections
+            .<RequestContext<?>>synchronizedList(new ArrayList<RequestContext<?>>());
 
     private int maxRequestsPerDevice;
 
-    public RpcContextImpl(final ProviderContext providerContext) {
+    public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
         this.providerContext = providerContext;
+        this.deviceContext = deviceContext;
     }
 
     /**
-     * 
      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
      *      org.opendaylight.yangtools.yang.binding.RpcService)
      */
     @Override
     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
             final S serviceInstance) {
-        rpcRegistrations.add(providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance));
+        final RoutedRpcRegistration<S> routedRpcReg = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance);
+        routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
+        rpcRegistrations.add(routedRpcReg);
     }
 
-    /**
-     * 
-     * 
-     * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setDeviceContext(DeviceContext)
-     *      api.openflow.device.DeviceContext)
-     */
     @Override
-    public void setDeviceContext(final DeviceContext deviceContext) {
-        this.deviceContext = deviceContext;
+    public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
+        final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
 
-    }
-
-    @Override
-    public Future<RpcResult<? extends DataObject>> addNewRequest(final DataObject data) {
-        return null;
+        if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
+            synchronizedRequestsList.add(requestContext);
+        } else {
+            final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
+                    .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
+            rpcResultFuture.set(rpcResult);
+        }
+        return rpcResultFuture;
     }
 
     /**
      * Unregisters all services.
-     * 
+     *
      * @see java.lang.AutoCloseable#close()
      */
     @Override
     public void close() throws Exception {
-        for (final RoutedRpcRegistration rpcRegistration : rpcRegistrations) {
+        for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
+            rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
             rpcRegistration.close();
         }
     }
@@ -82,9 +88,23 @@ public class RpcContextImpl implements RpcContext {
         this.maxRequestsPerDevice = maxRequestsPerDevice;
     }
 
+    @Override
+    public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
+        requestContexts.remove(requestContext);
+    }
+
+    @Override
+    public DeviceContext getDeviceContext() {
+        return deviceContext;
+    }
+
+    @Override
+    public <T> RequestContext<T> createRequestContext() {
+        return new RequestContextImpl<T>(this);
+    }
+
     public boolean isRequestContextCapacityEmpty() {
         return requestContexts.size() <= maxRequestsPerDevice;
     }
 
-
 }