2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.asdc.parser;
23 import static org.apache.commons.lang3.StringUtils.equalsAnyIgnoreCase;
24 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
25 import static org.apache.commons.lang3.StringUtils.isEmpty;
27 import com.google.common.collect.ImmutableMap;
29 import org.apache.commons.collections.MapUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.jetbrains.annotations.Nullable;
32 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
33 import org.onap.sdc.toscaparser.api.NodeTemplate;
34 import org.onap.sdc.toscaparser.api.elements.Metadata;
35 import org.onap.vid.model.ServiceModel;
36 import org.onap.vid.model.VidNotions;
37 import org.onap.vid.properties.Features;
38 import org.togglz.core.manager.FeatureManager;
40 public class VidNotionsBuilder {
42 private final FeatureManager featureManager;
44 //map of service type that are always macro services, and their relevant featureFlag
45 private static final Map<VidNotions.ModelCategory, Features> macroServicesByModelCategory = ImmutableMap.of(
46 VidNotions.ModelCategory.INFRASTRUCTURE_VPN, Features.FLAG_1908_INFRASTRUCTURE_VPN,
47 VidNotions.ModelCategory.Transport, Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI,
48 VidNotions.ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE, Features.FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI
51 public VidNotionsBuilder(FeatureManager featureManager) {
52 this.featureManager = featureManager;
55 VidNotions buildVidNotions(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
56 VidNotions.ModelCategory modelCategory = suggestModelCategory(csarHelper, serviceModel);
57 return new VidNotions(
58 suggestInstantiationUI(csarHelper, serviceModel),
60 suggestViewEditUI(csarHelper, serviceModel),
61 suggestInstantiationType(serviceModel, modelCategory));
64 private boolean isMacroTypeByModelCategory(VidNotions.ModelCategory modelCategory) {
65 Features featureOfMacroType = macroServicesByModelCategory.get(modelCategory);
66 //if featureOfMacroType is null this service is not a macro by its type
67 return (featureOfMacroType!=null && featureManager.isActive(featureOfMacroType));
70 VidNotions.InstantiationType suggestInstantiationType(ServiceModel serviceModel, VidNotions.ModelCategory modelCategory) {
71 if (isMacroTypeByModelCategory(modelCategory)) {
72 return VidNotions.InstantiationType.Macro;
74 if (serviceModel==null || serviceModel.getService()==null || isEmpty(serviceModel.getService().getInstantiationType())) {
75 return VidNotions.InstantiationType.ClientConfig;
77 String instantiationType = serviceModel.getService().getInstantiationType();
78 if (instantiationType.equals(ToscaParserImpl2.Constants.MACRO)) {
79 return VidNotions.InstantiationType.Macro;
81 if (instantiationType.equals(ToscaParserImpl2.Constants.A_LA_CARTE)) {
82 return VidNotions.InstantiationType.ALaCarte;
85 return VidNotions.InstantiationType.ClientConfig;
88 //UI route a-la-carte services to old UI only if InstantiationUI is LEGACY
89 //So any other value for InstantiationUI other than LEGACY make UI to route
90 //a-la-carte services to new UI
91 VidNotions.InstantiationUI suggestInstantiationUI(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
92 if(featureManager.isActive(Features.FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI) && isALaCarte(csarHelper)) {
93 return VidNotions.InstantiationUI.ANY_ALACARTE_NEW_UI;
95 if (featureManager.isActive(Features.FLAG_1902_VNF_GROUPING) && isGrouping(csarHelper)) {
96 return VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING;
98 if (featureManager.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)) {
99 VidNotions.InstantiationUI instantiationUI = determine5GInstantiationUI(csarHelper);
100 if ( instantiationUI != null ) return instantiationUI;
102 if (featureManager.isActive(Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI) && isTransportService(csarHelper)){
103 return VidNotions.InstantiationUI.TRANSPORT_SERVICE;
105 if (featureManager.isActive(Features.FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI) && isServiceWithCollectionResource(serviceModel)){
106 return VidNotions.InstantiationUI.SERVICE_WITH_COLLECTION_RESOURCE;
108 if (featureManager.isActive(Features.FLAG_1908_INFRASTRUCTURE_VPN) && isInfraStructureVpn(csarHelper)){
109 return VidNotions.InstantiationUI.INFRASTRUCTURE_VPN;
111 if (featureManager.isActive(Features.FLAG_1908_A_LA_CARTE_VNF_NEW_INSTANTIATION_UI) && isVnfServiceRole(csarHelper)){
112 return VidNotions.InstantiationUI.A_LA_CARTE_VNF_SERVICE_ROLE;
114 return VidNotions.InstantiationUI.LEGACY;
118 private boolean isVnfServiceRole(ISdcCsarHelper csarHelper) {
119 final String serviceRole = csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.SERVICE_ROLE );
120 return StringUtils.equalsIgnoreCase("VNF" , serviceRole);
124 private VidNotions.InstantiationUI determine5GInstantiationUI(ISdcCsarHelper csarHelper) {
125 if (isUuidExactlyHardCoded1ffce89fef3f(csarHelper)) {
126 return VidNotions.InstantiationUI.SERVICE_UUID_IS_1ffce89f_ef3f_4cbb_8b37_82134590c5de;
127 } else if (isALaCarte(csarHelper) && hasAnyNetworkWithPropertyNetworkTechnologyEqualsStandardSriovOrOvs(csarHelper)) {
128 return VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS;
129 } else if (isALaCarte(csarHelper) && hasFabricConfiguration(csarHelper)) {
130 return VidNotions.InstantiationUI.SERVICE_WITH_FABRIC_CONFIGURATION;
135 private boolean isTransportService(ISdcCsarHelper csarHelper) {
136 return ("TRANSPORT".equalsIgnoreCase(csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.SERVICE_TYPE)));
139 private boolean isServiceWithCollectionResource(ServiceModel serviceModel){
140 return MapUtils.isNotEmpty(serviceModel.getCollectionResources());
143 private boolean isInfraStructureVpn(ISdcCsarHelper csarHelper) {
144 Metadata serviceMetadata = csarHelper.getServiceMetadata();
145 return ("BONDING".equalsIgnoreCase(serviceMetadata.getValue(ToscaParserImpl2.Constants.SERVICE_TYPE)) &&
146 "INFRASTRUCTURE-VPN".equalsIgnoreCase(serviceMetadata.getValue(ToscaParserImpl2.Constants.SERVICE_ROLE)));
149 VidNotions.ModelCategory suggestModelCategory(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
150 if (isALaCarte(csarHelper) && hasAnyNetworkWithPropertyNetworkTechnologyEqualsStandardSriovOrOvs(csarHelper)){
151 return VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL;
153 if(isALaCarte(csarHelper) && hasFabricConfiguration(csarHelper)) {
154 return VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL;
156 if (isInfraStructureVpn(csarHelper)) {
157 return VidNotions.ModelCategory.INFRASTRUCTURE_VPN;
159 if (isTransportService(csarHelper)) {
160 return VidNotions.ModelCategory.Transport;
162 if (isServiceWithCollectionResource(serviceModel)) {
163 return VidNotions.ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE;
165 return VidNotions.ModelCategory.OTHER;
168 VidNotions.InstantiationUI suggestViewEditUI(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
169 if (featureManager.isActive(Features.FLAG_1902_VNF_GROUPING) && isGrouping(csarHelper)) {
170 return VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING;
173 if (featureManager.isActive(Features.FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI) &&
174 featureManager.isActive(Features.FLAG_1908_RESUME_MACRO_SERVICE) &&
175 isServiceWithCollectionResource(serviceModel)) {
176 return VidNotions.InstantiationUI.SERVICE_WITH_COLLECTION_RESOURCE;
179 if (featureManager.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)) {
180 if (isMacro(serviceModel) && !isMacroExcludedFromAsyncFlow(serviceModel)) {
181 return VidNotions.InstantiationUI.MACRO_SERVICE;
183 VidNotions.InstantiationUI instantiationUISuggestion = suggestInstantiationUI(csarHelper, serviceModel);
184 if (instantiationUISuggestion!=VidNotions.InstantiationUI.LEGACY) {
185 return instantiationUISuggestion;
189 return VidNotions.InstantiationUI.LEGACY;
192 private boolean isMacro(ServiceModel serviceModel) {
193 return ToscaParserImpl2.Constants.MACRO.equals(serviceModel.getService().getInstantiationType());
196 private boolean isUuidExactlyHardCoded1ffce89fef3f(ISdcCsarHelper csarHelper) {
197 return equalsIgnoreCase(
198 csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.UUID), "95eb2c44-bff2-4e8b-ad5d-8266870b7717");
201 private boolean hasAnyNetworkWithPropertyNetworkTechnologyEqualsStandardSriovOrOvs(ISdcCsarHelper csarHelper) {
202 return hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV","OVS") ;
205 boolean hasFabricConfiguration(ISdcCsarHelper csarHelper) {
206 return csarHelper.getServiceNodeTemplates().stream()
207 .flatMap(nodeTemplate -> csarHelper.getNodeTemplateChildren(nodeTemplate).stream())
208 .anyMatch(child -> child.getType().equals(ToscaParserImpl2.Constants.FABRIC_CONFIGURATION_TYPE));
211 boolean hasAnyNetworkWithPropertyEqualsToAnyOf(ISdcCsarHelper csarHelper, String propertyName, String... propertyValues) {
213 .getServiceVlList().stream()
214 .map(NodeTemplate::getProperties)
215 .flatMap(props -> props.entrySet().stream())
216 .filter(prop -> equalsIgnoreCase(prop.getKey(), propertyName))
217 // getValue().getValue() because value is Entry, where it's inner value is what we're looking for
218 .anyMatch(prop -> equalsAnyIgnoreCase(prop.getValue().getValue().toString(), propertyValues));
221 boolean isALaCarte(ISdcCsarHelper csarHelper) {
222 final String instantiationType = csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.INSTANTIATION_TYPE);
223 return StringUtils.equalsIgnoreCase(instantiationType, ToscaParserImpl2.Constants.A_LA_CARTE);
226 boolean isMacroExcludedFromAsyncFlow(ServiceModel serviceModel) {
227 return (MapUtils.isNotEmpty(serviceModel.getPnfs()) ||
228 MapUtils.isNotEmpty(serviceModel.getCollectionResources()) ||
229 (MapUtils.isNotEmpty(serviceModel.getNetworks()) && !featureManager.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)));
232 private boolean isGrouping(ISdcCsarHelper csarHelper) {
233 final String serviceRole = csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.SERVICE_ROLE);
234 return StringUtils.equalsIgnoreCase(serviceRole, ToscaParserImpl2.Constants.GROUPING);