aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/tasks/inventory/CreateAAIInventory.java
blob: df4229c985eea06aae58155f2447fb84b6d92742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.tasks.inventory;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.onap.aaiclient.client.aai.AAIResourcesClient;
import org.onap.so.cloud.CloudConfig;
import org.onap.so.cloud.resource.beans.CloudInformation;
import org.onap.so.db.catalog.beans.CloudIdentity;
import org.onap.so.db.catalog.beans.CloudSite;
import org.onap.so.heatbridge.HeatBridgeApi;
import org.onap.so.heatbridge.HeatBridgeImpl;
import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.Image;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.heat.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class CreateAAIInventory {

    private static final Logger logger = LoggerFactory.getLogger(CreateAAIInventory.class);

    private AAIResourcesClient aaiClient;

    @Autowired
    protected CloudConfig cloudConfig;

    @Autowired
    protected Environment env;

    public void heatbridge(CloudInformation cloudInformation) {
        try {
            CloudSite cloudSite = cloudConfig.getCloudSite(cloudInformation.getRegionId())
                    .orElseThrow(() -> new MsoCloudSiteNotFound(cloudInformation.getRegionId()));
            CloudIdentity cloudIdentity = cloudSite.getIdentityService();
            String heatStackId = cloudInformation.getTemplateInstanceId().split("/")[1];

            List<String> oobMgtNetNames = new ArrayList<>();

            HeatBridgeApi heatBridgeClient =
                    new HeatBridgeImpl(new AAIResourcesClient(), cloudIdentity, cloudInformation.getOwner(),
                            cloudInformation.getRegionId(), cloudSite.getRegionId(), cloudInformation.getTenantId());

            heatBridgeClient.authenticate();

            List<Resource> stackResources =
                    heatBridgeClient.queryNestedHeatStackResources(cloudInformation.getTemplateInstanceId());

            List<Server> osServers = heatBridgeClient.getAllOpenstackServers(stackResources);

            heatBridgeClient.createPserversAndPinterfacesIfNotPresentInAai(stackResources);

            List<Image> osImages = heatBridgeClient.extractOpenstackImagesFromServers(osServers);

            List<Flavor> osFlavors = heatBridgeClient.extractOpenstackFlavorsFromServers(osServers);

            logger.debug("Successfully queried heat stack{} for resources.", heatStackId);
            // os images
            if (osImages != null && !osImages.isEmpty()) {
                heatBridgeClient.buildAddImagesToAaiAction(osImages);
                logger.debug("Successfully built AAI actions to add images.");
            } else {
                logger.debug("No images to update to AAI.");
            }
            // flavors
            if (osFlavors != null && !osFlavors.isEmpty()) {
                heatBridgeClient.buildAddFlavorsToAaiAction(osFlavors);
                logger.debug("Successfully built AAI actions to add flavors.");
            } else {
                logger.debug("No flavors to update to AAI.");
            }

            // compute resources
            heatBridgeClient.buildAddVserversToAaiAction(cloudInformation.getVnfId(), cloudInformation.getVfModuleId(),
                    osServers);
            logger.debug("Successfully queried compute resources and built AAI vserver actions.");

            // neutron resources
            List<String> oobMgtNetIds = new ArrayList<>();

            // if no network-id list is provided, however network-name list is
            if (!CollectionUtils.isEmpty(oobMgtNetNames)) {
                oobMgtNetIds = heatBridgeClient.extractNetworkIds(oobMgtNetNames);
            }
            heatBridgeClient.buildAddVserverLInterfacesToAaiAction(stackResources, oobMgtNetIds,
                    cloudInformation.getOwner());
            logger.debug(
                    "Successfully queried neutron resources and built AAI actions to add l-interfaces to vservers.");

            // Update AAI
            logger.debug("Current Dry Run Value: {}", env.getProperty("heatBridgeDryrun", Boolean.class, true));
            heatBridgeClient.submitToAai(env.getProperty("heatBridgeDryrun", Boolean.class, true));
        } catch (Exception ex) {
            logger.debug("Heatbrige failed for stackId: " + cloudInformation.getTemplateInstanceId(), ex);
        }
    }

    protected AAIResourcesClient getAaiClient() {
        if (aaiClient == null)
            return new AAIResourcesClient();
        else
            return aaiClient;
    }

    protected void setAaiClient(AAIResourcesClient aaiResource) {
        aaiClient = aaiResource;
    }
}