e21903b7281d70cd74b2dc174b86663fdc4ddac2
[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  * Copyright (C) 2021 Samsung Electronics Intellectual Property. All rights reserved.
7  * =================================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the License
14  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15  * or implied. See the License for the specific language governing permissions and limitations under
16  * the License.
17  * ============LICENSE_END==========================================================================
18  */
19
20 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
21
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.IOException;
25 import java.time.Instant;
26 import java.time.ZoneId;
27 import java.util.Map;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class DMaaPFaultVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
33
34     private static final Logger LOG = LoggerFactory.getLogger(DMaaPFaultVESMsgConsumer.class);
35
36     private static final String DEFAULT_SDNRUSER = "admin";
37     private static final String DEFAULT_SDNRPASSWD = "admin";
38
39     private final GeneralConfig generalConfig;
40
41     public DMaaPFaultVESMsgConsumer(GeneralConfig generalConfig) {
42         this.generalConfig = generalConfig;
43     }
44
45     @Override
46     public void processMsg(String msg) throws Exception {
47         String faultNodeId;
48         String faultOccurrenceTime;
49         String faultObjectId;
50         String faultReason;
51         String faultSeverity;
52         String vesDomain;
53         int faultSequence;
54         String reportingEntityName;
55         ObjectMapper oMapper = new ObjectMapper();
56         JsonNode dmaapMessageRootNode;
57
58         LOG.info("Fault VES Message is - {}", msg);
59         try {
60             dmaapMessageRootNode = oMapper.readTree(msg);
61             reportingEntityName = dmaapMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
62             if (reportingEntityName.equals("ONAP SDN-R")) {
63                 LOG.info(
64                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
65                 return;
66             }
67
68             vesDomain = dmaapMessageRootNode.at("/event/commonEventHeader/domain").textValue();
69             if (!vesDomain.equalsIgnoreCase("fault")) {
70                 LOG.warn("Received {} domain VES Message in DMaaP Fault topic, ignoring it", vesDomain);
71                 return;
72             }
73             faultNodeId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
74             faultOccurrenceTime = Instant
75                     .ofEpochMilli(
76                             dmaapMessageRootNode.at("/event/commonEventHeader/startEpochMicrosec").longValue() / 1000)
77                     .atZone(ZoneId.of("Z")).toString();
78             faultObjectId = dmaapMessageRootNode.at("/event/faultFields/alarmInterfaceA").textValue();
79             faultReason = dmaapMessageRootNode.at("/event/faultFields/specificProblem").textValue();
80             faultSeverity = dmaapMessageRootNode.at("/event/faultFields/eventSeverity").textValue();
81             faultSequence = dmaapMessageRootNode.at("/event/commonEventHeader/sequence").intValue();
82
83             if (faultSeverity.equalsIgnoreCase("critical")) {
84                 faultSeverity = SeverityType.Critical.toString();
85             } else if (faultSeverity.equalsIgnoreCase("major")) {
86                 faultSeverity = SeverityType.Major.toString();
87             } else if (faultSeverity.equalsIgnoreCase("minor")) {
88                 faultSeverity = SeverityType.Minor.toString();
89             } else if (faultSeverity.equalsIgnoreCase("warning")) {
90                 faultSeverity = SeverityType.Warning.toString();
91             } else if (faultSeverity.equalsIgnoreCase("nonalarmed")) {
92                 faultSeverity = SeverityType.NonAlarmed.toString();
93             } else {
94                 faultSeverity = SeverityType.NonAlarmed.toString();
95             }
96
97             String baseUrl = getBaseUrl();
98             String sdnrUser = getSDNRUser();
99             String sdnrPasswd = getSDNRPasswd();
100
101             Map<String, String> payloadMapMessage = FaultNotificationClient.createFaultNotificationPayloadMap(faultNodeId,
102                     Integer.toString(faultSequence), faultOccurrenceTime, faultObjectId, faultReason, faultSeverity);
103
104             FaultNotificationClient faultClient = new FaultNotificationClient(baseUrl);
105             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
106             faultClient.setAuthorization(sdnrUser, sdnrPasswd);
107             String message = faultClient.prepareMessageFromPayloadMap(payloadMapMessage);
108             faultClient.sendNotification(message);
109
110         } catch (IOException e) {
111             LOG.info("Cannot parse json object ");
112             throw new Exception("Cannot parse json object", e);
113         }
114     }
115
116     public String getBaseUrl() {
117         return generalConfig.getBaseUrl();
118     }
119
120     public String getSDNRUser() {
121         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
122     }
123
124     public String getSDNRPasswd() {
125         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
126     }
127
128     public FaultNotificationClient getFaultNotificationClient(String baseUrl) {
129         return new FaultNotificationClient(baseUrl);
130     }
131 }