e74663dbbab48e4f59ddd20ef632bdd0454a393c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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.so.adapters.catalogdb.rest;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.onap.so.db.catalog.beans.CvnfcCustomization;
26 import org.onap.so.db.catalog.beans.HeatEnvironment;
27 import org.onap.so.db.catalog.beans.VfModuleCustomization;
28 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
29 import org.onap.so.rest.catalog.beans.Cvnfc;
30 import org.onap.so.rest.catalog.beans.Service;
31 import org.onap.so.rest.catalog.beans.VfModule;
32 import org.onap.so.rest.catalog.beans.Vnf;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
36
37
38 @Component
39 public class ServiceMapper {
40     private static final Logger logger = LoggerFactory.getLogger(ServiceMapper.class);
41
42     public Service mapService(org.onap.so.db.catalog.beans.Service service, int depth) {
43         Service restService = new Service();
44         if (service.getCategory() != null) {
45             restService.setCategory(service.getCategory());
46         }
47         restService.setCreated(service.getCreated());
48         restService.setDescription(service.getDescription());
49
50         if (service.getDistrobutionStatus() != null) {
51             restService.setDistrobutionStatus(service.getDistrobutionStatus());
52         }
53         restService.setEnvironmentContext(service.getEnvironmentContext());
54         restService.setModelInvariantId(service.getModelInvariantUUID());
55         restService.setModelName(service.getModelName());
56         restService.setModelVersionId(service.getModelUUID());
57         restService.setModelVersion(service.getModelVersion());
58         if (service.getServiceRole() != null) {
59             restService.setServiceRole(service.getServiceRole());
60         }
61         restService.setServiceType(service.getServiceType());
62         restService.setWorkloadContext(service.getWorkloadContext());
63         if (depth > 0)
64             restService.setVnf(mapVnfs(service, depth));
65         return restService;
66     }
67
68     private List<Vnf> mapVnfs(org.onap.so.db.catalog.beans.Service service, int depth) {
69         List<Vnf> vnfs = new ArrayList<>();
70         logger.info("Vnf Count : {}", service.getVnfCustomizations().size());
71         service.getVnfCustomizations().parallelStream().forEach(vnf -> vnfs.add(mapVnf(vnf, depth)));
72         return vnfs;
73     }
74
75     protected Vnf mapVnf(org.onap.so.db.catalog.beans.VnfResourceCustomization vnfResourceCustomization, int depth) {
76         Vnf vnf = new Vnf();
77         vnf.setAvailabilityZoneMaxCount(vnfResourceCustomization.getAvailabilityZoneMaxCount());
78         vnf.setCategory(vnfResourceCustomization.getVnfResources().getCategory());
79         vnf.setCloudVersionMax(vnfResourceCustomization.getVnfResources().getAicVersionMax());
80         vnf.setCloudVersionMin(vnfResourceCustomization.getVnfResources().getAicVersionMin());
81         vnf.setMaxInstances(vnfResourceCustomization.getMaxInstances());
82         vnf.setMinInstances(vnfResourceCustomization.getMinInstances());
83         vnf.setModelCustomizationId(vnfResourceCustomization.getModelCustomizationUUID());
84         vnf.setModelInstanceName(vnfResourceCustomization.getModelInstanceName());
85         vnf.setModelInvariantId(vnfResourceCustomization.getVnfResources().getModelInvariantId());
86         vnf.setModelName(vnfResourceCustomization.getVnfResources().getModelName());
87         vnf.setModelVersionId(vnfResourceCustomization.getVnfResources().getModelUUID());
88         vnf.setModelVersion(vnfResourceCustomization.getVnfResources().getModelVersion());
89         vnf.setMultiStageDesign(vnfResourceCustomization.getMultiStageDesign());
90         vnf.setNfFunction(vnfResourceCustomization.getNfFunction());
91         vnf.setNfNamingCode(vnfResourceCustomization.getNfNamingCode());
92         vnf.setNfRole(vnfResourceCustomization.getNfRole());
93         vnf.setNfType(vnfResourceCustomization.getNfType());
94         vnf.setNfDataValid(vnfResourceCustomization.getNfDataValid());
95         vnf.setOrchestrationMode(vnfResourceCustomization.getVnfResources().getOrchestrationMode());
96         vnf.setSubCategory(vnfResourceCustomization.getVnfResources().getSubCategory());
97         vnf.setToscaNodeType(vnfResourceCustomization.getVnfResources().getToscaNodeType());
98
99         if (depth > 1) {
100             vnf.setVfModule(mapVfModules(vnfResourceCustomization, depth));
101         }
102         return vnf;
103     }
104
105     private List<VfModule> mapVfModules(VnfResourceCustomization vnfResourceCustomization, int depth) {
106         List<VfModule> vfModules = new ArrayList<>();
107         vnfResourceCustomization.getVfModuleCustomizations().parallelStream()
108                 .forEach(vfModule -> vfModules.add(mapVfModule(vfModule, depth)));
109         return vfModules;
110     }
111
112     private VfModule mapVfModule(VfModuleCustomization vfModuleCust, int depth) {
113         VfModule vfModule = new VfModule();
114         vfModule.setAvailabilityZoneCount(vfModuleCust.getAvailabilityZoneCount());
115         vfModule.setCreated(vfModuleCust.getCreated());
116         vfModule.setDescription(vfModuleCust.getVfModule().getDescription());
117         vfModule.setInitialCount(vfModuleCust.getInitialCount());
118         vfModule.setIsBase(vfModuleCust.getVfModule().getIsBase());
119         vfModule.setIsVolumeGroup(getIsVolumeGroup(vfModuleCust));
120         vfModule.setMaxInstances(vfModuleCust.getMaxInstances());
121         vfModule.setMinInstances(vfModuleCust.getMinInstances());
122         vfModule.setLabel(vfModuleCust.getLabel());
123         vfModule.setModelCustomizationId(vfModuleCust.getModelCustomizationUUID());
124         vfModule.setModelInvariantId(vfModuleCust.getVfModule().getModelInvariantUUID());
125         vfModule.setModelName(vfModuleCust.getVfModule().getModelName());
126         vfModule.setModelVersionId(vfModuleCust.getVfModule().getModelUUID());
127         vfModule.setModelVersion(vfModuleCust.getVfModule().getModelVersion());
128         if (depth > 3) {
129             vfModule.setVnfc(mapCvnfcs(vfModuleCust));
130         }
131         return vfModule;
132     }
133
134     private List<Cvnfc> mapCvnfcs(VfModuleCustomization vfModuleCustomization) {
135         List<Cvnfc> cvnfcs = new ArrayList<>();
136         vfModuleCustomization.getCvnfcCustomization().parallelStream()
137                 .forEach(cvnfcCust -> cvnfcs.add(mapCvnfcCus(cvnfcCust)));
138         return cvnfcs;
139     }
140
141     private Cvnfc mapCvnfcCus(CvnfcCustomization cvnfcCust) {
142         Cvnfc cvnfc = new Cvnfc();
143         cvnfc.setCreated(cvnfcCust.getCreated());
144         cvnfc.setDescription(cvnfcCust.getDescription());
145         cvnfc.setModelCustomizationId(cvnfcCust.getModelCustomizationUUID());
146         cvnfc.setModelInstanceName(cvnfcCust.getModelInstanceName());
147         cvnfc.setModelInvariantId(cvnfcCust.getModelInvariantUUID());
148         cvnfc.setModelName(cvnfcCust.getModelName());
149         cvnfc.setModelVersion(cvnfcCust.getModelVersion());
150         cvnfc.setModelVersionId(cvnfcCust.getModelUUID());
151         cvnfc.setNfcFunction(cvnfcCust.getNfcFunction());
152         cvnfc.setNfcNamingCode(cvnfcCust.getNfcNamingCode());
153         return cvnfc;
154     }
155
156     private boolean getIsVolumeGroup(VfModuleCustomization vfModuleCust) {
157         boolean isVolumeGroup = false;
158         HeatEnvironment envt = vfModuleCust.getVolumeHeatEnv();
159         if (envt != null) {
160             isVolumeGroup = true;
161         }
162         return isVolumeGroup;
163     }
164
165 }