SONAR suggestion - Synchronize initialization of static fields

Change-Id: I2ddf8220bda49f235ddfc5713174c11a147df354
diff --git a/apps/acl/src/main/java/org/onosproject/acl/AclRule.java b/apps/acl/src/main/java/org/onosproject/acl/AclRule.java
index a5cc297..ff01aa4 100644
--- a/apps/acl/src/main/java/org/onosproject/acl/AclRule.java
+++ b/apps/acl/src/main/java/org/onosproject/acl/AclRule.java
@@ -43,7 +43,7 @@
     private final short dstTpPort;
     private final Action action;
 
-    private static IdGenerator idGenerator;
+    protected static IdGenerator idGenerator;
 
     /**
      * Enum type for ACL rule's action.
diff --git a/apps/acl/src/test/java/org/onosproject/acl/AclWebResourceTest.java b/apps/acl/src/test/java/org/onosproject/acl/AclWebResourceTest.java
index 13c8201..897e8b4 100644
--- a/apps/acl/src/test/java/org/onosproject/acl/AclWebResourceTest.java
+++ b/apps/acl/src/test/java/org/onosproject/acl/AclWebResourceTest.java
@@ -67,8 +67,7 @@
                 .add(AclStore.class, mockAclStore);
         BaseResource.setServiceDirectory(testDirectory);
 
-        IdGenerator idGenerator = new MockIdGenerator();
-        AclRule.bindIdGenerator(idGenerator);
+        AclRule.idGenerator = new MockIdGenerator();
     }
 
     @After
diff --git a/apps/gluon/src/main/java/org/onosproject/gluon/rsc/GluonServer.java b/apps/gluon/src/main/java/org/onosproject/gluon/rsc/GluonServer.java
index f4f400e..031ba69 100644
--- a/apps/gluon/src/main/java/org/onosproject/gluon/rsc/GluonServer.java
+++ b/apps/gluon/src/main/java/org/onosproject/gluon/rsc/GluonServer.java
@@ -86,10 +86,10 @@
 
 public class GluonServer {
 
-    private static String protonKeyUri;
-    private static String serverUri;
+    private String protonKeyUri;
+    private String serverUri;
 
-    private static CloseableHttpAsyncClient httpClient;
+    private CloseableHttpAsyncClient httpClient;
 
     //store gluon server supported subkeys
     private List<String> subKeys = new LinkedList<>();
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/cli/VplsCommand.java b/apps/vpls/src/main/java/org/onosproject/vpls/cli/VplsCommand.java
index 35caa97..72489b1 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/cli/VplsCommand.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/cli/VplsCommand.java
@@ -101,8 +101,8 @@
             COLOR_ERROR + "Interface " + BOLD + "%s" + RESET + COLOR_ERROR +
                     " cannot be removed from VPLS " + BOLD + "%s" + RESET + ".";
 
-    protected static Vpls vpls;
-    protected static InterfaceService interfaceService;
+    protected Vpls vpls;
+    protected InterfaceService interfaceService;
 
     @Argument(index = 0, name = "command", description = "Command name (add-if|" +
             "create|delete|list|rem-if|set-encap|show)",
@@ -377,7 +377,7 @@
      * @return true if the interface is already associated to a VPLS; false
      * otherwise
      */
-    private static boolean isIfaceAssociated(Interface iface) {
+    private boolean isIfaceAssociated(Interface iface) {
         return vpls.getAllVpls()
                 .stream()
                 .map(VplsData::interfaces)
diff --git a/apps/vpls/src/main/java/org/onosproject/vpls/cli/completer/VplsOptArgCompleter.java b/apps/vpls/src/main/java/org/onosproject/vpls/cli/completer/VplsOptArgCompleter.java
index 0624005..4b5ec46 100644
--- a/apps/vpls/src/main/java/org/onosproject/vpls/cli/completer/VplsOptArgCompleter.java
+++ b/apps/vpls/src/main/java/org/onosproject/vpls/cli/completer/VplsOptArgCompleter.java
@@ -38,8 +38,8 @@
  * VPLS optional argument completer.
  */
 public class VplsOptArgCompleter extends AbstractChoicesCompleter {
-    protected static Vpls vpls;
-    protected static InterfaceService interfaceService;
+    protected Vpls vpls;
+    protected InterfaceService interfaceService;
 
     @Override
     public List<String> choices() {
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
index 1eda703..aeaf84d 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
@@ -140,7 +140,7 @@
      *
      * @param service core service reference
      */
-    public static void setCoreService(CoreService service) {
+    public static synchronized void setCoreService(CoreService service) {
         coreService = service;
     }
 
diff --git a/core/api/src/main/java/org/onosproject/net/intent/Intent.java b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
index 7a541e7..b7af54f 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/Intent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/Intent.java
@@ -48,6 +48,7 @@
     private final ResourceGroup resourceGroup;
 
     private static IdGenerator idGenerator;
+    private static final Object ID_GENERATOR_LOCK = new Object();
 
     /**
      * Constructor for serializer.
@@ -248,8 +249,10 @@
      * @param newIdGenerator id generator
      */
     public static void bindIdGenerator(IdGenerator newIdGenerator) {
-        checkState(idGenerator == null, "Id generator is already bound.");
-        idGenerator = checkNotNull(newIdGenerator);
+        synchronized (ID_GENERATOR_LOCK) {
+            checkState(idGenerator == null, "Id generator is already bound.");
+            idGenerator = checkNotNull(newIdGenerator);
+        }
     }
 
     /**
@@ -260,8 +263,10 @@
      * @param oldIdGenerator the current id generator
      */
     public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
-        if (idGenerator == oldIdGenerator) {
-            idGenerator = null;
+        synchronized (ID_GENERATOR_LOCK) {
+            if (idGenerator == oldIdGenerator) {
+                idGenerator = null;
+            }
         }
     }
 
diff --git a/core/common/src/main/java/org/onosproject/common/DefaultTopology.java b/core/common/src/main/java/org/onosproject/common/DefaultTopology.java
index d94c88c..4b266a8 100644
--- a/core/common/src/main/java/org/onosproject/common/DefaultTopology.java
+++ b/core/common/src/main/java/org/onosproject/common/DefaultTopology.java
@@ -119,7 +119,7 @@
      *
      * @param linkWeigher new default link-weight
      */
-    public static void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
+    public static synchronized void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
         log.info("Setting new default link-weight function to {}", linkWeigher);
         defaultLinkWeigher = linkWeigher;
     }
@@ -130,7 +130,7 @@
      *
      * @param graphPathSearch new default algorithm
      */
-    public static void setDefaultGraphPathSearch(
+    public static synchronized void setDefaultGraphPathSearch(
             GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) {
         log.info("Setting new default graph path algorithm to {}", graphPathSearch);
         defaultGraphPathSearch = graphPathSearch;
diff --git a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/YangXmlUtils.java b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/YangXmlUtils.java
index bb4dfa5..08ef181 100644
--- a/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/YangXmlUtils.java
+++ b/drivers/utilities/src/main/java/org/onosproject/drivers/utilities/YangXmlUtils.java
@@ -195,7 +195,7 @@
      *
      * @return instance of YangXmlUtils
      */
-    public static YangXmlUtils getInstance() {
+    public static synchronized YangXmlUtils getInstance() {
         if (instance == null) {
             instance = new YangXmlUtils();
         }
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/dpi/impl/DpiStatisticsManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/dpi/impl/DpiStatisticsManager.java
index 10f82b4..b49c9cb 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/dpi/impl/DpiStatisticsManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/dpi/impl/DpiStatisticsManager.java
@@ -67,7 +67,7 @@
 @Service
 public class DpiStatisticsManager implements DpiStatisticsManagerService {
 
-    private static ServerSocket serverSocket;
+    private ServerSocket serverSocket;
     private static int port = 11990; // socket server listening port
 
     private final Logger log = getLogger(getClass());
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbApplicationService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbApplicationService.java
index bce4406..7ec8ed1 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbApplicationService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbApplicationService.java
@@ -59,7 +59,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ApplicationService applicationService;
 
-    private static ApplicationServiceNbServerInternal instance = null;
+    private ApplicationServiceNbServerInternal instance = null;
 
 
     @Activate
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbComponentConfigService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbComponentConfigService.java
index 8e0c2dd..673d126 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbComponentConfigService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbComponentConfigService.java
@@ -62,7 +62,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ComponentConfigService componentConfigService;
 
-    private static ComponentConfigServiceNbServerInternal instance = null;
+    private ComponentConfigServiceNbServerInternal instance = null;
 
     @Activate
     public void activate() {
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
index b583800..a06acf6 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
@@ -52,7 +52,7 @@
 
     private final Logger log = getLogger(getClass());
 
-    private static DeviceServiceNbServerInternal instance = null;
+    private DeviceServiceNbServerInternal instance = null;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected GrpcServiceRegistry registry;
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbHostService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbHostService.java
index d5e3660..2ad2bf6 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbHostService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbHostService.java
@@ -74,7 +74,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
-    private static HostServiceNBServerInternal instance = null;
+    private HostServiceNBServerInternal instance = null;
 
     @Activate
     public void activate() {
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbMastershipService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbMastershipService.java
index 1495467..34b8cd9 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbMastershipService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbMastershipService.java
@@ -63,7 +63,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected MastershipService mastershipService;
 
-    private static MastershipServiceNbServerInternal instance = null;
+    private MastershipServiceNbServerInternal instance = null;
 
     @Activate
     public void activate() {
diff --git a/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/BasicPceccHandler.java b/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/BasicPceccHandler.java
index c547b35..7376c14 100644
--- a/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/BasicPceccHandler.java
+++ b/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/BasicPceccHandler.java
@@ -113,7 +113,7 @@
      *
      * @return this class single instance
      */
-    public static BasicPceccHandler getInstance() {
+    public static synchronized BasicPceccHandler getInstance() {
         if (crHandlerInstance == null) {
             crHandlerInstance = new BasicPceccHandler();
         }
diff --git a/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/PceccSrTeBeHandler.java b/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/PceccSrTeBeHandler.java
index 8d50149..682f242 100644
--- a/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/PceccSrTeBeHandler.java
+++ b/protocols/pcep/server/ctl/src/main/java/org/onosproject/pcep/server/impl/PceccSrTeBeHandler.java
@@ -96,7 +96,7 @@
      *
      * @return this class single instance
      */
-    public static PceccSrTeBeHandler getInstance() {
+    public static synchronized PceccSrTeBeHandler getInstance() {
         if (srTeHandlerInstance == null) {
             srTeHandlerInstance = new PceccSrTeBeHandler();
         }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationResource.java b/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationResource.java
index 16f288a..d8f5a44 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationResource.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationResource.java
@@ -41,7 +41,8 @@
 @Path("applications")
 public class ApplicationResource extends BaseResource {
 
-    static String lastInstalledAppName = null;
+    private static String lastInstalledAppName = null;
+    private static final Object LAST_INSTALLED_APP_NAME_LOCK = new Object();
 
 
     @Path("upload")
@@ -51,7 +52,9 @@
                            @FormDataParam("file") InputStream stream) throws IOException {
         ApplicationAdminService service = get(ApplicationAdminService.class);
         Application app = service.install(stream);
-        lastInstalledAppName = app.id().name();
+        synchronized (LAST_INSTALLED_APP_NAME_LOCK) {
+            lastInstalledAppName = app.id().name();
+        }
         if (Objects.equals(activate, "true")) {
             service.activate(app.id());
         }
@@ -87,4 +90,10 @@
         Application app = service.getApplication(appId);
         return Response.ok(app.icon()).build();
     }
+
+    static String getLastInstalledAppName() {
+        synchronized (LAST_INSTALLED_APP_NAME_LOCK) {
+            return lastInstalledAppName;
+        }
+    }
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationViewMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationViewMessageHandler.java
index 984dce2..6ed92a5 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationViewMessageHandler.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/ApplicationViewMessageHandler.java
@@ -172,7 +172,7 @@
             // If the ID was not specified in the payload, use the name of the
             // most recently uploaded app.
             if (isNullOrEmpty(id)) {
-                id = ApplicationResource.lastInstalledAppName;
+                id = ApplicationResource.getLastInstalledAppName();
             }
 
             ApplicationId appId = as.getId(id);
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
index 24e02e6..cfd2025 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
@@ -38,6 +38,7 @@
     private static final long PING_DELAY_MS = 5000;
 
     private static UiWebSocketServlet instance;
+    private static final Object INSTANCE_LOCK = new Object();
 
     private ServiceDirectory directory = new DefaultServiceDirectory();
 
@@ -50,19 +51,23 @@
      * Closes all currently open UI web-sockets.
      */
     public static void closeAll() {
-        if (instance != null) {
-            instance.isStopped = true;
-            instance.sockets.forEach(UiWebSocket::close);
-            instance.sockets.clear();
-            instance.pruner.cancel();
-            instance.timer.cancel();
+        synchronized (INSTANCE_LOCK) {
+            if (instance != null) {
+                instance.isStopped = true;
+                instance.sockets.forEach(UiWebSocket::close);
+                instance.sockets.clear();
+                instance.pruner.cancel();
+                instance.timer.cancel();
+            }
         }
     }
 
     @Override
     public void init() throws ServletException {
         super.init();
-        instance = this;
+        synchronized (INSTANCE_LOCK) {
+            instance = this;
+        }
         timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
     }
 
@@ -93,8 +98,10 @@
      * @param payload message payload
      */
     static void sendToAll(String type, ObjectNode payload) {
-        if (instance != null) {
-            instance.sockets.forEach(ws -> ws.sendMessage(type, payload));
+        synchronized (INSTANCE_LOCK) {
+            if (instance != null) {
+                instance.sockets.forEach(ws -> ws.sendMessage(type, payload));
+            }
         }
     }
 
@@ -106,9 +113,11 @@
      * @param payload  message payload
      */
     static void sendToUser(String userName, String type, ObjectNode payload) {
-        if (instance != null) {
-            instance.sockets.stream().filter(ws -> userName.equals(ws.userName()))
-                    .forEach(ws -> ws.sendMessage(type, payload));
+        synchronized (INSTANCE_LOCK) {
+            if (instance != null) {
+                instance.sockets.stream().filter(ws -> userName.equals(ws.userName()))
+                        .forEach(ws -> ws.sendMessage(type, payload));
+            }
         }
     }