Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / CloudOwnerServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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.vid.services;
22
23
24 import com.google.common.collect.ImmutableList;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.onap.vid.aai.AaiClientInterface;
28 import org.onap.vid.exceptions.GenericUncheckedException;
29 import org.onap.vid.exceptions.NotFoundException;
30 import org.onap.vid.mso.model.CloudConfiguration;
31 import org.onap.vid.mso.rest.RequestDetails;
32 import org.onap.vid.properties.Features;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.togglz.core.manager.FeatureManager;
35
36 import java.util.List;
37 import java.util.Map;
38
39 public class CloudOwnerServiceImpl implements CloudOwnerService {
40
41     private static final List<String> CLOUD_CONFIGURATION_PATH = ImmutableList.of("requestDetails", "cloudConfiguration");
42     private static final List<String> LCP_CLOUD_REGION_ID_PATH = ImmutableList.of("requestDetails", "cloudConfiguration", "lcpCloudRegionId");
43
44     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(CloudOwnerService.class);
45
46     private final AaiClientInterface aaiClient;
47     private final FeatureManager featureManager;
48
49     @Autowired
50     public CloudOwnerServiceImpl(AaiClientInterface aaiClient, FeatureManager featureManager) {
51         this.aaiClient = aaiClient;
52         this.featureManager = featureManager;
53     }
54
55     @Override
56     public void enrichRequestWithCloudOwner(RequestDetails msoRequest) {
57         if (!featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)) {
58             return;
59         }
60         try {
61
62             //if cloudConfiguration field contains lcpRegion (e.g. in changeManagement scenarios)
63             if (msoRequest.getCloudConfiguration()!=null && StringUtils.isNotEmpty(msoRequest.getCloudConfiguration().getLcpCloudRegionId())) {
64                 enrichCloudConfigurationWithCloudOwner(msoRequest.getCloudConfiguration(), msoRequest.getCloudConfiguration().getLcpCloudRegionId());
65             }
66             //otherwise the cloudConfiguration is in the additionalProperties field of RequestDetails (e.g. in ng1 view/edit scenario)
67             else {
68                 enrichRequestWithCloudOwnerByAdditionalProperties(msoRequest);
69             }
70         }
71         catch (Exception e) {
72             throw new GenericUncheckedException("Failed to enrich requestDetails with cloudOwner", e);
73         }
74     }
75
76     private void enrichRequestWithCloudOwnerByAdditionalProperties(RequestDetails msoRequest) {
77         final Map cloudConfiguration;
78         final String lcpCloudRegionId;
79         try {
80             cloudConfiguration = msoRequest.extractValueByPathUsingAdditionalProperties(CLOUD_CONFIGURATION_PATH, Map.class);
81             lcpCloudRegionId = msoRequest.extractValueByPathUsingAdditionalProperties(LCP_CLOUD_REGION_ID_PATH, String.class);
82         }
83         catch (NotFoundException exception) {
84             LOGGER.debug("Can't find lcp region in RequestDetails. Assume no cloudOwner enrichment is needed. Reason: ", exception);
85             return;
86         }
87
88         cloudConfiguration.computeIfAbsent("cloudOwner", k ->  aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId));
89     }
90
91     @Override
92     public void enrichCloudConfigurationWithCloudOwner(CloudConfiguration cloudConfiguration, String lcpCloudRegionId) {
93         if (StringUtils.isEmpty(cloudConfiguration.getCloudOwner()) && featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)) {
94             String cloudOwner = aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId);
95             cloudConfiguration.setCloudOwner(cloudOwner);
96         }
97     }
98 }