ca0f52ac7b9077b4b48e513262b88ba35508aeb4
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / LCM / operation / GenericProviderOperationRequestFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.listener.LCM.operation;
27
28 import com.fasterxml.jackson.databind.JsonNode;
29 import org.json.JSONObject;
30 import org.onap.appc.exceptions.APPCException;
31 import org.onap.appc.listener.LCM.model.InputBody;
32 import org.onap.appc.listener.LCM.model.ResponseStatus;
33 import org.onap.appc.listener.util.Mapper;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 import java.net.URL;
39
40 public class GenericProviderOperationRequestFormatter implements ProviderOperationRequestFormatter {
41
42     private final EELFLogger LOG = EELFManager.getInstance().getLogger(GenericProviderOperationRequestFormatter.class);
43
44     //@formatter:off
45     @SuppressWarnings("nls")
46     private final static String TEMPLATE = "{\"input\": %s}";
47     //@formatter:on
48
49     @Override
50     public String buildPath(URL url, String rpcName) {
51         return url.getPath() + ":" + rpcName;
52     }
53
54     @Override
55     public String buildRequest(InputBody msg) {
56         JSONObject jsonObject = Mapper.toJsonObject(msg);
57         return String.format(TEMPLATE, jsonObject.toString());
58     }
59
60     @Override
61     public ResponseStatus getResponseStatus(JsonNode responseBody)throws APPCException{
62         try {
63             JsonNode status = responseBody.get("output").get("status");
64             return new ResponseStatus(status.get("code").asInt(), status.get("message").asText());
65         } catch (Exception e) {
66             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
67             throw new APPCException("APPC has an unknown RPC error");
68         }
69     }
70
71     @Override
72     public String getLocked(JSONObject responseBody) throws APPCException {
73         try {
74             JSONObject outputObject=responseBody.getJSONObject("output");
75             if(outputObject.has("locked")){
76                 return outputObject.getString("locked");
77             }else{
78                 return null;
79             }
80         } catch (Exception e) {
81             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
82             throw new APPCException("APPC has an unknown RPC error");
83         }
84     }
85 }