Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / LCM / operation / GenericProviderOperationRequestFormatter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.listener.LCM.operation;
23
24 import com.fasterxml.jackson.databind.JsonNode;
25 import org.json.JSONObject;
26 import org.openecomp.appc.exceptions.APPCException;
27 import org.openecomp.appc.listener.LCM.model.InputBody;
28 import org.openecomp.appc.listener.LCM.model.ResponseStatus;
29 import org.openecomp.appc.listener.util.Mapper;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 import java.net.URL;
35
36
37
38 public class GenericProviderOperationRequestFormatter implements ProviderOperationRequestFormatter {
39
40     private final EELFLogger LOG = EELFManager.getInstance().getLogger(GenericProviderOperationRequestFormatter.class);
41
42     //@formatter:off
43     @SuppressWarnings("nls")
44     private final static String TEMPLATE = "{\"input\": %s}";
45     //@formatter:on
46
47     @Override
48     public String buildPath(URL url, String rpcName) {
49         return url.getPath() + ":"+rpcName;
50     }
51
52 //    private String convertActionToUrl(String action) {
53 //        String res = "";
54 //        switch (action) {
55 //            case "SoftwareUpload":
56 //                res = "software-upload";
57 //                break;
58 //            case "LiveUpgrade":
59 //                res = "live-upgrade";
60 //                break;
61 //            case "HealthCheck":
62 //                res = "health-check";
63 //                break;
64 //            case "CheckLock":
65 //                res = "check-lock";
66 //                break;
67 //            default :
68 //                res = action;
69 //        }
70 //        return res.toLowerCase();
71 //    }
72
73     @Override
74     public String buildRequest(InputBody msg) {
75         JSONObject jsonObject = Mapper.toJsonObject(msg);
76         return String.format(TEMPLATE, jsonObject.toString());
77     }
78
79 /*    @Override
80     public OperationStatus getOperationStatus(JSONObject responseBody) throws APPCException {
81         try {
82             JSONObject status = responseBody.getJSONObject("output").getJSONObject("status");
83             return new OperationStatus(String.valueOf(status.getInt("code")), status.getString("message"));
84         } catch (Exception e) {
85             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
86             throw new APPCException("APPC has an unknown RPC error");
87         }
88     }*/
89
90     @Override
91     public ResponseStatus getResponseStatus(JsonNode responseBody)throws APPCException{
92         try {
93             JsonNode status = responseBody.get("output").get("status");
94             return new ResponseStatus(status.get("code").asInt(), status.get("message").asText());
95         } catch (Exception e) {
96             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
97             throw new APPCException("APPC has an unknown RPC error");
98         }
99     }
100
101     @Override
102     public String getLocked(JSONObject responseBody) throws APPCException {
103         try {
104             JSONObject outputObject=responseBody.getJSONObject("output");
105             if(outputObject.has("locked")){
106                 return outputObject.getString("locked");
107             }else{
108                 return null;
109             }
110         } catch (Exception e) {
111             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
112             throw new APPCException("APPC has an unknown RPC error");
113         }
114     }
115 }