ec51980123f32ddacfc3a32ca37e5e20abf6f3e6
[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
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
20
21 import com.fasterxml.jackson.databind.JsonNode;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.IOException;
24 import java.time.Instant;
25 import java.time.ZoneId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class DMaaPFaultVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
31
32     private static final Logger LOG = LoggerFactory.getLogger(DMaaPFaultVESMsgConsumer.class);
33
34     private static final String DEFAULT_SDNRUSER = "admin";
35     private static final String DEFAULT_SDNRPASSWD = "admin";
36
37     private final GeneralConfig generalConfig;
38
39     public DMaaPFaultVESMsgConsumer(GeneralConfig generalConfig) {
40         this.generalConfig = generalConfig;
41     }
42
43     @Override
44     public void processMsg(String msg) throws Exception {
45         String faultNodeId;
46         String faultOccurrenceTime;
47         String faultObjectId;
48         String faultReason;
49         String faultSeverity;
50         String vesDomain;
51         int faultSequence;
52         ObjectMapper oMapper = new ObjectMapper();
53         JsonNode dmaapMessageRootNode;
54
55         LOG.info("Fault VES Message is - {}", msg);
56         try {
57             dmaapMessageRootNode = oMapper.readTree(msg);
58             vesDomain = dmaapMessageRootNode.at("/event/commonEventHeader/domain").textValue();
59             if (!vesDomain.equalsIgnoreCase("fault")) {
60                 LOG.warn("Received {} domain VES Message in DMaaP Fault topic, ignoring it", vesDomain);
61                 return;
62             }
63             faultNodeId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
64             faultOccurrenceTime = Instant.ofEpochMilli(dmaapMessageRootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue()/1000)
65                     .atZone(ZoneId.of("Z")).toString();
66             faultObjectId = dmaapMessageRootNode.at("/event/faultFields/alarmInterfaceA").textValue();
67             faultReason = dmaapMessageRootNode.at("/event/faultFields/specificProblem").textValue();
68             faultSeverity = dmaapMessageRootNode.at("/event/faultFields/eventSeverity").textValue();
69             faultSequence = dmaapMessageRootNode.at("/event/commonEventHeader/sequence").intValue();
70
71             if (faultSeverity.equalsIgnoreCase("critical")) {
72                 faultSeverity = SeverityType.Critical.toString();
73             } else if (faultSeverity.equalsIgnoreCase("major")) {
74                 faultSeverity = SeverityType.Major.toString();
75             } else if (faultSeverity.equalsIgnoreCase("minor")) {
76                 faultSeverity = SeverityType.Minor.toString();
77             } else if (faultSeverity.equalsIgnoreCase("warning")) {
78                 faultSeverity = SeverityType.Warning.toString();
79             } else if (faultSeverity.equalsIgnoreCase("nonalarmed")) {
80                 faultSeverity = SeverityType.NonAlarmed.toString();
81             } else {
82                 faultSeverity = SeverityType.NonAlarmed.toString();
83             }
84
85             String baseUrl = getBaseUrl();
86             String sdnrUser = getSDNRUser();
87             String sdnrPasswd = getSDNRPasswd();
88
89             FaultNotificationClient faultClient = getFaultNotificationClient(baseUrl);
90             faultClient.setAuthorization(sdnrUser, sdnrPasswd);
91             faultClient.sendFaultNotification(faultNodeId, Integer.toString(faultSequence), faultOccurrenceTime,
92                     faultObjectId, faultReason, faultSeverity);
93
94         } catch (IOException e) {
95             LOG.info("Cannot parse json object ");
96             throw new Exception("Cannot parse json object", e);
97         }
98     }
99
100     public String getBaseUrl() {
101         return generalConfig.getBaseUrl();
102     }
103
104     public String getSDNRUser() {
105         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
106     }
107
108     public String getSDNRPasswd() {
109         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
110     }
111
112     public FaultNotificationClient getFaultNotificationClient(String baseUrl) {
113         return new FaultNotificationClient(baseUrl);
114     }
115 }