c62a8eca6b25a3c348cd093900ead7bb185ddefc
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl;
19
20 import java.util.EnumMap;
21 import java.util.Map;
22
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile.ConfigurationException;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.BaseSubConfig;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.ISubConfigHandler;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.devicemonitor.impl.DeviceMonitorProblems;
29
30 /**
31  * Configuration of devicemonitor, section [devicemonitor]
32  *   SeverityConnectionlossNeOAM=minor
33  *   SeverityConnectionlossOAM=major
34  *   SeverityConnectionlossMediator=critical
35  */
36 public class DmConfig extends BaseSubConfig{
37
38     private static final String SECTION_MARKER_TA = "devicemonitor";
39     private static final String PROPERTY_KEY_PREFIX_Severity = "Severity";
40
41     private static DmConfig dmConfig = null;
42
43     private Map<DeviceMonitorProblems, InternalSeverity> severty = new EnumMap<>(DeviceMonitorProblems.class);
44
45     /*
46      * Constructor
47      */
48     private DmConfig() {
49         super();
50     }
51
52     public DmConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
53         this(config, configHandler, true);
54     }
55
56     public DmConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
57             throws ConfigurationException {
58
59         super(config, configHandler, SECTION_MARKER_TA);
60
61         for (DeviceMonitorProblems problem : DeviceMonitorProblems.values()) {
62                 severty.put(problem, readProperty(problem));
63         }
64
65         if (save) {
66             for (DeviceMonitorProblems problem : DeviceMonitorProblems.values()) {
67                 configSetPropertyp(config, problem, severty.get(problem));
68             }
69             this.save();
70         }
71     }
72
73         public InternalSeverity getSeverity(DeviceMonitorProblems problem) {
74                 return severty.get(problem);
75         }
76
77     public static DmConfig getDefaultConfiguration() {
78         DmConfig c = new DmConfig();
79         for (DeviceMonitorProblems problem : DeviceMonitorProblems.values()) {
80                 c.severty.put(problem, InternalSeverity.Major);
81         }
82         return c;
83     }
84     public static boolean isInstantiated() {
85         return dmConfig != null;
86     }
87
88     public static DmConfig getDmConfig(IniConfigurationFile config, ISubConfigHandler configHandler) {
89         if (dmConfig == null) {
90             try {
91                 dmConfig = new DmConfig(config, configHandler);
92             } catch (ConfigurationException e) {
93                 dmConfig = DmConfig.getDefaultConfiguration();
94             }
95         }
96         return dmConfig;
97     }
98
99     public static DmConfig reload() {
100         if (dmConfig == null) {
101             return null;
102         }
103         DmConfig tmpConfig;
104         try {
105             tmpConfig = new DmConfig(dmConfig.getConfig(), dmConfig.getConfigHandler(), false);
106         } catch (ConfigurationException e) {
107             tmpConfig = DmConfig.getDefaultConfiguration();
108         }
109         dmConfig = tmpConfig;
110         return dmConfig;
111     }
112
113     public static void clear() {
114         dmConfig=null;
115     }
116
117     /*
118      * Private Helper functions
119      */
120     private static String getPropertyName(DeviceMonitorProblems problem) {
121         return PROPERTY_KEY_PREFIX_Severity+problem.name();
122     }
123
124     private static void configSetPropertyp(IniConfigurationFile config, DeviceMonitorProblems problem, InternalSeverity value) {
125         config.setProperty(SECTION_MARKER_TA + "."+getPropertyName(problem), value.getValueAsString());
126     }
127
128     private InternalSeverity readProperty(DeviceMonitorProblems problem) {
129         return InternalSeverity.valueOfString(getString(getPropertyName(problem), InternalSeverity.Major.getValueAsString()));
130     }
131
132 }