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