6daeb493d5d0e4234bf7f0eb693e95a1b1bdf22c
[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         String reportingEntityName;
53         ObjectMapper oMapper = new ObjectMapper();
54         JsonNode dmaapMessageRootNode;
55
56         LOG.info("Fault VES Message is - {}", msg);
57         try {
58             dmaapMessageRootNode = oMapper.readTree(msg);
59             reportingEntityName = dmaapMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
60             if (reportingEntityName.equals("ONAP SDN-R")) {
61                 LOG.info(
62                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
63                 return;
64             }
65
66             vesDomain = dmaapMessageRootNode.at("/event/commonEventHeader/domain").textValue();
67             if (!vesDomain.equalsIgnoreCase("fault")) {
68                 LOG.warn("Received {} domain VES Message in DMaaP Fault topic, ignoring it", vesDomain);
69                 return;
70             }
71             faultNodeId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
72             faultOccurrenceTime = Instant
73                     .ofEpochMilli(
74                             dmaapMessageRootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue() / 1000)
75                     .atZone(ZoneId.of("Z")).toString();
76             faultObjectId = dmaapMessageRootNode.at("/event/faultFields/alarmInterfaceA").textValue();
77             faultReason = dmaapMessageRootNode.at("/event/faultFields/specificProblem").textValue();
78             faultSeverity = dmaapMessageRootNode.at("/event/faultFields/eventSeverity").textValue();
79             faultSequence = dmaapMessageRootNode.at("/event/commonEventHeader/sequence").intValue();
80
81             if (faultSeverity.equalsIgnoreCase("critical")) {
82                 faultSeverity = SeverityType.Critical.toString();
83             } else if (faultSeverity.equalsIgnoreCase("major")) {
84                 faultSeverity = SeverityType.Major.toString();
85             } else if (faultSeverity.equalsIgnoreCase("minor")) {
86                 faultSeverity = SeverityType.Minor.toString();
87             } else if (faultSeverity.equalsIgnoreCase("warning")) {
88                 faultSeverity = SeverityType.Warning.toString();
89             } else if (faultSeverity.equalsIgnoreCase("nonalarmed")) {
90                 faultSeverity = SeverityType.NonAlarmed.toString();
91             } else {
92                 faultSeverity = SeverityType.NonAlarmed.toString();
93             }
94
95             String baseUrl = getBaseUrl();
96             String sdnrUser = getSDNRUser();
97             String sdnrPasswd = getSDNRPasswd();
98
99             FaultNotificationClient faultClient = getFaultNotificationClient(baseUrl);
100             faultClient.setAuthorization(sdnrUser, sdnrPasswd);
101             faultClient.sendFaultNotification(faultNodeId, Integer.toString(faultSequence), faultOccurrenceTime,
102                     faultObjectId, faultReason, faultSeverity);
103
104         } catch (IOException e) {
105             LOG.info("Cannot parse json object ");
106             throw new Exception("Cannot parse json object", e);
107         }
108     }
109
110     public String getBaseUrl() {
111         return generalConfig.getBaseUrl();
112     }
113
114     public String getSDNRUser() {
115         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
116     }
117
118     public String getSDNRPasswd() {
119         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
120     }
121
122     public FaultNotificationClient getFaultNotificationClient(String baseUrl) {
123         return new FaultNotificationClient(baseUrl);
124     }
125 }