76137674a95fbbba60f80e23c658af2cba1c4f3e
[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
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;
30
31 public class FaultNotificationClient extends BaseHTTPClient {
32
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;
36
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
48         public FaultNotificationClient(String baseUrl) {
49                 super(baseUrl);
50
51                 this.headerMap = new HashMap<>();
52                 this.headerMap.put("Content-Type", "application/json");
53                 this.headerMap.put("Accept", "application/json");
54         }
55
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())));
60
61         }
62
63         public boolean sendFaultNotification(String faultNodeId, String faultCounter, String faultOccurrenceTime, String faultObjectId,
64                         String faultReason, String faultSeverity) {
65                 String message = "";
66
67                 message = updateFaultPayload(faultNodeId, faultCounter, faultOccurrenceTime, faultObjectId, faultReason, faultSeverity);
68
69                 LOG.debug("Payload after updating values is: "+message);
70
71                 return sendFaultRequest("POST", message) == 200;
72
73         }
74
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);
83         }
84
85
86         private int sendFaultRequest(String method, String message) {
87                 LOG.debug("In sendFaultRequest - "+method+ " "+message);
88                 BaseHTTPResponse response;
89                 try {
90                         String uri = FAULT_NOTIFICATION_URI;
91                         response = this.sendRequest(uri, method, message, headerMap);
92                         LOG.debug("finished with responsecode {}", response.code);
93                         return response.code;
94                 } catch (IOException e) {
95                         LOG.warn("problem sending fault message {} : {}", e.getMessage());
96                         return -1;
97                 }
98         }
99 }