3eba8b217182825c6748e1c971e7f584f52541ba
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / VserverManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.google.gson.Gson;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonObject;
21 import com.nokia.cbam.lcm.v32.model.AffectedVirtualStorage;
22 import com.nokia.cbam.lcm.v32.model.AffectedVnfc;
23 import io.reactivex.Observable;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.onap.aai.model.Relationship;
27 import org.onap.aai.model.Volume;
28 import org.onap.aai.model.Vserver;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
33 import org.slf4j.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Conditional;
36 import org.springframework.stereotype.Component;
37
38 import static com.google.common.collect.Iterables.find;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
43
44 /**
45  * Responsible for managing {@link Vserver} in AAI
46  */
47 @Component
48 @Conditional(value = Conditions.UseForDirect.class)
49 class VserverManager extends AbstractManager {
50     private static Logger logger = org.slf4j.LoggerFactory.getLogger(AbstractManager.class);
51
52     @Autowired
53     VserverManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
54         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
55     }
56
57     static Relationship linkTo(String vimId, String tenantId, String serverProviderId) {
58         Relationship relationship = new Relationship();
59         relationship.setRelatedTo("vserver");
60         relationship.setRelationshipData(new ArrayList<>());
61         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(vimId)));
62         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-region-id", getRegionName(vimId)));
63         relationship.getRelationshipData().add(buildRelationshipData("tenant.tenant-id", tenantId));
64         relationship.getRelationshipData().add(buildRelationshipData("vserver.vserver-id", serverProviderId));
65         return relationship;
66     }
67
68     static String getTenantId(AffectedVnfc cbamVnfc) {
69         return extractMandatoryValue(cbamVnfc.getComputeResource().getAdditionalData(), "tenantId");
70     }
71
72     @Override
73     protected Logger getLogger() {
74         return logger;
75     }
76
77     void update(String vimId, String vnfId, AffectedVnfc cbamVnfc, List<AffectedVirtualStorage> affectedVirtualStorages, boolean inMaintenance) {
78         Vserver vserver = createOrGet(getVserver(vimId, cbamVnfc), new Vserver());
79         updateFields(vimId, vserver, cbamVnfc, vnfId, affectedVirtualStorages, inMaintenance);
80     }
81
82     void delete(String vimId, AffectedVnfc deletedVnfc) {
83         String tenantId = getTenantId(deletedVnfc);
84         String cloudOwner = getCloudOwner(vimId);
85         String regionName = getRegionName(vimId);
86         Vserver vserver = getVserver(vimId, deletedVnfc).blockingFirst();
87         aaiRestApiProvider.getCloudInfrastructureApi().deleteCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserver(cloudOwner, regionName, tenantId, vserver.getVserverId(), vserver.getResourceVersion());
88     }
89
90     private Observable<Vserver> getVserver(String vimId, AffectedVnfc cbamVnfc) {
91         String tenantId = getTenantId(cbamVnfc);
92         String cloudOwner = getCloudOwner(vimId);
93         String regionName = getRegionName(vimId);
94         return aaiRestApiProvider.getCloudInfrastructureApi().getCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserver(cloudOwner, regionName, tenantId, cbamVnfc.getComputeResource().getResourceId(), null, null, null, null, null, null, null, null, null);
95     }
96
97     private void updateFields(String vimId, Vserver server, AffectedVnfc cbamVnfc, String vnfId, List<AffectedVirtualStorage> affectedVirtualStorages, boolean inMaintenance) {
98         server.setInMaint(inMaintenance);
99         server.setIsClosedLoopDisabled(inMaintenance);
100         JsonElement additionalData = new Gson().toJsonTree(cbamVnfc.getComputeResource().getAdditionalData());
101         server.setVserverName(additionalData.getAsJsonObject().get("name").getAsString());
102         server.setVserverId(cbamVnfc.getComputeResource().getResourceId());
103         server.setProvStatus("active");
104         server.setRelationshipList(new ArrayList<>());
105         server.setVserverId(cbamVnfc.getComputeResource().getResourceId());
106         server.setVserverSelflink(extractSelfLink(cbamVnfc.getComputeResource().getAdditionalData()));
107         addSingletonRelation(server.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
108         if (cbamVnfc.getStorageResourceIds() != null) {
109             if (server.getVolumes() == null) {
110                 server.setVolumes(new ArrayList<>());
111             }
112             for (String virtualStorageId : cbamVnfc.getStorageResourceIds()) {
113                 Volume volume = new Volume();
114                 AffectedVirtualStorage affectedStorage = find(affectedVirtualStorages, storage -> virtualStorageId.equals(storage.getId()));
115                 volume.setVolumeId(affectedStorage.getResource().getResourceId());
116                 server.getVolumes().add(volume);
117             }
118         } else {
119             server.setVolumes(new ArrayList<>());
120         }
121         String tenantId = getTenantId(cbamVnfc);
122         String cloudOwner = getCloudOwner(vimId);
123         String regionName = getRegionName(vimId);
124         systemFunctions().blockingFirst(aaiRestApiProvider.getCloudInfrastructureApi().createOrUpdateCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserver(cloudOwner, regionName, tenantId, server.getVserverId(), server));
125     }
126
127     private String extractSelfLink(Object additionalData) {
128         try {
129             JsonObject root = new Gson().toJsonTree(additionalData).getAsJsonObject();
130             for (JsonElement link : childElement(root, "links").getAsJsonArray()) {
131                 if (link.getAsJsonObject().has("rel") && "self".equals(link.getAsJsonObject().get("rel").getAsString())) {
132                     return link.getAsJsonObject().get("href").getAsString();
133                 }
134             }
135             return "unknown";
136         } catch (Exception e) {
137             logger.debug("Missing links in the server", e);
138             return "unknown";
139         }
140     }
141 }