[SLICEMS]Fix bug that config thread hang up when cbs policy is empty
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / service / ConfigThread.java
1 /*******************************************************************************\r
2  *  ============LICENSE_START=======================================================\r
3  *  slice-analysis-ms\r
4  *  ================================================================================\r
5  *   Copyright (C) 2022 Huawei Technologies Co., Ltd.\r
6  *   ==============================================================================\r
7  *     Licensed under the Apache License, Version 2.0 (the "License");\r
8  *     you may not use this file except in compliance with the License.\r
9  *     You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *     Unless required by applicable law or agreed to in writing, software\r
14  *     distributed under the License is distributed on an "AS IS" BASIS,\r
15  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *     See the License for the specific language governing permissions and\r
17  *     limitations under the License.\r
18  *     ============LICENSE_END=========================================================\r
19  *\r
20  *******************************************************************************/\r
21 \r
22 package org.onap.slice.analysis.ms.service;\r
23 \r
24 import org.onap.slice.analysis.ms.models.ConfigPolicy;\r
25 import org.onap.slice.analysis.ms.service.ccvpn.CCVPNPmDatastore;\r
26 import org.onap.slice.analysis.ms.utils.SpringContextUtil;\r
27 import org.slf4j.Logger;\r
28 import org.slf4j.LoggerFactory;\r
29 \r
30 /**\r
31  * This thread is used to convert ccvpn runtime configurations from policy to our local memory CCVPNPmDatastore\r
32  */\r
33 public class ConfigThread extends Thread{\r
34 \r
35     CCVPNPmDatastore ccvpnPmDatastore = (CCVPNPmDatastore) SpringContextUtil.getBean(CCVPNPmDatastore.class);\r
36     private static Logger log = LoggerFactory.getLogger(ConfigThread.class);\r
37 \r
38 \r
39     public ConfigThread () {\r
40         super();\r
41     }\r
42 \r
43     public void run() {\r
44         log.info("Config Thread is starting...");\r
45         boolean done = false;\r
46         while(!done) {\r
47             try {\r
48                 Thread.sleep(1000);\r
49                 ConfigPolicy configPolicy = ConfigPolicy.getInstance();\r
50                 if(configPolicy.getConfig() != null) {\r
51                     String cllId = null;\r
52                     Boolean clBwAssuranceStatus = null;\r
53                     int originalBw = 0;\r
54                     if(configPolicy.getConfig().containsKey("cllId")){\r
55                         cllId = String.valueOf(configPolicy.getConfig().get("cllId"));\r
56                     }\r
57                     if(configPolicy.getConfig().containsKey("closedLoopStatus")){\r
58                         clBwAssuranceStatus = String.valueOf(configPolicy.getConfig().get("closedLoopStatus")).equalsIgnoreCase("true");\r
59                     }\r
60                     if(configPolicy.getConfig().containsKey("originalBw")){\r
61                         originalBw = Integer.parseInt(String.valueOf(configPolicy.getConfig().get("originalBw")));\r
62                     }\r
63                     if(cllId!=null && clBwAssuranceStatus!=null){\r
64                         ccvpnPmDatastore.updateConfigFromPolicy(cllId, clBwAssuranceStatus, originalBw);\r
65                     }\r
66                 } else {\r
67                     log.error("Config policy is empty, nothing to update.");\r
68                 }\r
69 \r
70             } catch (Exception e) {\r
71                 log.error("Exception in Config Thread ", e);\r
72                 done = true;\r
73             }\r
74         }\r
75     }\r
76 }\r