Merge "use encrypted auth for dmaap"
[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.TIMEOUT_FOR_NOTIFICATION;
28
29 import com.google.common.base.Strings;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.JavaDelegate;
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
33 import org.onap.so.logger.MsoLogger;
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     private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, PnfCheckInputs.class);
43
44     private String defaultTimeout;
45
46     @Autowired
47     public PnfCheckInputs(@Value("${aai.pnfEntryNotificationTimeout}") String defaultTimeout) {
48         this.defaultTimeout = defaultTimeout;
49     }
50
51     @Override
52     public void execute(DelegateExecution execution) {
53         validateCorrelationId(execution);
54         validatePnfUuid(execution);
55         validateTimeout(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         String timeout = (String) execution.getVariable(TIMEOUT_FOR_NOTIFICATION);
77         if (Strings.isNullOrEmpty(timeout)) {
78             LOGGER.debug("timeoutForPnfEntryNotification variable not found, setting default");
79             if (defaultTimeout == null) {
80                 new ExceptionUtil().buildAndThrowWorkflowException(execution, 9999,
81                     "default timeoutForPnfEntryNotification value not defined");
82             }
83             execution.setVariable(TIMEOUT_FOR_NOTIFICATION, defaultTimeout);
84         }
85     }
86 }