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