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 com.google.common.collect.ImmutableMap;
 
  24 import com.google.gson.Gson;
 
  25 import com.google.gson.GsonBuilder;
 
  26 import com.google.gson.reflect.TypeToken;
 
  28 import java.util.ArrayList;
 
  29 import java.util.HashMap;
 
  30 import java.util.List;
 
  32 import java.util.regex.Matcher;
 
  33 import java.util.regex.Pattern;
 
  35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  36 import org.onap.policy.common.logging.flexlogger.Logger;
 
  37 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  38 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
 
  39 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
 
  40 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
 
  41 import org.onap.sdc.toscaparser.api.NodeTemplate;
 
  42 import org.onap.sdc.toscaparser.api.RequirementAssignment;
 
  45  * Extract concerned info from NodeTemplate, currently ONLY HPA Feature.
 
  47  * @author Libo Zhu (libo.zhu@intel.com)
 
  49 public class ExtractFromNode {
 
  51     private static final String CONFIGURATION_VALUE = "configurationValue";
 
  52     private static final Logger LOGGER = FlexLogger.getLogger(ExtractFromNode.class);
 
  53     private static final String VDU_TYPE = "tosca.nodes.nfv.Vdu.Compute";
 
  54     private static final String VDU_CP_TYPE = "tosca.nodes.nfv.VduCp";
 
  55     private static final String VIRTUAL_MEM_SIZE_PATH = "virtual_memory#virtual_mem_size";
 
  56     private static final String NUM_VIRTUAL_CPU_PATH = "virtual_cpu#num_virtual_cpu";
 
  57     private static final String CPU_ARCHITECTURE_PATH = "virtual_cpu#cpu_architecture";
 
  58     private static final String MEMORY_PAGE_SIZE_PATH = "virtual_memory#vdu_memory_requirements#memoryPageSize";
 
  59     private static final String NETWORK_INTERFACE_TYPE_PATH =
 
  60             "virtual_network_interface_requirements#network_interface_requirements#interfaceType";
 
  61     private static final String NETWORK_PCI_PATH = "virtual_network_interface_requirements#nic_io_requirements";
 
  62     private static final String BASIC_CAPABILITIES_HPA_FEATURE = "BasicCapabilities";
 
  63     private static final String HUGE_PAGES_HPA_FEATURE = "hugePages";
 
  64     private static final Map<String, String> NETWORK_HPA_FEATURE_MAP =
 
  65             ImmutableMap.of("SR-IOV", "SriovNICNetwork", "PCI-Passthrough", "pciePassthrough");
 
  67     private ISdcCsarHelper sdcCsarHelper;
 
  68     final Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create();
 
  70     public void setSdcCsarHelper(final ISdcCsarHelper sdcCsarHelper) {
 
  71         this.sdcCsarHelper = sdcCsarHelper;
 
  75      * ExtractInfo from VNF , each VNF may includes more than one VDUs and CPs return new generated PdpxPolicy if it has
 
  76      * got Hpa feature info or else return null.
 
  78      * @param node the NodeTemplate
 
  80      * @return the extracted info from input node
 
  82      * @throws PolicyDecodingException if extract fails
 
  84     public Content extractInfo(final NodeTemplate node) throws PolicyDecodingException {
 
  86         LOGGER.debug("the meta data of this nodetemplate = " + sdcCsarHelper.getNodeTemplateMetadata(node));
 
  87         final List<NodeTemplate> lnodeChild = sdcCsarHelper.getNodeTemplateChildren(node);
 
  88         LOGGER.debug("the size of lnodeChild = " + lnodeChild.size());
 
  90         // Store all the VDUs under one VNF
 
  91         final List<NodeTemplate> lnodeVdu = new ArrayList<>();
 
  92         // Store all the Cps under one VNF
 
  93         final List<NodeTemplate> lnodeVduCp = new ArrayList<>();
 
  94         for (final NodeTemplate nodeChild : lnodeChild) {
 
  95             final String type = sdcCsarHelper.getTypeOfNodeTemplate(nodeChild);
 
  96             LOGGER.debug("the type of this nodeChild = " + type);
 
  97             LOGGER.debug("the meta data of this nodeChild = " + sdcCsarHelper.getNodeTemplateMetadata(nodeChild));
 
  98             if (type.equalsIgnoreCase(VDU_TYPE)) {
 
  99                 lnodeVdu.add(nodeChild);
 
 100             } else if (type.equalsIgnoreCase(VDU_CP_TYPE)) {
 
 101                 lnodeVduCp.add(nodeChild);
 
 104         LOGGER.debug("the size of vdu is =" + lnodeVdu.size());
 
 105         LOGGER.debug("the size of cp is =" + lnodeVduCp.size());
 
 107         final Content content = new Content();
 
 108         extractInfoVdu(lnodeVdu, content);
 
 109         extractInfoVduCp(lnodeVduCp, content);
 
 110         if (content.getFlavorFeatures().isEmpty()) {
 
 118      * ExtractInfofromVdu, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute.
 
 120      * @param lnodeVdu the list of Vdu node
 
 122      * @param content to be change based on lnodeVdu
 
 124     public void extractInfoVdu(final List<NodeTemplate> lnodeVdu, final Content content) {
 
 125         // each VDU <=> FlavorFeature
 
 126         for (final NodeTemplate node : lnodeVdu) {
 
 127             final Attribute flavorAttribute = new Attribute();
 
 128             flavorAttribute.setAttributeName("flavorName");
 
 129             flavorAttribute.setAttributeValue("");
 
 130             final Directive flavorDirective = new Directive();
 
 131             flavorDirective.setType("flavor_directive");
 
 132             flavorDirective.getAttributes().add(flavorAttribute);
 
 133             final FlavorFeature flavorFeature = new FlavorFeature();
 
 134             flavorFeature.setId(sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, "name"));
 
 135             flavorFeature.getDirectives().add(flavorDirective);
 
 137             final CapabilityAssignments capabilityAssignments = sdcCsarHelper.getCapabilitiesOf(node);
 
 138             final CapabilityAssignment capabilityAssignment =
 
 139                     capabilityAssignments.getCapabilityByName("virtual_compute");
 
 140             if (capabilityAssignment != null) {
 
 141                 generateBasicCapability(capabilityAssignment, flavorFeature);
 
 142                 generateHugePages(capabilityAssignment, flavorFeature);
 
 144             content.getFlavorFeatures().add(flavorFeature);
 
 149      * GenerateBasicCapability, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute.
 
 151      * @param capabilityAssignment represents the capability of node
 
 153      * @param flavorFeature represents all the features of specified flavor
 
 155     private void generateBasicCapability(final CapabilityAssignment capabilityAssignment,
 
 156             final FlavorFeature flavorFeature) {
 
 157         // the format is xxx MB/GB like 4096 MB
 
 158         final String virtualMemSize =
 
 159                 sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, VIRTUAL_MEM_SIZE_PATH);
 
 160         if (virtualMemSize != null) {
 
 161             LOGGER.debug("the virtualMemSize = " + virtualMemSize);
 
 162             final HpaFeatureAttribute hpaFeatureAttribute =
 
 163                     generateHpaFeatureAttribute("virtualMemSize", virtualMemSize);
 
 164             final FlavorProperty flavorProperty = new FlavorProperty();
 
 165             flavorProperty.setHpaFeature(BASIC_CAPABILITIES_HPA_FEATURE);
 
 166             flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 167             flavorFeature.getFlavorProperties().add(flavorProperty);
 
 170         // the format is int like 2
 
 171         final String numVirtualCpu =
 
 172                 sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, NUM_VIRTUAL_CPU_PATH);
 
 173         if (numVirtualCpu != null) {
 
 174             LOGGER.debug("the numVirtualCpu = " + numVirtualCpu);
 
 175             final HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute("numVirtualCpu", numVirtualCpu);
 
 176             final String cpuArchitecture =
 
 177                     sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, CPU_ARCHITECTURE_PATH);
 
 178             final FlavorProperty flavorProperty = new FlavorProperty();
 
 179             flavorProperty.setHpaFeature(BASIC_CAPABILITIES_HPA_FEATURE);
 
 180             if (cpuArchitecture != null) {
 
 181                 flavorProperty.setArchitecture(cpuArchitecture);
 
 183             flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 184             flavorFeature.getFlavorProperties().add(flavorProperty);
 
 189      * GenerateHpaFeatureAttribute based on the value of featureValue. the format: "hpa-attribute-key": "pciVendorId",
 
 190      * "hpa-attribute-value": "1234", "operator": "=", "unit": "xxx".
 
 192      * @param hpaAttributeKey get from the high layer tosca DM
 
 194      * @param featureValue get from the high layer tosca DM
 
 197     private HpaFeatureAttribute generateHpaFeatureAttribute(final String hpaAttributeKey, final String featureValue) {
 
 198         // based on input featureValue, return back a suitable hpaFeatureAttribute
 
 199         final HpaFeatureAttribute hpaFeatureAttribute = new HpaFeatureAttribute();
 
 200         hpaFeatureAttribute.setHpaAttributeKey(hpaAttributeKey);
 
 201         final String tmp = featureValue.replace(" ", "");
 
 202         final String pattern = "(\\D*)(\\d+)(\\D*)";
 
 203         final Pattern r = Pattern.compile(pattern);
 
 204         final Matcher m = r.matcher(tmp);
 
 206             LOGGER.debug("operator = " + m.group(1));
 
 207             LOGGER.debug("value = " + m.group(2));
 
 208             LOGGER.debug("unit = " + m.group(3));
 
 209             hpaFeatureAttribute.setOperator(m.group(1));
 
 210             hpaFeatureAttribute.setHpaAttributeValue(m.group(2));
 
 211             hpaFeatureAttribute.setUnit(m.group(3));
 
 213         return hpaFeatureAttribute;
 
 217      * GenerateHugePages, supported hpa features, All under the capability of tosca.nodes.nfv.Vdu.Compute. The format is
 
 218      * a map like: {"schemaVersion": "0", "schemaSelector": "", "hardwarePlatform": "generic", "mandatory": "true",
 
 219      * "configurationValue": "2 MB"}
 
 221      * @param capabilityAssignment represents the capability of node
 
 223      * @param flavorFeature represents all the features of specified flavor
 
 225     private void generateHugePages(final CapabilityAssignment capabilityAssignment, final FlavorFeature flavorFeature) {
 
 226         final String memoryPageSize =
 
 227                 sdcCsarHelper.getCapabilityPropertyLeafValue(capabilityAssignment, MEMORY_PAGE_SIZE_PATH);
 
 228         LOGGER.debug("the memoryPageSize = " + memoryPageSize);
 
 229         if (memoryPageSize != null) {
 
 230             final Map<String, String> retMap =
 
 231                     gson.fromJson(memoryPageSize, new TypeToken<HashMap<String, String>>() {}.getType());
 
 232             LOGGER.debug("the retMap = " + retMap);
 
 233             final String memoryPageSizeValue = retMap.get(CONFIGURATION_VALUE);
 
 234             final String mandatory = retMap.get("mandatory");
 
 235             if (memoryPageSizeValue == null) {
 
 238             final HpaFeatureAttribute hpaFeatureAttribute =
 
 239                     generateHpaFeatureAttribute("memoryPageSize", memoryPageSizeValue);
 
 240             final FlavorProperty flavorProperty = new FlavorProperty();
 
 241             flavorProperty.setHpaFeature(HUGE_PAGES_HPA_FEATURE);
 
 242             if (mandatory != null) {
 
 243                 flavorProperty.setMandatory(mandatory);
 
 245             flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 246             flavorFeature.getFlavorProperties().add(flavorProperty);
 
 251      * ExtractInfoVduCp, supported hpa features, under the virtual_network_interface_requirements of
 
 252      * tosca.nodes.nfv.VduCp.
 
 254      * @param lnodeVduCp the list of VduCp node
 
 256      * @param content to be change based on lnodeVduCp
 
 257      * @throws PolicyDecodingException if extract CP fails
 
 259     public void extractInfoVduCp(final List<NodeTemplate> lnodeVduCp, final Content content)
 
 260             throws PolicyDecodingException {
 
 261         // each CP will binds to a VDU so need the vdu flavor map info.
 
 262         final Map<String, FlavorFeature> vduFlavorMap = new HashMap<>();
 
 263         for (final FlavorFeature flavorFeature : content.getFlavorFeatures()) {
 
 264             LOGGER.debug("the id = " + flavorFeature.getId());
 
 265             vduFlavorMap.put(flavorFeature.getId(), flavorFeature);
 
 267         for (final NodeTemplate node : lnodeVduCp) {
 
 268             final String interfaceType =
 
 269                     sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, NETWORK_INTERFACE_TYPE_PATH);
 
 270             LOGGER.debug("the interfaceType = " + interfaceType);
 
 271             Map<String, Object> retMap = new HashMap<>();
 
 272             if (interfaceType != null) {
 
 273                 retMap = gson.fromJson(interfaceType, new TypeToken<HashMap<String, Object>>() {}.getType());
 
 274                 LOGGER.debug("the retMap = " + retMap);
 
 277             String networkHpaFeature;
 
 278             if (retMap.containsKey(CONFIGURATION_VALUE)) {
 
 279                 final String interfaceTypeValue = retMap.get(CONFIGURATION_VALUE).toString();
 
 280                 LOGGER.debug(" the interfacetype value is =" + interfaceTypeValue);
 
 281                 if (NETWORK_HPA_FEATURE_MAP.containsKey(interfaceTypeValue)) {
 
 282                     networkHpaFeature = NETWORK_HPA_FEATURE_MAP.get(interfaceTypeValue);
 
 283                     LOGGER.debug(" the networkHpaFeature is =" + networkHpaFeature);
 
 285                     LOGGER.debug(" unspported network interface ");
 
 289                 LOGGER.debug(" no configurationValue defined in interfaceType");
 
 293             for (final RequirementAssignment requriement : sdcCsarHelper.getRequirementsOf(node).getAll()) {
 
 294                 final String nodeTemplateName = requriement.getNodeTemplateName().toLowerCase();
 
 295                 LOGGER.debug("getNodeTemplateName =" + nodeTemplateName);
 
 296                 if (nodeTemplateName == null) {
 
 299                 if (!vduFlavorMap.containsKey(nodeTemplateName)) {
 
 300                     throw new PolicyDecodingException("vdu Flavor Map should contains the key " + nodeTemplateName);
 
 302                 generateNetworkFeature(networkHpaFeature, node, vduFlavorMap.get(nodeTemplateName));
 
 308      * GenerateNetworkFeature, all pci feature are grouped into FlavorFeature together. The format is a map like:
 
 309      * {"schemaVersion": "0", "schemaSelector": "", "hardwarePlatform": "generic", "mandatory": "true",
 
 310      * "configurationValue": "2 MB"}
 
 312      * @param networkHpaFeature represents the specified Hpa feature
 
 313      * @param node represents the CP Node
 
 314      * @param flavorFeature represents all the features of specified flavor
 
 316     private void generateNetworkFeature(final String networkHpaFeature, final NodeTemplate node,
 
 317             final FlavorFeature flavorFeature) {
 
 318         final FlavorProperty flavorProperty = new FlavorProperty();
 
 319         flavorProperty.setHpaFeature(networkHpaFeature);
 
 320         final String[] pciKeys = { "pciVendorId", "pciDeviceId", "pciNumDevices", "physicalNetwork" };
 
 321         for (final String pciKey : pciKeys) {
 
 322             LOGGER.debug("the pciKey = " + pciKey);
 
 323             final String pciKeyPath = NETWORK_PCI_PATH + "#" + pciKey;
 
 324             final String pciValue = sdcCsarHelper.getNodeTemplatePropertyLeafValue(node, pciKeyPath);
 
 325             if (pciValue != null) {
 
 326                 LOGGER.debug("the pciValue = " + pciValue);
 
 327                 final Map<String, String> retMap =
 
 328                         gson.fromJson(pciValue, new TypeToken<HashMap<String, String>>() {}.getType());
 
 329                 final String pciConfigValue = retMap.get(CONFIGURATION_VALUE);
 
 330                 if (pciConfigValue == null) {
 
 333                 final HpaFeatureAttribute hpaFeatureAttribute = generateHpaFeatureAttribute(pciKey, pciConfigValue);
 
 334                 flavorProperty.getHpaFeatureAttributes().add(hpaFeatureAttribute);
 
 337         flavorFeature.getFlavorProperties().add(flavorProperty);