dc8b5eab7203a95e8967b6b49dcc61fefa3fcaab
[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 public class GenericProviderOperationRequestFormatter implements ProviderOperationRequestFormatter {
40
41     private final EELFLogger LOG = EELFManager.getInstance().getLogger(GenericProviderOperationRequestFormatter.class);
42
43     //@formatter:off
44     @SuppressWarnings("nls")
45     private final static String TEMPLATE = "{\"input\": %s}";
46     //@formatter:on
47
48     @Override
49     public String buildPath(URL url, String rpcName) {
50         return url.getPath() + ":"+rpcName;
51     }
52
53     @Override
54     public String buildRequest(InputBody msg) {
55         JSONObject jsonObject = Mapper.toJsonObject(msg);
56         return String.format(TEMPLATE, jsonObject.toString());
57     }
58
59     @Override
60     public ResponseStatus getResponseStatus(JsonNode responseBody)throws APPCException{
61         try {
62             JsonNode status = responseBody.get("output").get("status");
63             return new ResponseStatus(status.get("code").asInt(), status.get("message").asText());
64         } catch (Exception e) {
65             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
66             throw new APPCException("APPC has an unknown RPC error");
67         }
68     }
69
70     @Override
71     public String getLocked(JSONObject responseBody) throws APPCException {
72         try {
73             JSONObject outputObject=responseBody.getJSONObject("output");
74             if(outputObject.has("locked")){
75                 return outputObject.getString("locked");
76             }else{
77                 return null;
78             }
79         } catch (Exception e) {
80             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
81             throw new APPCException("APPC has an unknown RPC error");
82         }
83     }
84 }