1b185f7c03ea80c44f57a2c5296b71b7c775c5ae
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / pnf / delegate / PnfCheckInputs.java
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.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class PnfCheckInputs implements JavaDelegate {
40
41     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}$";
42
43     private String pnfEntryNotificationTimeout;
44
45     @Autowired
46     public PnfCheckInputs(@Value("${aai.pnfEntryNotificationTimeout}") String pnfEntryNotificationTimeout) {
47         this.pnfEntryNotificationTimeout = pnfEntryNotificationTimeout;
48     }
49
50     @Override
51     public void execute(DelegateExecution execution) {
52         validateCorrelationId(execution);
53         validatePnfUuid(execution);
54         validateTimeout(execution);
55         validateServiceInstanceId(execution);
56     }
57
58     private void validateCorrelationId(DelegateExecution execution) {
59         String correlationId = (String) execution.getVariable(CORRELATION_ID);
60         if (Strings.isNullOrEmpty(correlationId)) {
61             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "correlationId variable not defined");
62         }
63     }
64
65     private void validatePnfUuid(DelegateExecution execution) {
66         String pnfUuid = (String) execution.getVariable(PNF_UUID);
67         if (Strings.isNullOrEmpty(pnfUuid)) {
68             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "pnfUuid variable not defined");
69         }
70         if (!pnfUuid.matches(UUID_REGEX)) {
71             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "pnfUuid is not a valid UUID");
72         }
73     }
74
75     private void validateTimeout(DelegateExecution execution) {
76         if (Strings.isNullOrEmpty(pnfEntryNotificationTimeout)) {
77             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999,
78                     "timeoutForPnfEntryNotification value not defined");
79         }
80         execution.setVariable(TIMEOUT_FOR_NOTIFICATION, pnfEntryNotificationTimeout);
81     }
82
83     private void validateServiceInstanceId(DelegateExecution execution) {
84         String serviceInstanceId = (String) execution.getVariable(SERVICE_INSTANCE_ID);
85         if (Strings.isNullOrEmpty(serviceInstanceId)) {
86             new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999, "serviceInstanceId variable not defined");
87         }
88     }
89 }