2  * Copyright 2016-2017, Nokia Corporation
 
   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
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;
 
  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.model.VnfInfo;
 
  25 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.vnfm.CatalogManager;
 
  29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManagerForVfc;
 
  30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
 
  31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProviderForVfc;
 
  32 import org.onap.vnfmdriver.model.*;
 
  33 import org.slf4j.Logger;
 
  34 import org.springframework.beans.factory.annotation.Autowired;
 
  35 import org.springframework.stereotype.Component;
 
  36 import org.yaml.snakeyaml.Yaml;
 
  38 import static com.nokia.cbam.lcm.v32.model.InstantiationState.INSTANTIATED;
 
  39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
 
  40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
 
  41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
 
  42 import static org.onap.vnfmdriver.model.OperationType.TERMINAL;
 
  43 import static org.slf4j.LoggerFactory.getLogger;
 
  46  * Responsible for handling granting before the execution of a VNF operation
 
  49 public class VfcGrantManager implements IGrantManager {
 
  50     private static Logger logger = getLogger(VfcGrantManager.class);
 
  51     private final CatalogManager catalogManager;
 
  52     private final CbamRestApiProvider cbamRestApiProvider;
 
  53     private final VfcRestApiProvider vfcRestApiProvider;
 
  56     VfcGrantManager(CatalogManagerForVfc catalogManager, CbamRestApiProviderForVfc cbamRestApiProvider, VfcRestApiProvider vfcRestApiProvider) {
 
  57         this.catalogManager = catalogManager;
 
  58         this.cbamRestApiProvider = cbamRestApiProvider;
 
  59         this.vfcRestApiProvider = vfcRestApiProvider;
 
  63     public void requestGrantForHeal(String vnfmId, String vnfId, String vimId, String onapCsarId, VnfHealRequest request, String jobId) {
 
  64         GrantVNFRequest grantRequest = buildGrantRequest(vnfmId, vimId, onapCsarId, jobId, OperationType.HEAL);
 
  65         ResourceChange resourceChange = new ResourceChange();
 
  66         resourceChange.setType(ChangeType.VDU);
 
  67         resourceChange.setVdu(request.getAffectedvm().getVduid());
 
  68         resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
 
  69         grantRequest.getRemoveResource().add(resourceChange);
 
  70         grantRequest.getAddResource().add(resourceChange);
 
  71         grantRequest.setVnfInstanceId(vnfId);
 
  72         requestGrant(grantRequest);
 
  76     public void requestGrantForScale(String vnfmId, String vnfId, String vimId, String onapCsarId, VnfScaleRequest request, String jobId) {
 
  79             com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
 
  80             cbamVnfdId = vnf.getVnfdId();
 
  81         } catch (Exception e) {
 
  82             throw buildFatalFailure(logger, "Unable to query VNF " + vnfId, e);
 
  84         OperationType operationType = ScaleDirection.IN.equals(request.getType()) ? OperationType.SCALEIN : OperationType.SCALEOUT;
 
  85         GrantVNFRequest grantRequest = buildGrantRequest(vnfmId, vimId, onapCsarId, jobId, operationType);
 
  86         String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, cbamVnfdId);
 
  87         Set<ResourceChange> resourceChanges = calculateResourceChangeDuringScaling(vnfdContent, request.getAspectId(), Integer.parseInt(request.getNumberOfSteps()));
 
  88         if (request.getType() == ScaleDirection.IN) {
 
  89             grantRequest.getRemoveResource().addAll(resourceChanges);
 
  92             grantRequest.getAddResource().addAll(resourceChanges);
 
  94         grantRequest.setVnfInstanceId(vnfId);
 
  95         requestGrant(grantRequest);
 
  99     public void requestGrantForTerminate(String vnfmId, String vnfId, String vimId, String onapVnfdId, VnfInfo vnf, String jobId) {
 
 100         if (vnf.getInstantiationState() == INSTANTIATED) {
 
 101             GrantVNFRequest grantRequest;
 
 103                 grantRequest = buildGrantRequest(vnfmId, vimId, onapVnfdId, jobId, TERMINAL);
 
 104                 grantRequest.setVnfInstanceId(vnfId);
 
 105                 addVnfcsToGrant(vnf, grantRequest);
 
 106             } catch (Exception e) {
 
 107                 throw buildFatalFailure(logger, "Unable to prepare grant request for termination", e);
 
 109             requestGrant(grantRequest);
 
 113     private void addVnfcsToGrant(VnfInfo vnf, GrantVNFRequest grantRequest) {
 
 114         //VNF is instantiated but has no VNFC
 
 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());
 
 127     public GrantVNFResponseVim requestGrantForInstantiate(String vnfmId, String vnfId, String vimId, String onapVnfdId, String instantiationLevelId, String cbamVnfdContent, String jobId) {
 
 128         GrantVNFRequest grantRequest;
 
 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);
 
 137         return requestGrant(grantRequest);
 
 140     private GrantVNFRequest buildGrantRequest(String vnfmId, String vimId, String onapCsarId, String jobId, OperationType operationType) {
 
 141         GrantVNFRequest grantVNFRequest = new GrantVNFRequest();
 
 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;
 
 157     private GrantVNFResponseVim requestGrant(GrantVNFRequest grantRequest) {
 
 159             logger.info("Requesting grant with ", grantRequest);
 
 160             GrantVNFResponse grantVNFResponse = vfcRestApiProvider.getNsLcmApi().grantvnf(grantRequest).blockingFirst();
 
 161             logger.info("Successfully received grant {}", grantVNFResponse);
 
 162             return grantVNFResponse.getVim();
 
 163         } catch (Exception e) {
 
 164             throw buildFatalFailure(logger, "Unable to request grant with " + grantRequest, e);
 
 168     private Set<ResourceChange> calculateResourceChangeDuringInstantiate(String cbamVnfdContent, String instantiationLevelId) {
 
 169         JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfdContent)).getAsJsonObject();
 
 170         JsonObject capabilities = child(child(child(root, "topology_template"), "substitution_mappings"), "capabilities");
 
 171         JsonObject deploymentFlavorProperties = child(child(capabilities, "deployment_flavour"), "properties");
 
 172         JsonObject instantiationLevels = child(deploymentFlavorProperties, "instantiation_levels");
 
 173         Set<ResourceChange> resourceChanges = new HashSet<>();
 
 174         for (Map.Entry<String, JsonElement> vdu_level : child(child(instantiationLevels, instantiationLevelId), ("vdu_levels")).entrySet()) {
 
 175             JsonElement numberOfInstances = vdu_level.getValue().getAsJsonObject().get("number_of_instances");
 
 176             for (int i = 0; i < numberOfInstances.getAsLong(); i++) {
 
 177                 ResourceChange resourceChange = new ResourceChange();
 
 178                 resourceChanges.add(resourceChange);
 
 179                 resourceChange.setVdu(vdu_level.getKey());
 
 180                 resourceChange.setType(ChangeType.VDU);
 
 181                 resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
 
 184         return resourceChanges;
 
 187     private Set<ResourceChange> calculateResourceChangeDuringScaling(String vnfdContent, String aspectId, int steps) {
 
 188         JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
 
 189         Set<ResourceChange> resourceChanges = new HashSet<>();
 
 190         JsonArray policies = child(root, "topology_template").getAsJsonObject().get("policies").getAsJsonArray();
 
 191         for (JsonElement policy : policies) {
 
 192             if ("heat_mapping".equals(policy.getAsJsonObject().entrySet().iterator().next().getKey())) {
 
 193                 JsonObject aspects = policy.getAsJsonObject().entrySet().iterator().next().getValue().getAsJsonObject().get("properties").getAsJsonObject().get("aspects").getAsJsonObject();
 
 194                 JsonObject aspect = child(aspects, aspectId);
 
 195                 if (aspect.has("vdus")) {
 
 196                     addChangesForAspect(steps, resourceChanges, aspect);
 
 200         return resourceChanges;
 
 203     private void addChangesForAspect(int steps, Set<ResourceChange> resourceChanges, JsonObject aspect) {
 
 204         for (Map.Entry<String, JsonElement> vdu : aspect.get("vdus").getAsJsonObject().entrySet()) {
 
 205             String vduId = vdu.getKey();
 
 206             for (int step = 0; step < steps; step++) {
 
 207                 for (int i = 0; i < vdu.getValue().getAsJsonArray().size(); i++) {
 
 208                     ResourceChange resourceChange = new ResourceChange();
 
 209                     resourceChange.setVdu(vduId);
 
 210                     resourceChange.setType(ChangeType.VDU);
 
 211                     resourceChange.setResourceDefinitionId(UUID.randomUUID().toString());
 
 212                     resourceChanges.add(resourceChange);
 
 219      * Represents the mandatory parameters that must be sent during grant request to VF-C
 
 222     static class AdditionalGrantParams {
 
 223         private final String vnfmId;
 
 224         private final String vimId;
 
 226         AdditionalGrantParams(String vnfmId, String vimId) {
 
 227             this.vnfmId = vnfmId;
 
 232          * @return the identifier of the VNFM requesting the grant
 
 234         public String getVnfmId() {
 
 239          * @return the identifier of the VIM for which the grant is requested
 
 241         public String getVimId() {