3fdbb6f20a2e20c29a510ac0e3ff7c104c09b496
[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 java.io.IOException;
22 import java.util.Base64;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class FaultNotificationClient extends BaseHTTPClient {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FaultNotificationClient.class);
33     private static final String FAULT_NOTIFICATION_URI = "restconf/operations/devicemanager:push-fault-notification";
34     private final Map<String, String> headerMap;
35
36     // @formatter:off
37     private static final String FAULT_PAYLOAD = "{\n"
38             + "  \"devicemanager:input\": {\n"
39             + "    \"devicemanager:node-id\": \"@node-id@\",\n"
40             + "    \"devicemanager:counter\": \"@counter@\",\n"
41             + "    \"devicemanager:timestamp\": \"@timestamp@\",\n"
42             + "    \"devicemanager:object-id\": \"@object-id@\",\n"
43             + "    \"devicemanager:problem\": \"@problem@\",\n"
44             + "    \"devicemanager:severity\": \"@severity@\"\n"
45             + "  }\n"
46             + "}";
47     // @formatter:on
48
49
50     public FaultNotificationClient(String baseUrl) {
51         super(baseUrl);
52
53         this.headerMap = new HashMap<>();
54         this.headerMap.put("Content-Type", "application/json");
55         this.headerMap.put("Accept", "application/json");
56     }
57
58     public void setAuthorization(String username, String password) {
59         String credentials = username + ":" + password;
60         this.headerMap.put("Authorization", "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
61
62     }
63
64     public boolean sendFaultNotification(String faultNodeId, String faultCounter, String faultOccurrenceTime,
65             String faultObjectId, String faultReason, String faultSeverity) {
66         String message = "";
67
68         message = updateFaultPayload(faultNodeId, faultCounter, faultOccurrenceTime, faultObjectId, faultReason,
69                 faultSeverity);
70
71         LOG.debug("Payload after updating values is: " + message);
72
73         return sendFaultRequest("POST", message) == 200;
74
75     }
76
77     private static String updateFaultPayload(String faultNodeId, String faultCounter, String faultOccurrenceTime,
78             String faultObjectId, String faultReason, String faultSeverity) {
79         // @formatter:off
80         return FAULT_PAYLOAD.replace("@node-id@", faultNodeId)
81                 .replace("@counter@", faultCounter)
82                 .replace("@timestamp@", faultOccurrenceTime)
83                 .replace("@object-id@", faultObjectId)
84                 .replace("@problem@", faultReason)
85                 .replace("@severity@", faultSeverity);
86         // @formatter:on
87     }
88
89
90     private int sendFaultRequest(String method, String message) {
91         LOG.debug("In sendFaultRequest - " + method + " " + message);
92         BaseHTTPResponse response;
93         try {
94             String uri = FAULT_NOTIFICATION_URI;
95             response = this.sendRequest(uri, method, message, headerMap);
96             LOG.debug("finished with responsecode {}", response.code);
97             return response.code;
98         } catch (IOException e) {
99             LOG.warn("problem sending fault message {} : {}", e.getMessage());
100             return -1;
101         }
102     }
103 }