2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
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;
36 import java.util.List;
39 public class CloudOwnerServiceImpl implements CloudOwnerService {
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");
44 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(CloudOwnerService.class);
46 private final AaiClientInterface aaiClient;
47 private final FeatureManager featureManager;
50 public CloudOwnerServiceImpl(AaiClientInterface aaiClient, FeatureManager featureManager) {
51 this.aaiClient = aaiClient;
52 this.featureManager = featureManager;
56 public void enrichRequestWithCloudOwner(RequestDetails msoRequest) {
57 if (!featureManager.isActive(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST)) {
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());
66 //otherwise the cloudConfiguration is in the additionalProperties field of RequestDetails (e.g. in ng1 view/edit scenario)
68 enrichRequestWithCloudOwnerByAdditionalProperties(msoRequest);
72 throw new GenericUncheckedException("Failed to enrich requestDetails with cloudOwner", e);
76 private void enrichRequestWithCloudOwnerByAdditionalProperties(RequestDetails msoRequest) {
77 final Map cloudConfiguration;
78 final String lcpCloudRegionId;
80 cloudConfiguration = msoRequest.extractValueByPathUsingAdditionalProperties(CLOUD_CONFIGURATION_PATH, Map.class);
81 lcpCloudRegionId = msoRequest.extractValueByPathUsingAdditionalProperties(LCP_CLOUD_REGION_ID_PATH, String.class);
83 catch (NotFoundException exception) {
84 LOGGER.debug("Can't find lcp region in RequestDetails. Assume no cloudOwner enrichment is needed. Reason: ", exception);
88 cloudConfiguration.computeIfAbsent("cloudOwner", k -> aaiClient.getCloudOwnerByCloudRegionId(lcpCloudRegionId));
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);