add direct information source
[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 org.onap.aai.domain.yang.v11.*;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Conditional;
31 import org.springframework.stereotype.Component;
32
33 import java.util.List;
34
35 import static com.google.common.collect.Iterables.find;
36 import static java.lang.String.format;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.CLOUD;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
41
42 /**
43  * Responsible for managing {@link Vserver} in AAI
44  */
45 @Component
46 @Conditional(value = Conditions.UseForDirect.class)
47 class VserverManager extends AbstractManager {
48     private static Logger logger = org.slf4j.LoggerFactory.getLogger(VserverManager.class);
49
50     @Autowired
51     VserverManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
52         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
53     }
54
55     static Relationship linkTo(String vimId, String tenantId, String serverProviderId) {
56         Relationship relationship = new Relationship();
57         relationship.setRelatedTo("vserver");
58         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(vimId)));
59         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-region-id", getRegionName(vimId)));
60         relationship.getRelationshipData().add(buildRelationshipData("tenant.tenant-id", tenantId));
61         relationship.getRelationshipData().add(buildRelationshipData("vserver.vserver-id", serverProviderId));
62         return relationship;
63     }
64
65     static String getTenantId(AffectedVnfc cbamVnfc) {
66         return extractMandatoryValue(cbamVnfc.getComputeResource().getAdditionalData(), "tenantId");
67     }
68
69     @Override
70     protected Logger getLogger() {
71         return logger;
72     }
73
74     void update(String vimId, String vnfId, AffectedVnfc cbamVnfc, List<AffectedVirtualStorage> affectedVirtualStorages, boolean inMaintenance) {
75         String url = buildUrl(vimId, cbamVnfc);
76         Vserver vserver = createOrGet(CLOUD, url, OBJECT_FACTORY.createVserver());
77         updateFields(vserver, cbamVnfc, vnfId, affectedVirtualStorages, url, inMaintenance);
78     }
79
80     void delete(String vimId, com.nokia.cbam.lcm.v32.model.AffectedVnfc deletedVnfc) {
81         aaiRestApiProvider.delete(logger, CLOUD, buildUrl(vimId, deletedVnfc));
82     }
83
84     private String buildUrl(String vimId, AffectedVnfc cbamVnfc) {
85         String tenantId = getTenantId(cbamVnfc);
86         String cloudOwner = getCloudOwner(vimId);
87         String regionName = getRegionName(vimId);
88         return format("/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s/vservers/vserver/%s", cloudOwner, regionName, tenantId, cbamVnfc.getComputeResource().getResourceId());
89     }
90
91     private void updateFields(Vserver server, AffectedVnfc cbamVnfc, String vnfId, List<AffectedVirtualStorage> affectedVirtualStorages, String url, boolean inMaintenance) {
92         server.setInMaint(inMaintenance);
93         server.setIsClosedLoopDisabled(inMaintenance);
94         JsonElement additionalData = new Gson().toJsonTree(cbamVnfc.getComputeResource().getAdditionalData());
95         server.setVserverName(additionalData.getAsJsonObject().get("name").getAsString());
96         server.setVserverId(cbamVnfc.getComputeResource().getResourceId());
97         server.setProvStatus("active");
98         server.setRelationshipList(new RelationshipList());
99         server.setVserverId(cbamVnfc.getComputeResource().getResourceId());
100         server.setVserverSelflink(extractSelfLink(cbamVnfc.getComputeResource().getAdditionalData()));
101         addSingletonRelation(server.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
102         if (server.getVolumes() == null) {
103             server.setVolumes(new Volumes());
104         }
105         if (cbamVnfc.getStorageResourceIds() != null) {
106             for (String virtualStorageId : cbamVnfc.getStorageResourceIds()) {
107                 Volume volume = new Volume();
108                 AffectedVirtualStorage affectedStorage = find(affectedVirtualStorages, storage -> virtualStorageId.equals(storage.getId()));
109                 volume.setVolumeId(affectedStorage.getResource().getResourceId());
110                 server.getVolumes().getVolume().add(volume);
111             }
112         } else {
113             server.setVolumes(OBJECT_FACTORY.createVolumes());
114         }
115         aaiRestApiProvider.put(logger, CLOUD, url, server, Void.class);
116     }
117
118     private String extractSelfLink(Object additionalData) {
119         try {
120             JsonObject root = new Gson().toJsonTree(additionalData).getAsJsonObject();
121             for (JsonElement link : childElement(root, "links").getAsJsonArray()) {
122                 if (link.getAsJsonObject().has("rel") && "self".equals(link.getAsJsonObject().get("rel").getAsString())) {
123                     return link.getAsJsonObject().get("href").getAsString();
124                 }
125             }
126             return "unknown";
127         } catch (Exception e) {
128             logger.debug("Missing links in the server", e);
129             return "unknown";
130         }
131     }
132 }