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