Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / utils / ThreadUtils.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
6  *   ==============================================================================
7  *     Licensed under the Apache License, Version 2.0 (the "License");
8  *     you may not use this file except in compliance with the License.
9  *     You may obtain a copy of the License at
10  *  
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *  
13  *     Unless required by applicable law or agreed to in writing, software
14  *     distributed under the License is distributed on an "AS IS" BASIS,
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *     See the License for the specific language governing permissions and
17  *     limitations under the License.
18  *     ============LICENSE_END=========================================================
19  *  
20  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms.utils;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.LinkedBlockingQueue;
31
32 import org.onap.dcaegen2.services.sonhms.EventHandler;
33 import org.onap.dcaegen2.services.sonhms.HoMetricsComponent;
34 import org.onap.dcaegen2.services.sonhms.child.ChildThread;
35 import org.onap.dcaegen2.services.sonhms.child.Graph;
36 import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
37 import org.onap.dcaegen2.services.sonhms.model.ThreadId;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42
43 public class ThreadUtils {
44     
45     private static Logger log = LoggerFactory.getLogger(ThreadUtils.class);
46     
47     /**
48      * Create thread.
49      */
50     public Boolean createNewThread(List<Graph> newClusters, BlockingQueue<List<String>> childStatusQueue,
51             ExecutorService pool, EventHandler eventHandler,String cellId) {
52         
53         
54         for (Graph cluster : newClusters) {
55
56             BlockingQueue<Map<CellPciPair, ArrayList<CellPciPair>>> queue = new LinkedBlockingQueue<>();
57             ThreadId threadId = new ThreadId();
58             threadId.setChildThreadId(0);
59             ChildThread child = new ChildThread(childStatusQueue, cluster, queue, threadId, new HoMetricsComponent());
60             
61             log.info("Creating new child thread");
62             pool.execute(child);
63             waitForThreadId(threadId);
64             UUID clusterId = UUID.randomUUID();
65
66             ClusterUtils clusterUtils = new ClusterUtils();
67             clusterUtils.saveCluster(cluster, clusterId, threadId.getChildThreadId());
68             EventHandler.addChildThreadMap(threadId.getChildThreadId(), child);
69             eventHandler.addChildStatus(threadId.getChildThreadId(), "processingNotifications");
70         }
71         return true;
72         
73     }
74     
75     private void waitForThreadId(ThreadId threadId) {
76
77         ThreadId thread = threadId;
78         try {
79             synchronized (thread) {
80                 while (thread.getChildThreadId() == 0) {
81                     thread.wait();
82                 }
83             }
84         } catch (InterruptedException e) {
85
86             log.error("ChildThread queue error {}", e); 
87             Thread.currentThread().interrupt();
88         }
89     }
90
91 }