2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.provider.lcm.service;
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;
36 import java.text.ParseException;
38 public class AbstractBaseUtils {
39 protected final String COMMON_ERROR_MESSAGE_TEMPLATE = "Error processing %s input : %s";
40 protected final String DELIMITER_COMMA = ",";
42 protected final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractBaseService.class);
45 * Build a Status without parameter
47 * @param lcmCommandStatus for the Status code and message format
48 * @return the newly built Status
50 protected Status buildStatusWithoutParams(LCMCommandStatus lcmCommandStatus) {
51 return buildStatus(lcmCommandStatus, null, null);
55 * Build a Status with "errorMsg" param key.
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
61 protected Status buildStatusForErrorMsg(LCMCommandStatus lcmCommandStatus, String message) {
62 return buildStatus(lcmCommandStatus, message, "errorMsg");
66 * Build a Status with "vnfId" param key.
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
72 protected Status buildStatusForVnfId(LCMCommandStatus lcmCommandStatus, String message) {
73 return buildStatus(lcmCommandStatus, message, "vnfId");
77 * Build a Status with "paramName" param key.
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
83 protected Status buildStatusForParamName(LCMCommandStatus lcmCommandStatus, String message) {
84 return buildStatus(lcmCommandStatus, message, "paramName");
88 * Build a Status with "id" param key.
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
94 protected Status buildStatusForId(LCMCommandStatus lcmCommandStatus, String message) {
95 return buildStatus(lcmCommandStatus, message, "id");
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
106 Status buildStatus(LCMCommandStatus lcmCommandStatus, String message, String key) {
107 Params params = new Params().addParam(key, message);
108 String statusMsg = lcmCommandStatus.getFormattedMessage(params);
110 return buildStatusWithCode(lcmCommandStatus.getResponseCode(), statusMsg);
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
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();
127 * Build a Status using ParseException
128 * @param e of the ParseException
129 * @return the newly built Status
131 protected Status buildStatusWithParseException(ParseException e) {
132 String errorMessage = e.getMessage() != null ? e.getMessage() : e.toString();
133 return buildStatusForErrorMsg(LCMCommandStatus.REQUEST_PARSING_FAILED, errorMessage);
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
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);
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
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();