Fix networkId issue while making call to oof
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / MainThreadComponent.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019-2020 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;
23
24 import java.sql.Timestamp;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.LinkedBlockingQueue;
32
33 import javax.annotation.PostConstruct;
34
35 import org.onap.dcaegen2.services.sonhms.child.ChildThread;
36 import org.onap.dcaegen2.services.sonhms.child.Graph;
37 import org.onap.dcaegen2.services.sonhms.dao.FixedPciCellsRepository;
38 import org.onap.dcaegen2.services.sonhms.utils.BeanUtil;
39 import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
40 import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.scheduling.annotation.Scheduled;
45 import org.springframework.stereotype.Component;
46
47 @Component
48 public class MainThreadComponent {
49
50     private static Logger log = LoggerFactory.getLogger(MainThreadComponent.class);
51
52     @Autowired
53     private NewSdnrNotification newNotification;
54     
55     @Autowired
56     private NewPmNotification newPmNotification;
57     
58     @Autowired
59     private NewFmNotification newFmNotification;
60     
61     /**
62      * main thread initialization.
63      */
64     @PostConstruct
65     public void init() {
66         log.debug("initializing main thread");
67         Thread thread = new Thread(new MainThread(newNotification, newFmNotification));
68         thread.start();
69         Thread pmThread = new Thread(new PmThread(newPmNotification));
70         pmThread.start();
71     }
72
73         @Scheduled(fixedRate = 300000, initialDelay = 60000)
74         public void checkFixedPciTable() {
75                 log.info("Inside checkFixedPciTable method");
76                 FixedPciCellsRepository fixedPciCellsRepository = BeanUtil.getBean(FixedPciCellsRepository.class);
77                 List<String> fixedPciCellsList = fixedPciCellsRepository.getFixedPciCells();
78                 if (!fixedPciCellsList.isEmpty()) {
79                         log.info("Fixed Pci table not empty");
80                         Timestamp lastInvokedOofTimeStamp = ChildThread.getLastInvokedOofTimeStamp();
81                         Timestamp fixedPciCreatedAt = fixedPciCellsRepository.getTimeStampforFixedPci();
82                         Long difference = fixedPciCreatedAt.getTime() - lastInvokedOofTimeStamp.getTime();
83                         Configuration configuration = Configuration.getInstance();
84
85                         if (Math.abs(difference) > configuration.getPolicyFixedPciTimeInterval()) {
86                                 log.info("Creating new child thread for sending fixedPciCells");
87                                 List<Graph> cluster = new ArrayList<>();
88                                 BlockingQueue<List<String>> childStatusQueue = new LinkedBlockingQueue<>();
89                                 EventHandler eventHandler = new EventHandler(childStatusQueue,
90                                                 Executors.newFixedThreadPool(Configuration.getInstance().getMaximumClusters()), new HashMap<>(),
91                                                 new ClusterUtils(), new ThreadUtils());
92                                 ExecutorService pool = Executors.newFixedThreadPool(5);
93                                 ThreadUtils threadUtils = new ThreadUtils();
94                                 boolean result = threadUtils.createNewThread(cluster, childStatusQueue, pool, eventHandler, null);
95                                 log.info("Child Thread creation result:"+ result);
96                         }
97
98                 } else {
99                         log.info("Exiting function fixedPci table empty");
100                 }
101         }
102 }