Untangle the XML/Hello message decoders
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / SingletonHolder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ThreadFactory;
13
14 import javassist.ClassPool;
15
16 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
17 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
18
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import com.google.common.util.concurrent.ThreadFactoryBuilder;
22
23 public class SingletonHolder {
24
25     public static final ClassPool CLASS_POOL = new ClassPool();
26     public static final org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator(
27             CLASS_POOL);
28     public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
29     public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
30     private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
31     private static ListeningExecutorService COMMIT_EXECUTOR = null;
32     private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
33
34     public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
35         if (NOTIFICATION_EXECUTOR == null) {
36             NOTIFICATION_EXECUTOR = createNamedExecutor("md-sal-binding-notification-%d");
37         }
38         return NOTIFICATION_EXECUTOR;
39     }
40
41     /**
42      * @deprecated This method is only used from configuration modules and thus callers of it
43      *             should use service injection to make the executor configurable.
44      */
45     @Deprecated
46     public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
47         if (COMMIT_EXECUTOR == null) {
48             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
49             /*
50              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
51              *        ordering guarantees, which means that using a concurrent threadpool results
52              *        in application data being committed in random order, potentially resulting
53              *        in inconsistent data being present. Once proper primitives are introduced,
54              *        concurrency can be reintroduced.
55              */
56             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
57             COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
58         }
59
60         return COMMIT_EXECUTOR;
61     }
62
63     private static ListeningExecutorService createNamedExecutor(String format) {
64         ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(format).build();
65         ExecutorService executor = Executors.newCachedThreadPool(factory);
66         return MoreExecutors.listeningDecorator(executor);
67     }
68
69     public static ExecutorService getDefaultChangeEventExecutor() {
70         if (CHANGE_EVENT_EXECUTOR == null) {
71             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
72             /*
73              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
74              *        ordering guarantees, which means that using a concurrent threadpool results
75              *        in application data being committed in random order, potentially resulting
76              *        in inconsistent data being present. Once proper primitives are introduced,
77              *        concurrency can be reintroduced.
78              */
79             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
80             CHANGE_EVENT_EXECUTOR  = MoreExecutors.listeningDecorator(executor);
81         }
82
83         return CHANGE_EVENT_EXECUTOR;
84     }
85 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.