f283672ee838f76e4b5dfa9591fc4111575af737
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / vfc / VfcGrantManager.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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.nokia.cbam.lcm.v32.ApiException;
25 import com.nokia.cbam.lcm.v32.model.VnfInfo;
26 import com.nokia.cbam.lcm.v32.model.VnfcResourceInfo;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IGrantManager;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
32 import org.onap.vnfmdriver.model.*;
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 import org.yaml.snakeyaml.Yaml;
38
39 import java.util.*;
40
41 import static com.nokia.cbam.lcm.v32.model.InstantiationState.INSTANTIATED;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
43 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
44 import static org.onap.vnfmdriver.model.OperationType.TERMINAL;
45 import static org.slf4j.LoggerFactory.getLogger;
46
47 /**
48  * Responsible for handling granting before the execution of a VNF operation
49  */
50 @Component
51 @Conditional(value = Conditions.UseForVfc.class)
52 public class VfcGrantManager implements IGrantManager {
53     private static Logger logger = getLogger(VfcGrantManager.class);
54     private final CatalogManager catalogManager;
55     private final CbamRestApiProvider cbamRestApiProvider;
56     private final VfcRestApiProvider vfcRestApiProvider;
57
58     @Autowired
59     VfcGrantManager(CatalogManager catalogManager, CbamRestApiProvider cbamRestApiProvider, VfcRestApiProvider vfcRestApiProvider) {
60         this.catalogManager = catalogManager;
61         this.cbamRestApiProvider = cbamRestApiProvider;
62         this.vfcRestApiProvider = vfcRestApiProvider;
63     }
64
65     @Override
66     public void requestGrantForHeal(String vnfmId, String vnfId, String vimId, String onapCsarId, VnfHealRequest request, String jobId) {
67         GrantVNFRequest grantRequest = buildGrantRequest(vnfmId, vimId, onapCsarId, jobId, OperationType.HEAL);
68         ResourceChange resourceChange = new ResourceChange();
69         resourceChange.setType(ChangeType.VDU);
70         resourceChange.setVdu(request.getAffectedvm().getVduid());
71         resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
72         grantRequest.getRemoveResource().add(resourceChange);
73         grantRequest.getAddResource().add(resourceChange);
74         grantRequest.setVnfInstanceId(vnfId);
75         requestGrant(grantRequest);
76     }
77
78     @Override
79     public void requestGrantForScale(String vnfmId, String vnfId, String vimId, String onapCsarId, VnfScaleRequest request, String jobId) {
80         try {
81             OperationType operationType = ScaleDirection.IN.equals(request.getType()) ? OperationType.SCALEIN : OperationType.SCALEOUT;
82             GrantVNFRequest grantRequest = buildGrantRequest(vnfmId, vimId, onapCsarId, jobId, operationType);
83             com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION);
84             String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, vnf.getVnfdId());
85             Set<ResourceChange> resourceChanges = calculateResourceChangeDuringScaling(vnfdContent, request.getAspectId(), Integer.parseInt(request.getNumberOfSteps()));
86             if (request.getType() == ScaleDirection.IN) {
87                 grantRequest.getRemoveResource().addAll(resourceChanges);
88
89             } else {
90                 grantRequest.getAddResource().addAll(resourceChanges);
91             }
92             grantRequest.setVnfInstanceId(vnfId);
93             requestGrant(grantRequest);
94         } catch (ApiException e) {
95             throw buildFatalFailure(logger, "Unable to query VNF " + vnfId, e);
96         }
97     }
98
99     @Override
100     public void requestGrantForTerminate(String vnfmId, String vnfId, String vimId, String onapVnfdId, VnfInfo vnf, String jobId) {
101         if (vnf.getInstantiationState() == INSTANTIATED) {
102             GrantVNFRequest grantRequest;
103             try {
104                 grantRequest = buildGrantRequest(vnfmId, vimId, onapVnfdId, jobId, TERMINAL);
105                 grantRequest.setVnfInstanceId(vnfId);
106                 addVnfsToGrant(vnf, grantRequest);
107             } catch (Exception e) {
108                 throw buildFatalFailure(logger, "Unable to prepare grant request for termination", e);
109             }
110             requestGrant(grantRequest);
111         }
112     }
113
114     private void addVnfsToGrant(VnfInfo vnf, GrantVNFRequest grantRequest) {
115         if (vnf.getInstantiatedVnfInfo().getVnfcResourceInfo() != null) {
116             for (VnfcResourceInfo vnfc : vnf.getInstantiatedVnfInfo().getVnfcResourceInfo()) {
117                 ResourceChange resourceChange = new ResourceChange();
118                 grantRequest.getRemoveResource().add(resourceChange);
119                 resourceChange.setVdu(vnfc.getVduId());
120                 resourceChange.setType(ChangeType.VDU);
121                 resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
122             }
123         }
124     }
125
126     @Override
127     public GrantVNFResponseVim requestGrantForInstantiate(String vnfmId, String vnfId, String vimId, String onapVnfdId, String instantiationLevelId, String cbamVnfdContent, String jobId) {
128         GrantVNFRequest grantRequest;
129         try {
130             grantRequest = buildGrantRequest(vnfmId, vimId, onapVnfdId, jobId, OperationType.INSTANTIATE);
131             grantRequest.setVnfInstanceId(vnfId);
132             grantRequest.setAddResource(new ArrayList<>());
133             grantRequest.getAddResource().addAll(calculateResourceChangeDuringInstantiate(cbamVnfdContent, instantiationLevelId));
134         } catch (Exception e) {
135             throw buildFatalFailure(logger, "Unable to prepare grant request for instantiation", e);
136         }
137         return requestGrant(grantRequest);
138     }
139
140     private GrantVNFRequest buildGrantRequest(String vnfmId, String vimId, String onapCsarId, String jobId, OperationType operationType) {
141         GrantVNFRequest grantVNFRequest = new GrantVNFRequest();
142         //FIXME
143         //Currently the grant request sent to VF-C must contain the VIM identifier in the
144         //grant response (normally in ETSI VIM identifier is received in the grant response
145         //from ETSI orchestrator the vimId parameter should be removed from this POJO
146         //to be able to fix this https://jira.onap.org/browse/VFC-603 must be solved
147         //the vimId should be removed from the AdditionalGrantParams structure
148         grantVNFRequest.setAdditionalParam(new AdditionalGrantParams(vnfmId, vimId));
149         grantVNFRequest.setVnfDescriptorId(onapCsarId);
150         grantVNFRequest.setJobId(jobId);
151         grantVNFRequest.setLifecycleOperation(operationType);
152         grantVNFRequest.setAddResource(new ArrayList<>());
153         grantVNFRequest.setRemoveResource(new ArrayList<>());
154         return grantVNFRequest;
155     }
156
157     private GrantVNFResponseVim requestGrant(GrantVNFRequest grantRequest) {
158         try {
159             return vfcRestApiProvider.getNsLcmApi().grantvnf(grantRequest).getVim();
160         } catch (org.onap.vnfmdriver.ApiException e) {
161             throw buildFatalFailure(logger, "Unable to request grant", e);
162         }
163     }
164
165     private Set<ResourceChange> calculateResourceChangeDuringInstantiate(String vnfdContent, String instantiationLevelId) {
166         JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
167         JsonObject capabilities = CbamUtils.child(CbamUtils.child(CbamUtils.child(root, "topology_template"), "substitution_mappings"), "capabilities");
168         JsonObject deploymentFlavorProperties = CbamUtils.child(CbamUtils.child(capabilities, "deployment_flavour"), "properties");
169         JsonObject instantiationLevels = CbamUtils.child(deploymentFlavorProperties, "instantiation_levels");
170         Set<ResourceChange> resourceChanges = new HashSet<>();
171         for (Map.Entry<String, JsonElement> vdu_level : CbamUtils.child(CbamUtils.child(instantiationLevels, instantiationLevelId), ("vdu_levels")).entrySet()) {
172             JsonElement numberOfInstances = vdu_level.getValue().getAsJsonObject().get("number_of_instances");
173             for (int i = 0; i < numberOfInstances.getAsLong(); i++) {
174                 ResourceChange resourceChange = new ResourceChange();
175                 resourceChanges.add(resourceChange);
176                 resourceChange.setVdu(vdu_level.getKey());
177                 resourceChange.setType(ChangeType.VDU);
178                 resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
179             }
180         }
181         return resourceChanges;
182     }
183
184     private Set<ResourceChange> calculateResourceChangeDuringScaling(String vnfdContent, String aspectId, int steps) {
185         JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
186         Set<ResourceChange> resourceChanges = new HashSet<>();
187         JsonArray policies = CbamUtils.child(root, "topology_template").getAsJsonObject().get("policies").getAsJsonArray();
188         for (JsonElement policy : policies) {
189             if ("heat_mapping".equals(policy.getAsJsonObject().entrySet().iterator().next().getKey())) {
190                 JsonObject aspects = policy.getAsJsonObject().entrySet().iterator().next().getValue().getAsJsonObject().get("properties").getAsJsonObject().get("aspects").getAsJsonObject();
191                 JsonObject aspect = aspects.get(aspectId).getAsJsonObject();
192                 if (aspect.has("vdus")) {
193                     addChangesForAspect(steps, resourceChanges, aspect);
194                 }
195             }
196         }
197         return resourceChanges;
198     }
199
200     private void addChangesForAspect(int steps, Set<ResourceChange> resourceChanges, JsonObject aspect) {
201         for (Map.Entry<String, JsonElement> vdu : aspect.get("vdus").getAsJsonObject().entrySet()) {
202             String vduId = vdu.getKey();
203             for (int step = 0; step < steps; step++) {
204                 for (int i = 0; i < vdu.getValue().getAsJsonArray().size(); i++) {
205                     ResourceChange resourceChange = new ResourceChange();
206                     resourceChange.setVdu(vduId);
207                     resourceChange.setType(ChangeType.VDU);
208                     resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
209                     resourceChanges.add(resourceChange);
210                 }
211             }
212         }
213     }
214
215     /**
216      * Represents the mandatory parameters that must be sent during grant request to VF-C
217      */
218     @VisibleForTesting
219     static class AdditionalGrantParams {
220         private final String vnfmId;
221         private final String vimId;
222
223         AdditionalGrantParams(String vnfmId, String vimId) {
224             this.vnfmId = vnfmId;
225             this.vimId = vimId;
226         }
227
228         /**
229          * @return the identifier of the VNFM requesting the grant
230          */
231         public String getVnfmId() {
232             return vnfmId;
233         }
234
235         /**
236          * @return the identifier of the VIM for which the grant is requested
237          */
238         public String getVimId() {
239             return vimId;
240         }
241     }
242 }