Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / service / AbstractBaseService.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 org.apache.commons.lang.StringUtils;
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.Payload;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
33 import org.onap.appc.executor.objects.LCMCommandStatus;
34 import org.onap.appc.logging.LoggingConstants;
35 import org.onap.appc.logging.LoggingUtils;
36 import org.onap.appc.provider.lcm.util.RequestInputBuilder;
37 import org.onap.appc.provider.lcm.util.ValidationService;
38 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
39 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
40
41 import java.text.ParseException;
42 import java.util.EnumSet;
43
44 public abstract class AbstractBaseService extends AbstractBaseUtils {
45     /**
46      * The list of ActionIdentifier keys.<br>
47      * The extra space in the front of the keyName is for better REST API response output.
48      */
49     enum ACTID_KEYS {
50         SERVICE_INSTANCE_ID(" service-instance-id"),
51         VF_MODULE_ID(" vf-module-id"),
52         VNF_ID(" vnf-id"),
53         VNFC_NAME(" vnfc-name"),
54         VSERVER_ID(" vserver-id");
55
56         String keyName;
57
58         ACTID_KEYS(String keyName) {
59             this.keyName = keyName;
60         }
61
62         String getKeyName() {
63             return keyName;
64         }
65     }
66
67       String rpcName;
68       Action expectedAction;
69
70     Status status;
71
72     protected AbstractBaseService(){};
73     /**
74      * Constructor
75      *
76      * @param theAction of the expected Action for this service
77      */
78     protected AbstractBaseService(Action theAction) {
79         expectedAction = theAction;
80         rpcName = getRpcName(theAction);
81     }
82
83
84     /**
85      * Validate Input: <br>
86      *    - using ValidationService to do common validation <br>
87      *    - validate Action matches the expected Action <br>
88      *    - validate existence of ActionIdentifier <br>
89      *
90      * @param commonHeader      of the input
91      * @param action            of the input
92      * @param actionIdentifiers of the input
93      * @return null if validation passed, otherwise, return Status with validation failure details.
94      */
95     Status validateInput(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
96         // common validation
97         Status validatedStatus = ValidationService.getInstance().validateInput(commonHeader, action, rpcName);
98         if (validatedStatus != null) {
99             return validatedStatus;
100         }
101
102         // action validation
103         if (action != expectedAction) {
104             validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, "action");
105             return validatedStatus;
106         }
107
108         // action identifier
109         if (actionIdentifiers == null) {
110             validatedStatus = buildStatusForParamName(
111                 LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "action-identifiers");
112         }
113
114         return validatedStatus;
115     }
116
117     /**
118      * Validate input as well as VNF ID in actionIdentifier
119      *
120      * @param commonHeader      of the input
121      * @param action            of the input
122      * @param actionIdentifiers of the input
123      * @return null if validation passed, otherwise, return Status with validation failure details.
124      */
125     Status validateVnfId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
126         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
127         if (validatedStatus != null) {
128             return validatedStatus;
129         }
130
131         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), "vnf-id");
132         if (validatedStatus == null) {
133             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
134         }
135
136         return validatedStatus;
137     }
138
139     /**
140      * Validate input as well as VSERVER ID in actionIdentifier
141      *
142      * @param commonHeader      of the input
143      * @param action            of the input
144      * @param actionIdentifiers of the input
145      * @return null if validation passed, otherwise, return Status with validation failure details.
146      */
147     Status validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
148         Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
149         if (validatedStatus != null) {
150             return validatedStatus;
151         }
152
153         validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), "vserver-id");
154         if (validatedStatus == null) {
155             validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
156         }
157
158         return validatedStatus;
159     }
160
161     /**
162      * Validate a value of the must have parameter
163      * @param value   the value of the parameter
164      * @param keyName the key name of the parameter
165      * @return null if validation passed, otherwise, return Status with validation failure details.
166      */
167     Status validateMustHaveParamValue(String value, String keyName) {
168         Status validatedStatus = null;
169         if (StringUtils.isEmpty(value)) {
170             if (value == null) {
171                 validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
172             } else {
173                 validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
174             }
175         }
176         return validatedStatus;
177     }
178
179     /**
180      * Validate the excluded Action Identifier to ensure they do not exist.
181      * Set Status if any error occurs.
182      *
183      * @param actionIdentifiers of the to be validated object
184      * @param exclusionKeys of a list of ACTID_KEYS should be ignored in this validation
185      * @return null if validation passed, otherwise, return Status with validation failure details.
186      */
187     Status validateExcludedActIds(ActionIdentifiers actionIdentifiers, EnumSet<ACTID_KEYS> exclusionKeys) {
188         StringBuilder names = new StringBuilder();
189         boolean append = false;
190         for (ACTID_KEYS key : ACTID_KEYS.values()) {
191             if (exclusionKeys.contains(key)) {
192                 continue;
193             }
194
195             switch (key) {
196                 case SERVICE_INSTANCE_ID:
197                     append = actionIdentifiers.getServiceInstanceId() != null;
198                     break;
199                 case VF_MODULE_ID:
200                     append = actionIdentifiers.getVfModuleId() != null;
201                     break;
202                 case VSERVER_ID:
203                     append = actionIdentifiers.getVserverId() != null;
204                     break;
205                 case VNFC_NAME:
206                     append = actionIdentifiers.getVnfcName() != null;
207                     break;
208                 case VNF_ID:
209                     append = actionIdentifiers.getVnfId() != null;
210                     break;
211                 default:
212                     append = false;
213             }
214
215             if (append) {
216                 names.append(key.getKeyName()).append(DELIMITER_COMMA);
217             }
218         }
219
220         Status validatedStatus = null;
221         int namesLength = names.length();
222         if (namesLength != 0) {
223             names.setLength(namesLength - 1);
224             validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, names.toString());
225         }
226
227         return validatedStatus;
228     }
229
230     /**
231      * Get RequestHandlerInput
232      * @param commonHeader of the input
233      * @param actionIdentifiers of the input
234      * @param payload of the input
235      * @param callerClassName String of this.getClass().getName() of the call class
236      * @return the newly built RequestHandlerInput if no error occured, otherwise, return null.
237      */
238     RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
239                                                ActionIdentifiers actionIdentifiers,
240                                                Payload payload,
241                                                String callerClassName) {
242
243         try {
244             RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
245                 .commonHeader(commonHeader)
246                 .actionIdentifiers(actionIdentifiers)
247                 .action(expectedAction.name())
248                 .rpcName(rpcName);
249             if (payload != null) {
250                 requestInputBuilder = requestInputBuilder.payload(payload);
251             }
252             return requestInputBuilder.build();
253         } catch (ParseException e) {
254             status = buildStatusWithParseException(e);
255
256             LoggingUtils.logErrorMessage(
257                 LoggingConstants.TargetNames.APPC_PROVIDER,
258                 String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
259                 callerClassName);
260         }
261         return null;
262     }
263
264     /**
265      * Execute the action through RequestExecutor
266      * @param requestHandlerInput contains everything about the action
267      */
268     RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
269         RequestHandlerOutput requestHandlerOutput = null;
270         if (requestHandlerInput == null) {
271             status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
272                 "executeAction with null RequestHandlerInput");
273         } else {
274             RequestExecutor requestExecutor = new RequestExecutor();
275             requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
276             status = buildStatusWithDispatcherOutput(requestHandlerOutput);
277         }
278         return requestHandlerOutput;
279     }
280 }