52d294955ae38699f5c1907204e420b02f22e5b8
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.validations;
22
23 import java.util.Optional;
24 import java.util.regex.Pattern;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.common.listener.validation.PreBuildingBlockValidator;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.onap.so.listener.Skip;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 @Skip
37 public class CloudRegionOrchestrationValidator implements PreBuildingBlockValidator {
38
39     private static Logger logger = LoggerFactory.getLogger(CloudRegionOrchestrationValidator.class);
40     private final Pattern pattern = Pattern.compile(
41             "(?:Activate|Assign|Create|Deactivate|Delete|Unassign|Update)(?:Network|Vnf|VfModule|VolumeGroup|FabricConfiguration)BB");
42
43     @Autowired
44     private ExceptionBuilder exceptionBuilder;
45
46     @Override
47     public boolean shouldRunFor(String bbName) {
48         return pattern.matcher(bbName).find();
49     }
50
51     @Override
52     public Optional<String> validate(BuildingBlockExecution execution) {
53         String msg = null;
54         try {
55             CloudRegion cloudRegion = execution.getGeneralBuildingBlock().getCloudRegion();
56             if (Boolean.TRUE.equals(cloudRegion.getOrchestrationDisabled())) {
57                 msg = String.format(
58                         "Error: The request has failed due to orchestration currently disabled for the target cloud region %s for cloud owner %s",
59                         cloudRegion.getLcpCloudRegionId(), cloudRegion.getCloudOwner());
60                 logger.error(msg);
61                 return Optional.ofNullable(msg);
62             }
63         } catch (Exception e) {
64             logger.error("failed to validate", e);
65             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e);
66         }
67         return Optional.empty();
68     }
69
70 }