summaryrefslogtreecommitdiffstats
path: root/appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/lcm/service/AbstractBaseService.java
blob: ccf244532764efe148fc85575c3803e236a4d064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.provider.lcm.service;

import org.apache.commons.lang.StringUtils;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
import org.onap.appc.executor.objects.LCMCommandStatus;
import org.onap.appc.logging.LoggingConstants;
import org.onap.appc.logging.LoggingUtils;
import org.onap.appc.provider.lcm.util.RequestInputBuilder;
import org.onap.appc.provider.lcm.util.ValidationService;
import org.onap.appc.requesthandler.objects.RequestHandlerInput;
import org.onap.appc.requesthandler.objects.RequestHandlerOutput;

import java.text.ParseException;
import java.util.EnumSet;

public abstract class AbstractBaseService extends AbstractBaseUtils {
    /**
     * The list of ActionIdentifier keys.<br>
     * The extra space in the front of the keyName is for better REST API response output.
     */
    enum ACTID_KEYS {
        SERVICE_INSTANCE_ID(" service-instance-id"),
        VF_MODULE_ID(" vf-module-id"),
        VNF_ID(" vnf-id"),
        VNFC_NAME(" vnfc-name"),
        VSERVER_ID(" vserver-id");

        String keyName;

        ACTID_KEYS(String keyName) {
            this.keyName = keyName;
        }

        String getKeyName() {
            return keyName;
        }
    }

      String rpcName;
      Action expectedAction;

    Status status;

    protected AbstractBaseService(){};
    /**
     * Constructor
     *
     * @param theAction of the expected Action for this service
     */
    protected AbstractBaseService(Action theAction) {
        expectedAction = theAction;
        rpcName = getRpcName(theAction);
    }


    /**
     * Validate Input: <br>
     *    - using ValidationService to do common validation <br>
     *    - validate Action matches the expected Action <br>
     *    - validate existence of ActionIdentifier <br>
     *
     * @param commonHeader      of the input
     * @param action            of the input
     * @param actionIdentifiers of the input
     * @return null if validation passed, otherwise, return Status with validation failure details.
     */
    Status validateInput(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
        // common validation
        Status validatedStatus = ValidationService.getInstance().validateInput(commonHeader, action, rpcName);
        if (validatedStatus != null) {
            return validatedStatus;
        }

        // action validation
        if (action != expectedAction) {
            validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, "action");
            return validatedStatus;
        }

        // action identifier
        if (actionIdentifiers == null) {
            validatedStatus = buildStatusForParamName(
                LCMCommandStatus.MISSING_MANDATORY_PARAMETER, "action-identifiers");
        }

        return validatedStatus;
    }

    /**
     * Validate input as well as VNF ID in actionIdentifier
     *
     * @param commonHeader      of the input
     * @param action            of the input
     * @param actionIdentifiers of the input
     * @return null if validation passed, otherwise, return Status with validation failure details.
     */
    Status validateVnfId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
        Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
        if (validatedStatus != null) {
            return validatedStatus;
        }

        validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVnfId(), "vnf-id");
        if (validatedStatus == null) {
            validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VNF_ID));
        }

        return validatedStatus;
    }

    /**
     * Validate input as well as VSERVER ID in actionIdentifier
     *
     * @param commonHeader      of the input
     * @param action            of the input
     * @param actionIdentifiers of the input
     * @return null if validation passed, otherwise, return Status with validation failure details.
     */
    Status validateVserverId(CommonHeader commonHeader, Action action, ActionIdentifiers actionIdentifiers) {
        Status validatedStatus = validateInput(commonHeader, action, actionIdentifiers);
        if (validatedStatus != null) {
            return validatedStatus;
        }

        validatedStatus = validateMustHaveParamValue(actionIdentifiers.getVserverId(), "vserver-id");
        if (validatedStatus == null) {
            validatedStatus = validateExcludedActIds(actionIdentifiers, EnumSet.of(ACTID_KEYS.VSERVER_ID));
        }

        return validatedStatus;
    }

    /**
     * Validate a value of the must have parameter
     * @param value   the value of the parameter
     * @param keyName the key name of the parameter
     * @return null if validation passed, otherwise, return Status with validation failure details.
     */
    Status validateMustHaveParamValue(String value, String keyName) {
        Status validatedStatus = null;
        if (StringUtils.isEmpty(value)) {
            if (value == null) {
                validatedStatus = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, keyName);
            } else {
                validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, keyName);
            }
        }
        return validatedStatus;
    }

    /**
     * Validate the excluded Action Identifier to ensure they do not exist.
     * Set Status if any error occurs.
     *
     * @param actionIdentifiers of the to be validated object
     * @param exclusionKeys of a list of ACTID_KEYS should be ignored in this validation
     * @return null if validation passed, otherwise, return Status with validation failure details.
     */
    Status validateExcludedActIds(ActionIdentifiers actionIdentifiers, EnumSet<ACTID_KEYS> exclusionKeys) {
        StringBuilder names = new StringBuilder();
        boolean append = false;
        for (ACTID_KEYS key : ACTID_KEYS.values()) {
            if (exclusionKeys.contains(key)) {
                continue;
            }

            switch (key) {
                case SERVICE_INSTANCE_ID:
                    append = actionIdentifiers.getServiceInstanceId() != null;
                    break;
                case VF_MODULE_ID:
                    append = actionIdentifiers.getVfModuleId() != null;
                    break;
                case VSERVER_ID:
                    append = actionIdentifiers.getVserverId() != null;
                    break;
                case VNFC_NAME:
                    append = actionIdentifiers.getVnfcName() != null;
                    break;
                case VNF_ID:
                    append = actionIdentifiers.getVnfId() != null;
                    break;
                default:
                    append = false;
            }

            if (append) {
                names.append(key.getKeyName()).append(DELIMITER_COMMA);
            }
        }

        Status validatedStatus = null;
        int namesLength = names.length();
        if (namesLength != 0) {
            names.setLength(namesLength - 1);
            validatedStatus = buildStatusForErrorMsg(LCMCommandStatus.INVALID_INPUT_PARAMETER, names.toString());
        }

        return validatedStatus;
    }

    /**
     * Get RequestHandlerInput
     * @param commonHeader of the input
     * @param actionIdentifiers of the input
     * @param payload of the input
     * @param callerClassName String of this.getClass().getName() of the call class
     * @return the newly built RequestHandlerInput if no error occured, otherwise, return null.
     */
    RequestHandlerInput getRequestHandlerInput(CommonHeader commonHeader,
                                               ActionIdentifiers actionIdentifiers,
                                               Payload payload,
                                               String callerClassName) {

        try {
            RequestInputBuilder requestInputBuilder = new RequestInputBuilder().requestContext()
                .commonHeader(commonHeader)
                .actionIdentifiers(actionIdentifiers)
                .action(expectedAction.name())
                .rpcName(rpcName);
            if (payload != null) {
                requestInputBuilder = requestInputBuilder.payload(payload);
            }
            return requestInputBuilder.build();
        } catch (ParseException e) {
            status = buildStatusWithParseException(e);

            LoggingUtils.logErrorMessage(
                LoggingConstants.TargetNames.APPC_PROVIDER,
                String.format(COMMON_ERROR_MESSAGE_TEMPLATE, expectedAction, e.getMessage()),
                callerClassName);
        }
        return null;
    }

    /**
     * Execute the action through RequestExecutor
     * @param requestHandlerInput contains everything about the action
     */
    RequestHandlerOutput executeAction(RequestHandlerInput requestHandlerInput) {
        RequestHandlerOutput requestHandlerOutput = null;
        if (requestHandlerInput == null) {
            status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR,
                "executeAction with null RequestHandlerInput");
        } else {
            RequestExecutor requestExecutor = new RequestExecutor();
            requestHandlerOutput = requestExecutor.executeRequest(requestHandlerInput);
            status = buildStatusWithDispatcherOutput(requestHandlerOutput);
        }
        return requestHandlerOutput;
    }
}