df4229c985eea06aae58155f2447fb84b6d92742
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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.adapters.tasks.inventory;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.onap.aaiclient.client.aai.AAIResourcesClient;
27 import org.onap.so.cloud.CloudConfig;
28 import org.onap.so.cloud.resource.beans.CloudInformation;
29 import org.onap.so.db.catalog.beans.CloudIdentity;
30 import org.onap.so.db.catalog.beans.CloudSite;
31 import org.onap.so.heatbridge.HeatBridgeApi;
32 import org.onap.so.heatbridge.HeatBridgeImpl;
33 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
34 import org.openstack4j.model.compute.Flavor;
35 import org.openstack4j.model.compute.Image;
36 import org.openstack4j.model.compute.Server;
37 import org.openstack4j.model.heat.Resource;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.core.env.Environment;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class CreateAAIInventory {
46
47     private static final Logger logger = LoggerFactory.getLogger(CreateAAIInventory.class);
48
49     private AAIResourcesClient aaiClient;
50
51     @Autowired
52     protected CloudConfig cloudConfig;
53
54     @Autowired
55     protected Environment env;
56
57     public void heatbridge(CloudInformation cloudInformation) {
58         try {
59             CloudSite cloudSite = cloudConfig.getCloudSite(cloudInformation.getRegionId())
60                     .orElseThrow(() -> new MsoCloudSiteNotFound(cloudInformation.getRegionId()));
61             CloudIdentity cloudIdentity = cloudSite.getIdentityService();
62             String heatStackId = cloudInformation.getTemplateInstanceId().split("/")[1];
63
64             List<String> oobMgtNetNames = new ArrayList<>();
65
66             HeatBridgeApi heatBridgeClient =
67                     new HeatBridgeImpl(new AAIResourcesClient(), cloudIdentity, cloudInformation.getOwner(),
68                             cloudInformation.getRegionId(), cloudSite.getRegionId(), cloudInformation.getTenantId());
69
70             heatBridgeClient.authenticate();
71
72             List<Resource> stackResources =
73                     heatBridgeClient.queryNestedHeatStackResources(cloudInformation.getTemplateInstanceId());
74
75             List<Server> osServers = heatBridgeClient.getAllOpenstackServers(stackResources);
76
77             heatBridgeClient.createPserversAndPinterfacesIfNotPresentInAai(stackResources);
78
79             List<Image> osImages = heatBridgeClient.extractOpenstackImagesFromServers(osServers);
80
81             List<Flavor> osFlavors = heatBridgeClient.extractOpenstackFlavorsFromServers(osServers);
82
83             logger.debug("Successfully queried heat stack{} for resources.", heatStackId);
84             // os images
85             if (osImages != null && !osImages.isEmpty()) {
86                 heatBridgeClient.buildAddImagesToAaiAction(osImages);
87                 logger.debug("Successfully built AAI actions to add images.");
88             } else {
89                 logger.debug("No images to update to AAI.");
90             }
91             // flavors
92             if (osFlavors != null && !osFlavors.isEmpty()) {
93                 heatBridgeClient.buildAddFlavorsToAaiAction(osFlavors);
94                 logger.debug("Successfully built AAI actions to add flavors.");
95             } else {
96                 logger.debug("No flavors to update to AAI.");
97             }
98
99             // compute resources
100             heatBridgeClient.buildAddVserversToAaiAction(cloudInformation.getVnfId(), cloudInformation.getVfModuleId(),
101                     osServers);
102             logger.debug("Successfully queried compute resources and built AAI vserver actions.");
103
104             // neutron resources
105             List<String> oobMgtNetIds = new ArrayList<>();
106
107             // if no network-id list is provided, however network-name list is
108             if (!CollectionUtils.isEmpty(oobMgtNetNames)) {
109                 oobMgtNetIds = heatBridgeClient.extractNetworkIds(oobMgtNetNames);
110             }
111             heatBridgeClient.buildAddVserverLInterfacesToAaiAction(stackResources, oobMgtNetIds,
112                     cloudInformation.getOwner());
113             logger.debug(
114                     "Successfully queried neutron resources and built AAI actions to add l-interfaces to vservers.");
115
116             // Update AAI
117             logger.debug("Current Dry Run Value: {}", env.getProperty("heatBridgeDryrun", Boolean.class, true));
118             heatBridgeClient.submitToAai(env.getProperty("heatBridgeDryrun", Boolean.class, true));
119         } catch (Exception ex) {
120             logger.debug("Heatbrige failed for stackId: " + cloudInformation.getTemplateInstanceId(), ex);
121         }
122     }
123
124     protected AAIResourcesClient getAaiClient() {
125         if (aaiClient == null)
126             return new AAIResourcesClient();
127         else
128             return aaiClient;
129     }
130
131     protected void setAaiClient(AAIResourcesClient aaiResource) {
132         aaiClient = aaiResource;
133     }
134 }