Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / parser / VidNotionsBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.asdc.parser;
22
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;
26
27 import com.google.common.collect.ImmutableMap;
28 import java.util.Map;
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;
39
40 public class VidNotionsBuilder {
41
42     private final FeatureManager featureManager;
43
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
49     );
50
51     public VidNotionsBuilder(FeatureManager featureManager) {
52         this.featureManager = featureManager;
53     }
54
55     VidNotions buildVidNotions(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
56         VidNotions.ModelCategory modelCategory = suggestModelCategory(csarHelper, serviceModel);
57         return new VidNotions(
58                 suggestInstantiationUI(csarHelper, serviceModel),
59                 modelCategory,
60                 suggestViewEditUI(csarHelper, serviceModel),
61                 suggestInstantiationType(serviceModel, modelCategory));
62     }
63
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));
68     }
69
70     VidNotions.InstantiationType suggestInstantiationType(ServiceModel serviceModel, VidNotions.ModelCategory modelCategory) {
71         if (isMacroTypeByModelCategory(modelCategory)) {
72             return VidNotions.InstantiationType.Macro;
73         }
74         if (serviceModel==null || serviceModel.getService()==null || isEmpty(serviceModel.getService().getInstantiationType())) {
75             return VidNotions.InstantiationType.ClientConfig;
76         }
77         String instantiationType = serviceModel.getService().getInstantiationType();
78         if (instantiationType.equals(ToscaParserImpl2.Constants.MACRO)) {
79             return VidNotions.InstantiationType.Macro;
80         }
81         if (instantiationType.equals(ToscaParserImpl2.Constants.A_LA_CARTE)) {
82             return VidNotions.InstantiationType.ALaCarte;
83         }
84
85         return VidNotions.InstantiationType.ClientConfig;
86     }
87
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;
94         }
95         if (featureManager.isActive(Features.FLAG_1902_VNF_GROUPING) && isGrouping(csarHelper)) {
96             return VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING;
97         }
98         if (featureManager.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)) {
99             VidNotions.InstantiationUI instantiationUI = determine5GInstantiationUI(csarHelper);
100             if ( instantiationUI != null ) return instantiationUI;
101         }
102         if (featureManager.isActive(Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI) && isTransportService(csarHelper)){
103             return VidNotions.InstantiationUI.TRANSPORT_SERVICE;
104         }
105         if (featureManager.isActive(Features.FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI) && isServiceWithCollectionResource(serviceModel)){
106             return VidNotions.InstantiationUI.SERVICE_WITH_COLLECTION_RESOURCE;
107         }
108         if (featureManager.isActive(Features.FLAG_1908_INFRASTRUCTURE_VPN) && isInfraStructureVpn(csarHelper)){
109             return VidNotions.InstantiationUI.INFRASTRUCTURE_VPN;
110         }
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;
113         }
114         return VidNotions.InstantiationUI.LEGACY;
115
116     }
117
118     private boolean isVnfServiceRole(ISdcCsarHelper csarHelper) {
119         final String serviceRole = csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.SERVICE_ROLE );
120         return StringUtils.equalsIgnoreCase("VNF" , serviceRole);
121     }
122
123     @Nullable
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;
131         }
132         return null;
133     }
134
135     private boolean isTransportService(ISdcCsarHelper csarHelper) {
136         return ("TRANSPORT".equalsIgnoreCase(csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.SERVICE_TYPE)));
137     }
138
139     private boolean isServiceWithCollectionResource(ServiceModel serviceModel){
140         return MapUtils.isNotEmpty(serviceModel.getCollectionResources());
141     }
142
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)));
147     }
148
149     VidNotions.ModelCategory suggestModelCategory(ISdcCsarHelper csarHelper, ServiceModel serviceModel) {
150         if (isALaCarte(csarHelper) && hasAnyNetworkWithPropertyNetworkTechnologyEqualsStandardSriovOrOvs(csarHelper)){
151             return VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL;
152         }
153         if(isALaCarte(csarHelper) && hasFabricConfiguration(csarHelper)) {
154             return VidNotions.ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL;
155         }
156         if (isInfraStructureVpn(csarHelper)) {
157             return VidNotions.ModelCategory.INFRASTRUCTURE_VPN;
158         }
159         if (isTransportService(csarHelper)) {
160             return VidNotions.ModelCategory.Transport;
161         }
162         if (isServiceWithCollectionResource(serviceModel)) {
163             return VidNotions.ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE;
164         }
165         return VidNotions.ModelCategory.OTHER;
166     }
167
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;
171         }
172
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;
177         }
178
179         if (featureManager.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)) {
180             if (isMacro(serviceModel) && !isMacroExcludedFromAsyncFlow(serviceModel)) {
181                 return VidNotions.InstantiationUI.MACRO_SERVICE;
182             }
183             VidNotions.InstantiationUI instantiationUISuggestion = suggestInstantiationUI(csarHelper, serviceModel);
184             if (instantiationUISuggestion!=VidNotions.InstantiationUI.LEGACY) {
185                 return instantiationUISuggestion;
186             }
187         }
188
189         return VidNotions.InstantiationUI.LEGACY;
190     }
191
192     private boolean isMacro(ServiceModel serviceModel) {
193         return ToscaParserImpl2.Constants.MACRO.equals(serviceModel.getService().getInstantiationType());
194     }
195
196     private boolean isUuidExactlyHardCoded1ffce89fef3f(ISdcCsarHelper csarHelper) {
197         return equalsIgnoreCase(
198                 csarHelper.getServiceMetadata().getValue(ToscaParserImpl2.Constants.UUID), "95eb2c44-bff2-4e8b-ad5d-8266870b7717");
199     }
200
201     private boolean hasAnyNetworkWithPropertyNetworkTechnologyEqualsStandardSriovOrOvs(ISdcCsarHelper csarHelper) {
202         return hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV","OVS") ;
203     }
204
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));
209     }
210
211     boolean hasAnyNetworkWithPropertyEqualsToAnyOf(ISdcCsarHelper csarHelper, String propertyName,  String... propertyValues) {
212         return csarHelper
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));
219     }
220
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);
224     }
225
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)));
230     }
231
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);
235     }
236 }