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