Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / AbstractBaseUtils.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.onap.appc.provider.lcm.service;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.StatusBuilder;
32 import org.onap.appc.executor.objects.LCMCommandStatus;
33 import org.onap.appc.executor.objects.Params;
34 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
35
36 import java.text.ParseException;
37
38 public class AbstractBaseUtils {
39     protected final String COMMON_ERROR_MESSAGE_TEMPLATE =  "Error processing %s input : %s";
40     protected final String DELIMITER_COMMA = ",";
41
42         protected final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractBaseService.class);
43
44     /**
45      * Build a Status without parameter
46      *
47      * @param lcmCommandStatus for the Status code and message format
48      * @return the newly built Status
49      */
50     protected Status buildStatusWithoutParams(LCMCommandStatus lcmCommandStatus) {
51         return buildStatus(lcmCommandStatus, null, null);
52     }
53
54     /**
55      * Build a Status with "errorMsg" param key.
56      *
57      * @param lcmCommandStatus for the Status code and message format
58      * @param message          String for the Status message variable
59      * @return the newly built Status
60      */
61     protected Status buildStatusForErrorMsg(LCMCommandStatus lcmCommandStatus, String message) {
62         return buildStatus(lcmCommandStatus, message, "errorMsg");
63     }
64
65     /**
66      * Build a Status with "vnfId" param key.
67      *
68      * @param lcmCommandStatus for the Status code and message format
69      * @param message          String for the Status message variable
70      * @return the newly build Status
71      */
72     protected Status buildStatusForVnfId(LCMCommandStatus lcmCommandStatus, String message) {
73         return buildStatus(lcmCommandStatus, message, "vnfId");
74     }
75
76     /**
77      * Build a Status with "paramName" param key.
78      *
79      * @param lcmCommandStatus for the Status code and message format
80      * @param message          String for the Status message variable
81      * @return the newly built Status
82      */
83     protected Status buildStatusForParamName(LCMCommandStatus lcmCommandStatus, String message) {
84         return buildStatus(lcmCommandStatus, message, "paramName");
85     }
86
87     /**
88      * Build a Status with "id" param key.
89      *
90      * @param lcmCommandStatus for the Status code and message format
91      * @param message          String for the Status message variable
92      * @return the newly build Status
93      */
94     protected Status buildStatusForId(LCMCommandStatus lcmCommandStatus, String message) {
95         return buildStatus(lcmCommandStatus, message, "id");
96     }
97
98     /**
99      * Build a Status.
100      *
101      * @param lcmCommandStatus for the Status code and message format
102      * @param message          String for the Status message variable
103      * @param key              String for the LCMcommandStatus format
104      * @return the newly built Status
105      */
106     Status buildStatus(LCMCommandStatus lcmCommandStatus, String message, String key) {
107         Params params = new Params().addParam(key, message);
108         String statusMsg = lcmCommandStatus.getFormattedMessage(params);
109
110         return buildStatusWithCode(lcmCommandStatus.getResponseCode(), statusMsg);
111     }
112
113     /**
114      * Build a Status with passed in code
115      * @param statusCode Integer of the status code
116      * @param statusMsg String of the status description
117      * @return the newly build Status
118      */
119     private Status buildStatusWithCode(Integer statusCode, String statusMsg) {
120         StatusBuilder status = new StatusBuilder();
121         status.setCode(statusCode);
122         status.setMessage(statusMsg);
123         return status.build();
124     }
125
126     /**
127      * Build a Status using ParseException
128      * @param e of the ParseException
129      * @return the newly built Status
130      */
131     protected Status buildStatusWithParseException(ParseException e) {
132         String errorMessage = e.getMessage() != null ? e.getMessage() : e.toString();
133         return buildStatusForErrorMsg(LCMCommandStatus.REQUEST_PARSING_FAILED, errorMessage);
134     }
135
136     /**
137      * Build a Status using RequestHandlerOutput
138      * @param requestHandlerOutput object which contains the status code and message for building the new status
139      * @return the newly built Status
140      */
141     protected Status buildStatusWithDispatcherOutput(RequestHandlerOutput requestHandlerOutput){
142         Integer statusCode = requestHandlerOutput.getResponseContext().getStatus().getCode();
143         String statusMessage = requestHandlerOutput.getResponseContext().getStatus().getMessage();
144         return  buildStatusWithCode(statusCode, statusMessage);
145     }
146
147     /**
148      * Get RPC name from Action. When there 2 words in the Action, RPC name will be dash separated string.
149      * @param action of Action object
150      * @return RPC name of the Action
151      */
152     protected String getRpcName(Action action) {
153         String regex = "([a-z])([A-Z]+)";
154         String replacement = "$1-$2";
155         return action.name().replaceAll(regex, replacement).toLowerCase();
156     }
157 }