change version
[holmes/dsa.git] / dmaap-dsa / src / main / java / org / onap / holmes / dsa / dmaappolling / DMaaPResponseUtil.java
1 /*
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.dsa.dmaappolling;
17
18 import com.alibaba.fastjson.JSON;
19 import com.alibaba.fastjson.JSONArray;
20 import com.alibaba.fastjson.JSONObject;
21 import org.jvnet.hk2.annotations.Service;
22 import org.onap.holmes.common.api.stat.AlarmAdditionalField;
23 import org.onap.holmes.common.api.stat.VesAlarm;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 @Service
30 public class DMaaPResponseUtil {
31
32     public VesAlarm convertJsonToVesAlarm(String responseJson) throws IOException {
33         JSONObject jsonNode = JSON.parseObject(responseJson);
34
35         VesAlarm vesAlarm = new VesAlarm();
36
37         JSONObject eventJson = JSON.parseObject(jsonNode.get("event") +"");
38         JSONObject commonEventHeaderJson = JSON.parseObject(eventJson.get("commonEventHeader") +"");
39         convertCommonEventHeaderJsonToEvent(commonEventHeaderJson, vesAlarm);
40
41         JSONObject faultFieldsJson = JSON.parseObject(eventJson.get("faultFields") +"");
42         convertFaultFieldsJsonToEvent(faultFieldsJson, vesAlarm);
43         return vesAlarm;
44     }
45
46     private void convertCommonEventHeaderJsonToEvent(JSONObject commonEventHeaderJson,
47                                                      VesAlarm vesAlarm) {
48         vesAlarm.setDomain((String) commonEventHeaderJson.get("domain"));
49         vesAlarm.setEventId((String) commonEventHeaderJson.get("eventId"));
50         vesAlarm.setEventName((String) commonEventHeaderJson.get("eventName"));
51         vesAlarm.setAlarmIsCleared(vesAlarm.getEventName().endsWith("Cleared") ? 1 : 0);
52         vesAlarm.setEventType(getTextElementByNode(commonEventHeaderJson, "eventType"));
53         vesAlarm.setInternalHeaderFields(
54                 getTextElementByNode(commonEventHeaderJson, "internalHeaderFields"));
55         vesAlarm.setLastEpochMicrosec(commonEventHeaderJson.getLong("lastEpochMicrosec"));
56         vesAlarm.setNfcNamingCode(getTextElementByNode(commonEventHeaderJson, "nfcNamingCode"));
57         vesAlarm.setNfNamingCode(getTextElementByNode(commonEventHeaderJson, "nfNamingCode"));
58         vesAlarm.setPriority((String) commonEventHeaderJson.get("priority"));
59         vesAlarm.setReportingEntityId(
60                 getTextElementByNode(commonEventHeaderJson, "reportingEntityId"));
61         vesAlarm.setReportingEntityName( (String) commonEventHeaderJson.get("reportingEntityName"));
62         vesAlarm.setSequence((Integer) commonEventHeaderJson.get("sequence"));
63         vesAlarm.setSourceId(getTextElementByNode(commonEventHeaderJson, "sourceId"));
64         vesAlarm.setSourceName( (String) commonEventHeaderJson.get("sourceName"));
65         vesAlarm.setStartEpochMicrosec(commonEventHeaderJson.getLong("startEpochMicrosec"));
66         vesAlarm.setVersion(commonEventHeaderJson.getLong("version"));
67     }
68
69     private void convertFaultFieldsJsonToEvent(JSONObject faultFieldsJson, VesAlarm vesAlarm) {
70         vesAlarm.setAlarmAdditionalInformation(getListElementByNode(faultFieldsJson, "alarmAdditionalInformation"));
71         vesAlarm.setAlarmCondition(faultFieldsJson.getString("alarmCondition"));
72         vesAlarm.setAlarmInterfaceA(getTextElementByNode(faultFieldsJson, "alarmInterfaceA"));
73         vesAlarm.setEventCategory(getTextElementByNode(faultFieldsJson,"eventCategory"));
74         vesAlarm.setEventSeverity(faultFieldsJson.getString("eventSeverity"));
75         vesAlarm.setEventSourceType(faultFieldsJson.getString("eventSourceType"));
76         vesAlarm.setFaultFieldsVersion(faultFieldsJson.getLong("faultFieldsVersion"));
77         vesAlarm.setSpecificProblem(faultFieldsJson.getString("specificProblem"));
78         vesAlarm.setVfStatus(faultFieldsJson.getString("vfStatus"));
79     }
80
81     private String getTextElementByNode(JSONObject jsonNode,String name){
82         if(jsonNode.get(name) != null){
83             return jsonNode.getString(name);
84         }
85         return null;
86     }
87
88     private Long getLongElementByNode(JSONObject jsonNode, String name) {
89         if(jsonNode.get(name) != null){
90             return jsonNode.getLong(name);
91         }
92         return null;
93     }
94
95     private List<AlarmAdditionalField> getListElementByNode(JSONObject jsonNode, String name){
96         List<AlarmAdditionalField> alarms = new ArrayList<AlarmAdditionalField>();
97         if (jsonNode.get(name) != null) {
98             JSONArray alarmAdditionalInformations = jsonNode.getJSONArray(name);
99             for (int i = 0; i < alarmAdditionalInformations.size(); i++) {
100                 JSONObject jsonObject = alarmAdditionalInformations.getJSONObject(i);
101                 if (jsonObject.get("name") != null
102                         && jsonObject.get("value") != null) {
103                     AlarmAdditionalField field = new AlarmAdditionalField();
104                     field.setName(getTextElementByNode(jsonObject, "name"));
105                     field.setValue(getTextElementByNode(jsonObject, "value"));
106                     alarms.add(field);
107                 }
108             }
109         }
110         return alarms;
111     }
112 }