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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
21 import java.io.IOException;
22 import java.util.Base64;
23 import java.util.HashMap;
26 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
27 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class FaultNotificationClient extends BaseHTTPClient {
33 private static final Logger LOG = LoggerFactory.getLogger(FaultNotificationClient.class);
34 private static final String FAULT_NOTIFICATION_URI = "restconf/operations/devicemanager:push-fault-notification";
35 private final Map<String, String> headerMap;
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" +
48 public FaultNotificationClient(String baseUrl) {
51 this.headerMap = new HashMap<>();
52 this.headerMap.put("Content-Type", "application/json");
53 this.headerMap.put("Accept", "application/json");
56 public void setAuthorization(String username, String password) {
57 String credentials = username + ":" + password;
58 this.headerMap.put("Authorization",
59 "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
63 public boolean sendFaultNotification(String faultNodeId, String faultCounter, String faultOccurrenceTime, String faultObjectId,
64 String faultReason, String faultSeverity) {
67 message = updateFaultPayload(faultNodeId, faultCounter, faultOccurrenceTime, faultObjectId, faultReason, faultSeverity);
69 LOG.debug("Payload after updating values is: "+message);
71 return sendFaultRequest("POST", message) == 200;
75 private static String updateFaultPayload(String faultNodeId, String faultCounter, String faultOccurrenceTime, String faultObjectId,
76 String faultReason, String faultSeverity) {
77 return FAULT_PAYLOAD.replace("@node-id@", faultNodeId)
78 .replace("@counter@", faultCounter)
79 .replace("@timestamp@", faultOccurrenceTime)
80 .replace("@object-id@", faultObjectId)
81 .replace("@problem@", faultReason)
82 .replace("@severity@", faultSeverity);
86 private int sendFaultRequest(String method, String message) {
87 LOG.debug("In sendFaultRequest - "+method+ " "+message);
88 BaseHTTPResponse response;
90 String uri = FAULT_NOTIFICATION_URI;
91 response = this.sendRequest(uri, method, message, headerMap);
92 LOG.debug("finished with responsecode {}", response.code);
94 } catch (IOException e) {
95 LOG.warn("problem sending fault message {} : {}", e.getMessage());