diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2019-02-20 08:44:40 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2019-02-20 08:44:40 +0800 |
commit | 1d8c0955bc266f08559385a58b2f40a9457cd5d9 (patch) | |
tree | 2a7b4aa6998e2611d6d6d852aad8f744d50871c3 /engine-d/src/main | |
parent | 37c34f3c2d55f92940f58eaa37dace6edaf91166 (diff) |
Merged DSA into the Engine Mgmt Module
Change-Id: If6b4e8345dcda8599606989ba9f4afd3497e48f1
Issue-ID: HOLMES-199
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'engine-d/src/main')
-rw-r--r-- | engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java | 112 | ||||
-rw-r--r-- | engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java | 116 |
2 files changed, 228 insertions, 0 deletions
diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java new file mode 100644 index 0000000..be30153 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/DMaaPResponseUtil.java @@ -0,0 +1,112 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.dsa.dmaappolling; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.api.stat.AlarmAdditionalField; +import org.onap.holmes.common.api.stat.VesAlarm; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Service +public class DMaaPResponseUtil { + + public VesAlarm convertJsonToVesAlarm(String responseJson) throws IOException { + JSONObject jsonNode = JSON.parseObject(responseJson); + + VesAlarm vesAlarm = new VesAlarm(); + + JSONObject eventJson = JSON.parseObject(jsonNode.get("event") +""); + JSONObject commonEventHeaderJson = JSON.parseObject(eventJson.get("commonEventHeader") +""); + convertCommonEventHeaderJsonToEvent(commonEventHeaderJson, vesAlarm); + + JSONObject faultFieldsJson = JSON.parseObject(eventJson.get("faultFields") +""); + convertFaultFieldsJsonToEvent(faultFieldsJson, vesAlarm); + return vesAlarm; + } + + private void convertCommonEventHeaderJsonToEvent(JSONObject commonEventHeaderJson, + VesAlarm vesAlarm) { + vesAlarm.setDomain((String) commonEventHeaderJson.get("domain")); + vesAlarm.setEventId((String) commonEventHeaderJson.get("eventId")); + vesAlarm.setEventName((String) commonEventHeaderJson.get("eventName")); + vesAlarm.setAlarmIsCleared(vesAlarm.getEventName().endsWith("Cleared") ? 1 : 0); + vesAlarm.setEventType(getTextElementByNode(commonEventHeaderJson, "eventType")); + vesAlarm.setInternalHeaderFields( + getTextElementByNode(commonEventHeaderJson, "internalHeaderFields")); + vesAlarm.setLastEpochMicrosec(commonEventHeaderJson.getLong("lastEpochMicrosec")); + vesAlarm.setNfcNamingCode(getTextElementByNode(commonEventHeaderJson, "nfcNamingCode")); + vesAlarm.setNfNamingCode(getTextElementByNode(commonEventHeaderJson, "nfNamingCode")); + vesAlarm.setPriority((String) commonEventHeaderJson.get("priority")); + vesAlarm.setReportingEntityId( + getTextElementByNode(commonEventHeaderJson, "reportingEntityId")); + vesAlarm.setReportingEntityName( (String) commonEventHeaderJson.get("reportingEntityName")); + vesAlarm.setSequence((Integer) commonEventHeaderJson.get("sequence")); + vesAlarm.setSourceId(getTextElementByNode(commonEventHeaderJson, "sourceId")); + vesAlarm.setSourceName( (String) commonEventHeaderJson.get("sourceName")); + vesAlarm.setStartEpochMicrosec(commonEventHeaderJson.getLong("startEpochMicrosec")); + vesAlarm.setVersion(commonEventHeaderJson.getLong("version")); + } + + private void convertFaultFieldsJsonToEvent(JSONObject faultFieldsJson, VesAlarm vesAlarm) { + vesAlarm.setAlarmAdditionalInformation(getListElementByNode(faultFieldsJson, "alarmAdditionalInformation")); + vesAlarm.setAlarmCondition(faultFieldsJson.getString("alarmCondition")); + vesAlarm.setAlarmInterfaceA(getTextElementByNode(faultFieldsJson, "alarmInterfaceA")); + vesAlarm.setEventCategory(getTextElementByNode(faultFieldsJson,"eventCategory")); + vesAlarm.setEventSeverity(faultFieldsJson.getString("eventSeverity")); + vesAlarm.setEventSourceType(faultFieldsJson.getString("eventSourceType")); + vesAlarm.setFaultFieldsVersion(faultFieldsJson.getLong("faultFieldsVersion")); + vesAlarm.setSpecificProblem(faultFieldsJson.getString("specificProblem")); + vesAlarm.setVfStatus(faultFieldsJson.getString("vfStatus")); + } + + private String getTextElementByNode(JSONObject jsonNode,String name){ + if(jsonNode.get(name) != null){ + return jsonNode.getString(name); + } + return null; + } + + private Long getLongElementByNode(JSONObject jsonNode, String name) { + if(jsonNode.get(name) != null){ + return jsonNode.getLong(name); + } + return null; + } + + private List<AlarmAdditionalField> getListElementByNode(JSONObject jsonNode, String name){ + List<AlarmAdditionalField> alarms = new ArrayList<AlarmAdditionalField>(); + if (jsonNode.get(name) != null) { + JSONArray alarmAdditionalInformations = jsonNode.getJSONArray(name); + for (int i = 0; i < alarmAdditionalInformations.size(); i++) { + JSONObject jsonObject = alarmAdditionalInformations.getJSONObject(i); + if (jsonObject.get("name") != null + && jsonObject.get("value") != null) { + AlarmAdditionalField field = new AlarmAdditionalField(); + field.setName(getTextElementByNode(jsonObject, "name")); + field.setValue(getTextElementByNode(jsonObject, "value")); + alarms.add(field); + } + } + } + return alarms; + } +} diff --git a/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java new file mode 100644 index 0000000..160fc38 --- /dev/null +++ b/engine-d/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java @@ -0,0 +1,116 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.dsa.dmaappolling; + +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.onap.holmes.common.api.stat.VesAlarm; +import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.GsonUtil; +import org.onap.holmes.common.utils.HttpsUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +@Getter +@Setter +@Slf4j +public class Subscriber { + + private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator() + .getService(DMaaPResponseUtil.class); + + /** + * The number of milliseconds to wait for messages if none are immediately available. This + * should normally be used, and set at 15000 or higher. + */ + private int timeout = 15000; + + /** + * The maximum number of messages to return + */ + private int limit = 100; + + /** + * The number of milliseconds to poll interval time. This should normally be used, and set at + * 15000 or higher. + */ + private int period = timeout; + + private boolean secure; + private String topic; + private String url; + private String uuid = UUID.randomUUID() + ""; + private String consumerGroup = "homlesGroup" + uuid; + private String consumer = "homles" + uuid; + private String authInfo; + private String authExpDate; + + public List<VesAlarm> subscribe() throws CorrelationException { + List<String> response; + try { + response = getDMaaPData(); + } catch (Exception e) { + throw new CorrelationException("Failed to get data from DMaaP.", e); + } + try { + return extractVesAlarm(response); + } catch (Exception e) { + throw new CorrelationException("Failed to convert the response data to VES alarms.", e); + } + } + + private List<String> getDMaaPData() throws Exception { + String response; + CloseableHttpClient closeableHttpClient = null; + HttpGet httpGet = new HttpGet(url + "/" + consumerGroup + "/" + consumer + "?timeout=" + period); + try { + closeableHttpClient = HttpsUtils.getHttpClient(timeout); + HttpResponse httpResponse = HttpsUtils + .get(httpGet, new HashMap<>(), closeableHttpClient); + response = HttpsUtils.extractResponseEntity(httpResponse); + } catch (Exception e) { + throw e; + } finally { + httpGet.releaseConnection(); + if (closeableHttpClient != null) { + try { + closeableHttpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } + } + return GsonUtil.jsonToBean(response, List.class); + } + + private List<VesAlarm> extractVesAlarm(List<String> responseEntity) throws IOException { + List<VesAlarm> vesAlarmList = new ArrayList<>(); + for (String entity : responseEntity) { + vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity)); + } + return vesAlarmList; + } +} |