Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / pnf / delegate / ConfigCheckerDelegate.java
1 /*
2  * ============LICENSE_START======================================================= Copyright (C) 2019 Nordix
3  * Foundation. ================================================================================ Licensed under the
4  * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5  * obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied. See the License for the specific language governing permissions and limitations under the
10  * License.
11  *
12  * SPDX-License-Identifier: Apache-2.0 ============LICENSE_END=========================================================
13  */
14
15 package org.onap.so.bpmn.infrastructure.pnf.delegate;
16
17 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.MODEL_UUID;
18 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_NAME;
19 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_BLUEPRINT_VERSION;
20 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_CUSTOMIZATION_UUID;
21 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PRC_INSTANCE_NAME;
22 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SERVICE_MODEL_INFO;
23 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.SKIP_POST_INSTANTIATION_CONFIGURATION;
24 import java.util.List;
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.camunda.bpm.engine.delegate.JavaDelegate;
27 import org.onap.so.bpmn.core.json.JsonUtils;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
30 import org.onap.so.db.catalog.client.CatalogDbClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * This implementation of {@link JavaDelegate} is used to check the SO catalogdb for PNF config flag.
38  *
39  * It queries the PNF resource customization table for the skip_post_instantiation_configuration for required PNF model
40  * UUID.
41  */
42 @Component
43 public class ConfigCheckerDelegate implements JavaDelegate {
44
45     private Logger logger = LoggerFactory.getLogger(ConfigCheckerDelegate.class);
46
47     // ERROR CODE for variable not found in the delegation Context
48     private static int ERROR_CODE = 601;
49
50     @Autowired
51     protected ExceptionBuilder exceptionUtil;
52
53     @Autowired
54     protected CatalogDbClient catalogDbClient;
55
56     @Override
57     public void execute(DelegateExecution delegateExecution) throws Exception {
58
59         logger.debug("Running execute block for activity id:{}, name:{}", delegateExecution.getCurrentActivityId(),
60                 delegateExecution.getCurrentActivityName());
61
62         if (delegateExecution.hasVariable(SERVICE_MODEL_INFO)) {
63             String serviceModelInfo = (String) delegateExecution.getVariable(SERVICE_MODEL_INFO);
64             String serviceModelUuid = JsonUtils.getJsonValue(serviceModelInfo, MODEL_UUID);
65             delegateExecution.setVariable(MODEL_UUID, serviceModelUuid);
66             List<PnfResourceCustomization> pnfCustomizations =
67                     catalogDbClient.getPnfResourceCustomizationByModelUuid(serviceModelUuid);
68             if (pnfCustomizations != null && pnfCustomizations.size() >= 1) {
69                 PnfResourceCustomization pnfResourceCustomization = pnfCustomizations.get(0);
70                 boolean skipPostInstantiationConfiguration = pnfResourceCustomization.isSkipPostInstConf();
71                 delegateExecution.setVariable(SKIP_POST_INSTANTIATION_CONFIGURATION,
72                         skipPostInstantiationConfiguration);
73                 delegateExecution.setVariable(PRC_BLUEPRINT_NAME, pnfResourceCustomization.getBlueprintName());
74                 delegateExecution.setVariable(PRC_BLUEPRINT_VERSION, pnfResourceCustomization.getBlueprintVersion());
75                 delegateExecution.setVariable(PRC_CUSTOMIZATION_UUID,
76                         pnfResourceCustomization.getModelCustomizationUUID());
77                 delegateExecution.setVariable(PRC_INSTANCE_NAME, pnfResourceCustomization.getModelInstanceName());
78             } else {
79                 logger.warn("Unable to find the PNF resource customizations of model service UUID: {}",
80                         serviceModelUuid);
81                 exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
82                         "Unable to find the PNF resource customizations of model service UUID:  " + serviceModelUuid);
83             }
84         } else {
85             logger.warn("Unable to find the parameter: {} in the execution context", SERVICE_MODEL_INFO);
86             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
87                     "Unable to find parameter " + SERVICE_MODEL_INFO);
88         }
89     }
90 }