Update license header in appc-event-listener files
[appc.git] / appc-event-listener / 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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.LCM.operation;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import org.json.JSONObject;
28 import org.onap.appc.exceptions.APPCException;
29 import org.onap.appc.listener.LCM.model.InputBody;
30 import org.onap.appc.listener.LCM.model.ResponseStatus;
31 import org.onap.appc.listener.util.Mapper;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 import java.net.URL;
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     @Override
53     public String buildRequest(InputBody msg) {
54         JSONObject jsonObject = Mapper.toJsonObject(msg);
55         return String.format(TEMPLATE, jsonObject.toString());
56     }
57
58     @Override
59     public ResponseStatus getResponseStatus(JsonNode responseBody)throws APPCException{
60         try {
61             JsonNode status = responseBody.get("output").get("status");
62             return new ResponseStatus(status.get("code").asInt(), status.get("message").asText());
63         } catch (Exception e) {
64             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
65             throw new APPCException("APPC has an unknown RPC error");
66         }
67     }
68
69     @Override
70     public String getLocked(JSONObject responseBody) throws APPCException {
71         try {
72             JSONObject outputObject=responseBody.getJSONObject("output");
73             if(outputObject.has("locked")){
74                 return outputObject.getString("locked");
75             }else{
76                 return null;
77             }
78         } catch (Exception e) {
79             LOG.error("Unknown error processing failed response from provider. Json not in expected format", e);
80             throw new APPCException("APPC has an unknown RPC error");
81         }
82     }
83 }