2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.reception.decoding.pdpx;
 
  23 import java.util.ArrayList;
 
  24 import java.util.List;
 
  26 import java.util.HashMap;
 
  28 import java.io.FileWriter;
 
  29 import java.io.Writer;
 
  31 import java.util.regex.Matcher;
 
  32 import java.util.regex.Pattern;
 
  34 import com.google.gson.Gson;
 
  35 import com.google.gson.GsonBuilder;
 
  36 import com.google.gson.annotations.SerializedName;
 
  38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  39 import org.onap.policy.common.logging.flexlogger.Logger;
 
  40 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  42 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
 
  43 import org.onap.sdc.tosca.parser.impl.SdcCsarHelperImpl;
 
  44 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
 
  45 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
 
  47 import org.onap.sdc.toscaparser.api.NodeTemplate;
 
  48 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
 
  49 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
 
  50 import org.onap.sdc.toscaparser.api.elements.Metadata;
 
  53  * Extract concerned info from NodeTemplate, currently ONLY HPA Feature.
 
  55  * @author Libo Zhu (libo.zhu@intel.com)
 
  57 public class ExtractFromNode {
 
  59     private static final Logger LOGGER = FlexLogger.getLogger(ExtractFromNode.class);
 
  60     private static final String CONTENT_RESOURCES = "name";
 
  61     private static final String VDU_TYPE = "tosca.nodes.nfv.Vdu.Compute";
 
  62     private static final String VDU_CP_TYPE = "tosca.nodes.nfv.VduCp";
 
  63     private static final String VIRTUAL_MEM_SIZE_PATH = "virtual_memory#virtual_mem_size";
 
  64     private static final String NUM_VIRTUAL_CPU_PATH = "virtual_cpu#num_virtual_cpu";
 
  65     private static final String CPU_ARCHITECTURE_PATH = "virtual_cpu#cpu_architecture";
 
  66     private static final String NUMBER_OF_PAGES_PATH = "virtual_memory#vdu_memory_requirements#numberOfPages";
 
  67     private static final String BASIC_CAPABILITIES = "BasicCapabilities";
 
  70     ISdcCsarHelper sdcCsarHelper;
 
  71     final Gson gson = new GsonBuilder()
 
  74                             .disableHtmlEscaping()
 
  78     public void setSdcCsarHelper(ISdcCsarHelper sdcCsarHelper) {
 
  79         this.sdcCsarHelper = sdcCsarHelper;
 
  83      * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated PdpxPolicy 
 
  84      * if it has got Hpa feature info or else return null.
 
  86      * @param node the NodeTemplate
 
  87      * @return the extracted info from input node
 
  88      * @throws PolicyDecodingException if extract fails
 
  90     public PdpxPolicy extractInfo(NodeTemplate node) throws PolicyDecodingException {
 
  91         PdpxPolicy pdpxPolicy = new PdpxPolicy();
 
  92         Content content = pdpxPolicy.getContent();
 
  94         String outputFile = sdcCsarHelper.getNodeTemplateMetadata(node).getValue("name");
 
  95         outputFile += ".json";
 
  96         LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(node));
 
  97         LOGGER.debug("outputFile = " + outputFile);
 
  99         List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node);
 
 100         LOGGER.debug("the size of lnodeChild = " + lnodeChild.size());
 
 102         //Store all the VDUs under one VNF
 
 103         List<NodeTemplate> lnodeVdu = new ArrayList<>();
 
 104         //Store all the Cps under one VNF
 
 105         List<NodeTemplate> lnodeVduCp = new ArrayList<>();
 
 106         for ( NodeTemplate nodeChild : lnodeChild) {
 
 107             String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild);
 
 108             LOGGER.debug("the type of this nodeChild = " + type);
 
 109             LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild));
 
 110             if ( type.equalsIgnoreCase(VDU_TYPE)) {
 
 111                 lnodeVdu.add(nodeChild);
 
 112             } else if ( type.equalsIgnoreCase(VDU_CP_TYPE)) {
 
 113                 lnodeVduCp.add(nodeChild);
 
 117         LOGGER.debug("the size of vdu is =" + lnodeVdu.size());
 
 118         LOGGER.debug("the size of cp is =" + lnodeVduCp.size());
 
 120         extractInfoVdu(lnodeVdu, content);
 
 121         extractInfoVduCp(lnodeVduCp, content);
 
 123         if (content.getFlavorFeatures().isEmpty() ){
 
 127         try (Writer writer = new FileWriter(outputFile)) {
 
 128             gson.toJson(pdpxPolicy, writer);
 
 129         } catch (Exception e) {
 
 130             LOGGER.error("can't write generated policies to file " , e);
 
 131             throw new PolicyDecodingException ("Exception caught when writing generated policies to file ", e);
 
 138      * ExtractInfofromVdu, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute.
 
 140      * @param lnodeVdu the list of Vdu node
 
 141      * @param content to be change based on lnodeVdu
 
 143     public void extractInfoVdu(final List<NodeTemplate> lnodeVdu, Content content) {
 
 144         //each VDU <=> FlavorFeature
 
 145         for ( NodeTemplate node : lnodeVdu) {
 
 146             String id = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, "name");
 
 147             FlavorFeature flavorFeature = new FlavorFeature();
 
 148             flavorFeature.setId(id);
 
 149             Attribute flavorAttribute = new Attribute();
 
 150             flavorAttribute.setAttributeName("flavorName");
 
 151             flavorAttribute.setAttributeValue("");
 
 152             Directive flavorDirective = new Directive();
 
 153             flavorDirective.setType("flavor_directive");
 
 154             flavorDirective.getAttributes().add(flavorAttribute);
 
 155             flavorFeature.getDirectives().add(flavorDirective);
 
 157             CapabilityAssignments capabilityAssignments = sdcCsarHelper.getCapabilitiesOf(node);
 
 158             CapabilityAssignment capabilityAssignment = capabilityAssignments.getCapabilityByName("virtual_compute");
 
 159             if (capabilityAssignment != null) {
 
 160                 generateBasicCapability(capabilityAssignment, flavorFeature);
 
 161                 generateHugePages(capabilityAssignment, flavorFeature);
 
 164             content.getFlavorFeatures().add(flavorFeature);  
 
 169      * GenerateBasicCapability, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute.
 
 171      * @param capabilityAssignment represents the capability of node
 
 172      * @param flavorFeature represents all the features of specified flavor
 
 174     private void generateBasicCapability(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){
 
 175             //the format is xxx MB/GB like 4096 MB
 
 176             String virtualMemSize = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment,
 
 177                 VIRTUAL_MEM_SIZE_PATH);
 
 178             if (virtualMemSize != null) {
 
 179                 LOGGER.debug("the virtualMemSize = " + virtualMemSize);
 
 180                 HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("virtualMemSize", virtualMemSize);
 
 181                 FlavorProperty flavorProperty = new FlavorProperty();
 
 182                 flavorProperty.setHpaFeature(BASIC_CAPABILITIES);
 
 183                 flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 184                 flavorFeature.getFlavorProperties().add(flavorProperty);
 
 187             //the format is int like 2 
 
 188             String numVirtualCpu = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment,
 
 189                 NUM_VIRTUAL_CPU_PATH);
 
 190             if (numVirtualCpu != null) {
 
 191                 LOGGER.debug("the numVirtualCpu = " + numVirtualCpu);
 
 192                 HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu);
 
 193                 String cpuArchitecture = sdcCsarHelper.getCapabilityPropertyLeafValue
 
 194                         (capabilityAssignment,CPU_ARCHITECTURE_PATH);
 
 195                 FlavorProperty flavorProperty = new FlavorProperty();
 
 196                 flavorProperty.setHpaFeature(BASIC_CAPABILITIES);
 
 197                 if (cpuArchitecture != null) {
 
 198                     flavorProperty.setArchitecture(cpuArchitecture);
 
 200                 flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 201                 flavorFeature.getFlavorProperties().add(flavorProperty);
 
 206      * GenerateHpaFeatureAttribute based on the value of featureValue.
 
 207      * the format: "hpa-attribute-key": "pciVendorId", "hpa-attribute-value": "1234", "operator": "=", "unit": "xxx".
 
 209      * @param hpaAttributeKey get from the high layer tosca DM
 
 210      * @param featureValue get from the high layer tosca DM
 
 211      * @return the format used in underlayer component
 
 213     private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue){
 
 215         HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute();
 
 216         hpaFeatureAttribute.setHpaAttributeKey(hpaAttributeKey);
 
 217         String tmp = featureValue.replace(" ","");
 
 218         String pattern = "(\\D*)(\\d+)(\\D*)";
 
 219         Pattern r = Pattern.compile(pattern);
 
 220         Matcher m = r.matcher(tmp);
 
 222             LOGGER.debug("operator = " + m.group(1));
 
 223             LOGGER.debug("value = " + m.group(2));
 
 224             LOGGER.debug("unit = " + m.group(3));
 
 225             hpaFeatureAttribute.setOperator(m.group(1));
 
 226             hpaFeatureAttribute.setHpaAttributeValue(m.group(2));
 
 227             hpaFeatureAttribute.setUnit(m.group(3));
 
 229         return hpaFeatureAttribute;
 
 233      * GenerateHugePages, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute.
 
 235      * @param capabilityAssignment represents the capability of node
 
 236      * @param flavorFeature represents all the features of specified flavor
 
 238     private void generateHugePages(final CapabilityAssignment capabilityAssignment, FlavorFeature flavorFeature){
 
 239             //the format is a map like: {"schema-version": "0", "schema-location": "", "platform-id": "generic", 
 
 240             // "mandatory": true, "configuration-value": "2 MB"}
 
 241             String numberOfPages = sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment,
 
 242                 NUMBER_OF_PAGES_PATH);
 
 243             if (numberOfPages != null) {
 
 244                 LOGGER.debug("the virtualMemSize = " + numberOfPages);
 
 245             //TODO add HugePages support
 
 250      * ExtractInfoVduCp, supposted hpa features, under the virtual_network_interface_requirements of
 
 251      * tosca.nodes.nfv.VduCp. 
 
 253      * @param lnodeVduCp the list of VduCp node
 
 254      * @param content to be change based on lnodeVduCp
 
 256     @SuppressWarnings("unchecked")
 
 257     public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, Content content) {
 
 258         for ( NodeTemplate node : lnodeVduCp) {
 
 259         //TODO to add VDU cp Hpa feature extract