Migrated dom.BrokerImpl to xtend code. 27/1427/6
authorTony Tkacik <ttkacik@cisco.com>
Wed, 25 Sep 2013 17:02:02 +0000 (19:02 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 1 Oct 2013 13:25:28 +0000 (13:25 +0000)
Change-Id: Ibf2a63c2d26be8dfa2f10f5467040dc753418362
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
opendaylight/md-sal/sal-dom-broker/pom.xml
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.java [deleted file]
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.xtend [new file with mode: 0644]
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ConsumerContextImpl.xtend [new file with mode: 0644]
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ProviderContextImpl.xtend [new file with mode: 0644]

index b3f3f3e8d5b69ebe1ed0bc0d9b0b29b7fda2e3c0..21a145170cf3e0d6cb5f7659b3ee7f8f2b9b2806 100644 (file)
             <groupId>com.google.guava</groupId>\r
             <artifactId>guava</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>org.eclipse.xtend</groupId>\r
+            <artifactId>org.eclipse.xtend.lib</artifactId>\r
+        </dependency>\r
     </dependencies>\r
 \r
     <build>\r
                     </instructions>\r
                 </configuration>\r
             </plugin>\r
+            <plugin>\r
+                <groupId>org.eclipse.xtend</groupId>\r
+                <artifactId>xtend-maven-plugin</artifactId>\r
+            </plugin>\r
         </plugins>\r
     </build>\r
 </project>\r
diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.java
deleted file mode 100644 (file)
index 24b2b4b..0000000
+++ /dev/null
@@ -1,290 +0,0 @@
-/*
- * Copyright (c) 2013 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.controller.sal.dom.broker;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-import org.opendaylight.controller.sal.core.api.Broker;
-import org.opendaylight.controller.sal.core.api.BrokerService;
-import org.opendaylight.controller.sal.core.api.Consumer;
-import org.opendaylight.controller.sal.core.api.Provider;
-import org.opendaylight.controller.sal.core.api.RpcImplementation;
-import org.opendaylight.controller.sal.core.spi.BrokerModule;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.osgi.framework.BundleContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class BrokerImpl implements Broker {
-    private static Logger log = LoggerFactory.getLogger(BrokerImpl.class);
-
-    // Broker Generic Context
-    private Set<ConsumerSessionImpl> sessions = Collections
-            .synchronizedSet(new HashSet<ConsumerSessionImpl>());
-    private Set<ProviderSessionImpl> providerSessions = Collections
-            .synchronizedSet(new HashSet<ProviderSessionImpl>());
-    private Set<BrokerModule> modules = Collections
-            .synchronizedSet(new HashSet<BrokerModule>());
-    private Map<Class<? extends BrokerService>, BrokerModule> serviceProviders = Collections
-            .synchronizedMap(new HashMap<Class<? extends BrokerService>, BrokerModule>());
-
-    // RPC Context
-    private Map<QName, RpcImplementation> rpcImpls = Collections
-            .synchronizedMap(new HashMap<QName, RpcImplementation>());
-
-    // Implementation specific
-    private ExecutorService executor;
-
-    private BundleContext bundleContext;
-
-    @Override
-    public ConsumerSession registerConsumer(Consumer consumer,BundleContext ctx) {
-        checkPredicates(consumer);
-        log.info("Registering consumer " + consumer);
-        ConsumerSessionImpl session = newSessionFor(consumer,ctx);
-        consumer.onSessionInitiated(session);
-        sessions.add(session);
-        return session;
-    }
-
-    @Override
-    public ProviderSession registerProvider(Provider provider,BundleContext ctx) {
-        checkPredicates(provider);
-
-        ProviderSessionImpl session = newSessionFor(provider,ctx);
-        provider.onSessionInitiated(session);
-        providerSessions.add(session);
-        return session;
-    }
-
-    public void addModule(BrokerModule module) {
-        log.info("Registering broker module " + module);
-        if (modules.contains(module)) {
-            log.error("Module already registered");
-            throw new IllegalArgumentException("Module already exists.");
-        }
-    
-        Set<Class<? extends BrokerService>> provServices = module
-                .getProvidedServices();
-        for (Class<? extends BrokerService> serviceType : provServices) {
-            log.info("  Registering session service implementation: "
-                    + serviceType.getCanonicalName());
-            serviceProviders.put(serviceType, module);
-        }
-    }
-
-    public <T extends BrokerService> T serviceFor(Class<T> service,
-            ConsumerSessionImpl session) {
-        BrokerModule prov = serviceProviders.get(service);
-        if (prov == null) {
-            log.warn("Service " + service.toString() + " is not supported");
-            return null;
-        }
-        return prov.getServiceForSession(service, session);
-    }
-
-    // RPC Functionality
-    
-    private void addRpcImplementation(QName rpcType,
-            RpcImplementation implementation) {
-        synchronized (rpcImpls) {
-            if (rpcImpls.get(rpcType) != null) {
-                throw new IllegalStateException("Implementation for rpc "
-                        + rpcType + " is already registered.");
-            }
-            rpcImpls.put(rpcType, implementation);
-        }
-        // TODO Add notification for availability of Rpc Implementation
-    }
-
-    private void removeRpcImplementation(QName rpcType,
-            RpcImplementation implToRemove) {
-        synchronized (rpcImpls) {
-            if (implToRemove == rpcImpls.get(rpcType)) {
-                rpcImpls.remove(rpcType);
-            }
-        }
-        // TODO Add notification for removal of Rpc Implementation
-    }
-
-    private Future<RpcResult<CompositeNode>> invokeRpc(QName rpc,
-            CompositeNode input) {
-        RpcImplementation impl = rpcImpls.get(rpc);
-        // if()
-
-        Callable<RpcResult<CompositeNode>> call = callableFor(impl,
-                rpc, input);
-        Future<RpcResult<CompositeNode>> result = executor.submit(call);
-
-        return result;
-    }
-    
-    // Validation
-
-    private void checkPredicates(Provider prov) {
-        if (prov == null)
-            throw new IllegalArgumentException("Provider should not be null.");
-        for (ProviderSessionImpl session : providerSessions) {
-            if (prov.equals(session.getProvider()))
-                throw new IllegalStateException("Provider already registered");
-        }
-
-    }
-
-    private void checkPredicates(Consumer cons) {
-        if (cons == null)
-            throw new IllegalArgumentException("Consumer should not be null.");
-        for (ConsumerSessionImpl session : sessions) {
-            if (cons.equals(session.getConsumer()))
-                throw new IllegalStateException("Consumer already registered");
-        }
-    }
-
-    // Private Factory methods
-    
-    private ConsumerSessionImpl newSessionFor(Consumer provider, BundleContext ctx) {
-        return new ConsumerSessionImpl(provider,ctx);
-    }
-
-    private ProviderSessionImpl newSessionFor(Provider provider, BundleContext ctx) {
-        return new ProviderSessionImpl(provider,ctx);
-    }
-
-    private void consumerSessionClosed(ConsumerSessionImpl consumerSessionImpl) {
-        sessions.remove(consumerSessionImpl);
-        providerSessions.remove(consumerSessionImpl);
-    }
-
-    private static Callable<RpcResult<CompositeNode>> callableFor(
-            final RpcImplementation implemenation, final QName rpc,
-            final CompositeNode input) {
-
-        return new Callable<RpcResult<CompositeNode>>() {
-
-            @Override
-            public RpcResult<CompositeNode> call() throws Exception {
-                return implemenation.invokeRpc(rpc, input);
-            }
-        };
-    }
-    
-    private class ConsumerSessionImpl implements ConsumerSession {
-
-        private final Consumer consumer;
-
-        private Map<Class<? extends BrokerService>, BrokerService> instantiatedServices = Collections
-                .synchronizedMap(new HashMap<Class<? extends BrokerService>, BrokerService>());
-        private boolean closed = false;
-
-        private BundleContext context;
-
-        public Consumer getConsumer() {
-            return consumer;
-        }
-
-        public ConsumerSessionImpl(Consumer consumer, BundleContext ctx) {
-            this.consumer = consumer;
-            this.context = ctx;
-        }
-
-        @Override
-        public Future<RpcResult<CompositeNode>> rpc(QName rpc,
-                CompositeNode input) {
-            return BrokerImpl.this.invokeRpc(rpc, input);
-        }
-
-        @Override
-        public <T extends BrokerService> T getService(Class<T> service) {
-            BrokerService potential = instantiatedServices.get(service);
-            if (potential != null) {
-                @SuppressWarnings("unchecked")
-                T ret = (T) potential;
-                return ret;
-            }
-            T ret = BrokerImpl.this.serviceFor(service, this);
-            if (ret != null) {
-                instantiatedServices.put(service, ret);
-            }
-            return ret;
-        }
-
-        @Override
-        public void close() {
-            Collection<BrokerService> toStop = instantiatedServices.values();
-            this.closed = true;
-            for (BrokerService brokerService : toStop) {
-                brokerService.closeSession();
-            }
-            BrokerImpl.this.consumerSessionClosed(this);
-        }
-
-        @Override
-        public boolean isClosed() {
-            return closed;
-        }
-
-    }
-
-    private class ProviderSessionImpl extends ConsumerSessionImpl implements
-            ProviderSession {
-
-        private Provider provider;
-        private Map<QName, RpcImplementation> sessionRpcImpls = Collections.synchronizedMap(new HashMap<QName, RpcImplementation>());
-
-        public ProviderSessionImpl(Provider provider, BundleContext ctx) {
-            super(null,ctx);
-            this.provider = provider;
-        }
-
-        @Override
-        public void addRpcImplementation(QName rpcType,
-                RpcImplementation implementation)
-                throws IllegalArgumentException {
-            if (rpcType == null) {
-                throw new IllegalArgumentException("rpcType must not be null");
-            }
-            if (implementation == null) {
-                throw new IllegalArgumentException(
-                        "Implementation must not be null");
-            }
-            BrokerImpl.this.addRpcImplementation(rpcType, implementation);
-            sessionRpcImpls.put(rpcType, implementation);
-        }
-
-        @Override
-        public void removeRpcImplementation(QName rpcType,
-                RpcImplementation implToRemove) throws IllegalArgumentException {
-            RpcImplementation localImpl = rpcImpls.get(rpcType);
-            if (localImpl != implToRemove) {
-                throw new IllegalStateException(
-                        "Implementation was not registered in this session");
-            }
-
-            BrokerImpl.this.removeRpcImplementation(rpcType, implToRemove);
-            sessionRpcImpls.remove(rpcType);
-        }
-
-        public Provider getProvider() {
-            return this.provider;
-        }
-
-    }
-
-    public void setBundleContext(BundleContext context) {
-        this.bundleContext = context;
-    }
-}
diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.xtend b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.xtend
new file mode 100644 (file)
index 0000000..6b57cdc
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2013 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.controller.sal.dom.broker;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import org.opendaylight.controller.sal.core.api.Broker;
+import org.opendaylight.controller.sal.core.api.BrokerService;
+import org.opendaylight.controller.sal.core.api.Consumer;
+import org.opendaylight.controller.sal.core.api.Provider;
+import org.opendaylight.controller.sal.core.api.RpcImplementation;
+import org.opendaylight.controller.sal.core.spi.BrokerModule;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.osgi.framework.BundleContext;
+import org.slf4j.LoggerFactory;
+
+public class BrokerImpl implements Broker {
+    private static val log = LoggerFactory.getLogger(BrokerImpl);
+
+    // Broker Generic Context
+    private val Set<ConsumerContextImpl> sessions = Collections.synchronizedSet(new HashSet<ConsumerContextImpl>());
+    private val Set<ProviderContextImpl> providerSessions = Collections.synchronizedSet(
+        new HashSet<ProviderContextImpl>());
+    private val Set<BrokerModule> modules = Collections.synchronizedSet(new HashSet<BrokerModule>());
+    private val Map<Class<? extends BrokerService>, BrokerModule> serviceProviders = Collections.
+        synchronizedMap(new HashMap<Class<? extends BrokerService>, BrokerModule>());
+
+    // RPC Context
+    private val Map<QName, RpcImplementation> rpcImpls = Collections.synchronizedMap(
+        new HashMap<QName, RpcImplementation>());
+
+    // Implementation specific
+    @Property
+    private var ExecutorService executor;
+    @Property
+    private var BundleContext bundleContext;
+
+    override registerConsumer(Consumer consumer, BundleContext ctx) {
+        checkPredicates(consumer);
+        log.info("Registering consumer " + consumer);
+        val session = newSessionFor(consumer, ctx);
+        consumer.onSessionInitiated(session);
+        sessions.add(session);
+        return session;
+    }
+
+    override registerProvider(Provider provider, BundleContext ctx) {
+        checkPredicates(provider);
+
+        val session = newSessionFor(provider, ctx);
+        provider.onSessionInitiated(session);
+        providerSessions.add(session);
+        return session;
+    }
+
+    public def addModule(BrokerModule module) {
+        log.info("Registering broker module " + module);
+        if(modules.contains(module)) {
+            log.error("Module already registered");
+            throw new IllegalArgumentException("Module already exists.");
+        }
+
+        val provServices = module.getProvidedServices();
+        for (Class<? extends BrokerService> serviceType : provServices) {
+            log.info("  Registering session service implementation: " + serviceType.getCanonicalName());
+            serviceProviders.put(serviceType, module);
+        }
+    }
+
+    public def <T extends BrokerService> T serviceFor(Class<T> service, ConsumerContextImpl session) {
+        val prov = serviceProviders.get(service);
+        if(prov == null) {
+            log.warn("Service " + service.toString() + " is not supported");
+            return null;
+        }
+        return prov.getServiceForSession(service, session);
+    }
+
+    // RPC Functionality
+    protected def void addRpcImplementation(QName rpcType, RpcImplementation implementation) {
+        if(rpcImpls.get(rpcType) != null) {
+            throw new IllegalStateException("Implementation for rpc " + rpcType + " is already registered.");
+        }
+
+        //TODO: Add notification for availability of Rpc Implementation
+        rpcImpls.put(rpcType, implementation);
+    }
+
+    protected def void removeRpcImplementation(QName rpcType, RpcImplementation implToRemove) {
+        if(implToRemove == rpcImpls.get(rpcType)) {
+            rpcImpls.remove(rpcType);
+        }
+    }
+
+    protected def Future<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
+        val impl = rpcImpls.get(rpc);
+        val result = executor.submit([|impl.invokeRpc(rpc, input)] as Callable<RpcResult<CompositeNode>>);
+        return result;
+    }
+
+    // Validation
+    private def void checkPredicates(Provider prov) {
+        if(prov == null)
+            throw new IllegalArgumentException("Provider should not be null.");
+        for (ProviderContextImpl session : providerSessions) {
+            if(prov.equals(session.getProvider()))
+                throw new IllegalStateException("Provider already registered");
+        }
+
+    }
+
+    private def void checkPredicates(Consumer cons) {
+        if(cons == null)
+            throw new IllegalArgumentException("Consumer should not be null.");
+        for (ConsumerContextImpl session : sessions) {
+            if(cons.equals(session.getConsumer()))
+                throw new IllegalStateException("Consumer already registered");
+        }
+    }
+
+    // Private Factory methods
+    private def ConsumerContextImpl newSessionFor(Consumer provider, BundleContext ctx) {
+        val ret = new ConsumerContextImpl(provider, ctx);
+        ret.broker = this;
+        return ret;
+    }
+
+    private def ProviderContextImpl newSessionFor(Provider provider, BundleContext ctx) {
+        val ret = new ProviderContextImpl(provider, ctx);
+        ret.broker = this;
+        return ret;
+    }
+
+    protected def void consumerSessionClosed(ConsumerContextImpl consumerContextImpl) {
+        sessions.remove(consumerContextImpl);
+        providerSessions.remove(consumerContextImpl);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ConsumerContextImpl.xtend b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ConsumerContextImpl.xtend
new file mode 100644 (file)
index 0000000..d79eeee
--- /dev/null
@@ -0,0 +1,60 @@
+package org.opendaylight.controller.sal.dom.broker
+
+import java.util.Collections
+import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
+import java.util.HashMap
+import org.opendaylight.controller.sal.core.api.BrokerService
+import org.opendaylight.controller.sal.core.api.Consumer
+import org.osgi.framework.BundleContext
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.data.api.CompositeNode
+
+class ConsumerContextImpl implements ConsumerSession {
+
+    @Property
+    private val Consumer consumer;
+
+    @Property
+    private var BrokerImpl broker;
+
+    private val instantiatedServices = Collections.synchronizedMap(
+        new HashMap<Class<? extends BrokerService>, BrokerService>());
+    private boolean closed = false;
+
+    private BundleContext context;
+
+    public new(Consumer consumer, BundleContext ctx) {
+        this._consumer = consumer;
+        this.context = ctx;
+    }
+
+    override rpc(QName rpc, CompositeNode input) {
+        return broker.invokeRpc(rpc, input);
+    }
+
+    override <T extends BrokerService> T getService(Class<T> service) {
+        val potential = instantiatedServices.get(service);
+        if(potential != null) {
+            val ret = potential as T;
+            return ret;
+        }
+        val ret = broker.serviceFor(service, this);
+        if(ret != null) {
+            instantiatedServices.put(service, ret);
+        }
+        return ret;
+    }
+
+    override close() {
+        val toStop = instantiatedServices.values();
+        this.closed = true;
+        for (BrokerService brokerService : toStop) {
+            brokerService.closeSession();
+        }
+        broker.consumerSessionClosed(this);
+    }
+
+    override isClosed() {
+        return closed;
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ProviderContextImpl.xtend b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/ProviderContextImpl.xtend
new file mode 100644 (file)
index 0000000..8e402e2
--- /dev/null
@@ -0,0 +1,46 @@
+package org.opendaylight.controller.sal.dom.broker
+
+import org.opendaylight.controller.sal.core.api.Broker.ProviderSession
+import org.opendaylight.controller.sal.core.api.Provider
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.controller.sal.core.api.RpcImplementation
+import org.osgi.framework.BundleContext
+import static java.util.Collections.*
+import java.util.Collections
+import java.util.HashMap
+
+class ProviderContextImpl extends ConsumerContextImpl implements ProviderSession {
+
+    @Property
+    private val Provider provider;
+
+    private val rpcImpls = Collections.synchronizedMap(new HashMap<QName, RpcImplementation>());
+
+    new(Provider provider, BundleContext ctx) {
+        super(null, ctx);
+        this._provider = provider;
+    }
+
+    override addRpcImplementation(QName rpcType, RpcImplementation implementation) throws IllegalArgumentException {
+        if(rpcType == null) {
+            throw new IllegalArgumentException("rpcType must not be null");
+        }
+        if(implementation == null) {
+            throw new IllegalArgumentException("Implementation must not be null");
+        }
+        broker.addRpcImplementation(rpcType, implementation);
+        rpcImpls.put(rpcType, implementation);
+    }
+
+    override removeRpcImplementation(QName rpcType, RpcImplementation implToRemove) throws IllegalArgumentException {
+        val localImpl = rpcImpls.get(rpcType);
+        if(localImpl != implToRemove) {
+            throw new IllegalStateException(
+                "Implementation was not registered in this session");
+        }
+
+        broker.removeRpcImplementation(rpcType, implToRemove);
+        rpcImpls.remove(rpcType);
+    }
+
+}