Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / CloudOwnerServiceImpl.java
1 package org.onap.vid.services;
2
3
4 import com.google.common.collect.ImmutableList;
5 import org.apache.commons.lang3.StringUtils;
6 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
7 import org.onap.vid.aai.AaiClientInterface;
8 import org.onap.vid.exceptions.GenericUncheckedException;
9 import org.onap.vid.exceptions.NotFoundException;
10 import org.onap.vid.mso.model.CloudConfiguration;
11 import org.onap.vid.mso.rest.RequestDetails;
12 import org.onap.vid.properties.Features;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.togglz.core.manager.FeatureManager;
15
16 import java.util.List;
17 import java.util.Map;
18
19 public class CloudOwnerServiceImpl implements CloudOwnerService {
20
21     private static final List<String> CLOUD_CONFIGURATION_PATH = ImmutableList.of("requestDetails", "cloudConfiguration");
22     private static final List<String> LCP_CLOUD_REGION_ID_PATH = ImmutableList.of("requestDetails", "cloudConfiguration", "lcpCloudRegionId");
23
24     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(CloudOwnerService.class);
25
26     private final AaiClientInterface aaiClient;
27     private final FeatureManager featureManager;
28
29     @Autowired
30     public CloudOwnerServiceImpl(AaiClientInterface aaiClient, FeatureManager featureManager) {
31         this.aaiClient = aaiClient;
32         this.featureManager = featureManager;
33     }
34
35     @Override
36     public void enrichRequestWithCloudOwner(RequestDetails msoRequest) {
37         if (!featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)) {
38             return;
39         }
40         try {
41
42             //if cloudConfiguration field contains lcpRegion (e.g. in changeManagement scenarios)
43             if (msoRequest.getCloudConfiguration()!=null && StringUtils.isNotEmpty(msoRequest.getCloudConfiguration().getLcpCloudRegionId())) {
44                 enrichCloudConfigurationWithCloudOwner(msoRequest.getCloudConfiguration(), msoRequest.getCloudConfiguration().getLcpCloudRegionId());
45             }
46             //otherwise the cloudConfiguration is in the additionalProperties field of RequestDetails (e.g. in ng1 view/edit scenario)
47             else {
48                 enrichRequestWithCloudOwnerByAdditionalProperties(msoRequest);
49             }
50         }
51         catch (Exception e) {
52             throw new GenericUncheckedException("Failed to enrich requestDetails with cloudOwner", e);
53         }
54     }
55
56     protected void enrichRequestWithCloudOwnerByAdditionalProperties(RequestDetails msoRequest) {
57         String lcpCloudRegionId = null;
58         try {
59             lcpCloudRegionId = msoRequest.extractValueByPathUsingAdditionalProperties(LCP_CLOUD_REGION_ID_PATH, String.class);
60         }
61         catch (NotFoundException exception) {
62             LOGGER.debug("Can't find lcp region in RequestDetails. Assume no cloudOwner enrichment is needed. Reason: "+exception.getMessage());
63             return;
64         }
65         String cloudOwner = aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId);
66         msoRequest.extractValueByPathUsingAdditionalProperties(CLOUD_CONFIGURATION_PATH, Map.class).put("cloudOwner", cloudOwner);
67     }
68
69     @Override
70     public void enrichCloudConfigurationWithCloudOwner(CloudConfiguration cloudConfiguration, String lcpCloudRegionId) {
71         if (featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)) {
72             String cloudOwner = aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId);
73             cloudConfiguration.setCloudOwner(cloudOwner);
74         }
75     }
76 }