a975339e587c40362ca16674846debb4bd8cc897
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2018 Nokia
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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.pnf.delegate;
24
25 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.CORRELATION_ID;
26 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_UUID;
27 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_INSTANCE_ID;
28 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.TIMEOUT_FOR_NOTIFICATION;
29
30 import com.google.common.base.Strings;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.camunda.bpm.engine.delegate.JavaDelegate;
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
34 import org.onap.so.logger.MsoLogger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class PnfCheckInputs implements JavaDelegate {
41
42     public static final String UUID_REGEX = "(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[1-5]{1}[0-9a-f]{3}-[89ab]{1}[0-9a-f]{3}-[0-9a-f]{12}$";
43     private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, PnfCheckInputs.class);
44
45     private String defaultTimeout;
46
47     @Autowired
48     public PnfCheckInputs(@Value("${aai.pnfEntryNotificationTimeout}") String defaultTimeout) {
49         this.defaultTimeout = defaultTimeout;
50     }
51
52     @Override
53     public void execute(DelegateExecution execution) {
54         validateCorrelationId(execution);
55         validatePnfUuid(execution);
56         validateTimeout(execution);
57         validateServiceInstanceId(execution);
58     }
59
60     private void validateCorrelationId(DelegateExecution execution) {
61         String correlationId = (String) execution.getVariable(CORRELATION_ID);
62         if (Strings.isNullOrEmpty(correlationId)) {
63             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "correlationId variable not defined");
64         }
65     }
66
67     private void validatePnfUuid(DelegateExecution execution) {
68         String pnfUuid = (String) execution.getVariable(PNF_UUID);
69         if (Strings.isNullOrEmpty(pnfUuid)) {
70             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "pnfUuid variable not defined");
71         }
72         if (!pnfUuid.matches(UUID_REGEX)) {
73             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "pnfUuid is not a valid UUID");
74         }
75     }
76
77     private void validateTimeout(DelegateExecution execution) {
78         String timeout = (String) execution.getVariable(TIMEOUT_FOR_NOTIFICATION);
79         if (Strings.isNullOrEmpty(timeout)) {
80             LOGGER.debug("timeoutForPnfEntryNotification variable not found, setting default");
81             if (defaultTimeout == null) {
82                 new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999,
83                     "default timeoutForPnfEntryNotification value not defined");
84             }
85             execution.setVariable(TIMEOUT_FOR_NOTIFICATION, defaultTimeout);
86         }
87     }
88
89     private void validateServiceInstanceId(DelegateExecution execution) {
90         String serviceInstanceId = (String) execution.getVariable(SERVICE_INSTANCE_ID);
91         if (Strings.isNullOrEmpty(serviceInstanceId)) {
92             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "serviceInstanceId variable not defined");
93         }
94     }
95 }