2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.service;
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;
35 import java.text.ParseException;
37 public class AbstractBaseUtils {
38 protected final String COMMON_ERROR_MESSAGE_TEMPLATE = "Error processing %s input : %s";
39 protected final String DELIMITER_COMMA = ",";
41 protected final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractBaseService.class);
44 * Build a Status without parameter
46 * @param lcmCommandStatus for the Status code and message format
47 * @return the newly built Status
49 protected Status buildStatusWithoutParams(LCMCommandStatus lcmCommandStatus) {
50 return buildStatus(lcmCommandStatus, null, null);
54 * Build a Status with "errorMsg" param key.
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
60 protected Status buildStatusForErrorMsg(LCMCommandStatus lcmCommandStatus, String message) {
61 return buildStatus(lcmCommandStatus, message, "errorMsg");
65 * Build a Status with "vnfId" param key.
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
71 protected Status buildStatusForVnfId(LCMCommandStatus lcmCommandStatus, String message) {
72 return buildStatus(lcmCommandStatus, message, "vnfId");
76 * Build a Status with "paramName" param key.
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
82 protected Status buildStatusForParamName(LCMCommandStatus lcmCommandStatus, String message) {
83 return buildStatus(lcmCommandStatus, message, "paramName");
87 * Build a Status with "id" param key.
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
93 protected Status buildStatusForId(LCMCommandStatus lcmCommandStatus, String message) {
94 return buildStatus(lcmCommandStatus, message, "id");
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
105 Status buildStatus(LCMCommandStatus lcmCommandStatus, String message, String key) {
106 Params params = new Params().addParam(key, message);
107 String statusMsg = lcmCommandStatus.getFormattedMessage(params);
109 return buildStatusWithCode(lcmCommandStatus.getResponseCode(), statusMsg);
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
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();
126 * Build a Status using ParseException
127 * @param e of the ParseException
128 * @return the newly built Status
130 protected Status buildStatusWithParseException(ParseException e) {
131 String errorMessage = e.getMessage() != null ? e.getMessage() : e.toString();
132 return buildStatusForErrorMsg(LCMCommandStatus.REQUEST_PARSING_FAILED, errorMessage);
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
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);
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
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();