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