Merge "Correct path for service-instance"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdnc-simulator / src / main / java / org / onap / so / sdncsimulator / providers / ServiceOperationsCacheServiceProviderimpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdncsimulator.providers;
21
22 import static org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration.CREATED;
23 import static org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration.PENDINGCREATE;
24 import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE;
25 import static org.onap.so.sdncsimulator.utils.Constants.RESTCONF_CONFIG_END_POINT;
26 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_DATA_VNFS_VNF;
27 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION;
28 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION_CACHE;
29 import static org.onap.so.sdncsimulator.utils.Constants.VNF_DATA_VNF_TOPOLOGY;
30 import static org.onap.so.sdncsimulator.utils.Constants.YES;
31 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getString;
32 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getStringOrNull;
33 import static org.onap.so.sdncsimulator.utils.ObjectUtils.isValid;
34 import java.time.LocalDateTime;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Optional;
38 import javax.validation.Valid;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastActionEnumeration;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastRpcActionEnumeration;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiOnapmodelinformationOnapModelInformation;
43 import org.onap.sdnc.northbound.client.model.GenericResourceApiOperStatusData;
44 import org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration;
45 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
46 import org.onap.sdnc.northbound.client.model.GenericResourceApiRpcActionEnumeration;
47 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
48 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
49 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServiceData;
50 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfs;
51 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnf;
52 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnfVnfData;
53 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceinformationServiceInformation;
54 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicemodelinfrastructureService;
55 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicestatusServiceStatus;
56 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyServiceTopology;
57 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier;
58 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
59 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfinformationVnfInformation;
60 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfrequestinputVnfRequestInput;
61 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnftopologyVnfTopology;
62 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure;
63 import org.onap.so.sdncsimulator.models.Output;
64 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.cache.Cache;
69 import org.springframework.cache.CacheManager;
70 import org.springframework.http.HttpStatus;
71 import org.springframework.stereotype.Service;
72
73 /**
74  * @author Waqas Ikram (waqas.ikram@est.tech)
75  *
76  */
77 @Service
78 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
79         implements ServiceOperationsCacheServiceProvider {
80
81     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
82
83     @Autowired
84     public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
85         super(cacheManager);
86     }
87
88     @Override
89     public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
90
91         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
92         final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
93
94         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
95         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
96             final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
97             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
98             LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
99                     serviceInstanceId);
100
101             final GenericResourceApiServicemodelinfrastructureService service =
102                     getServiceItem(input, serviceInstanceId);
103             cache.put(serviceInstanceId, service);
104
105             final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
106
107             return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
108                     .responseCode(serviceStatus.getResponseCode()).responseMessage(serviceStatus.getResponseMessage())
109                     .svcRequestId(svcRequestId).serviceResponseInformation(new GenericResourceApiInstanceReference()
110                             .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)));
111
112         }
113         LOGGER.error(
114                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
115                 input);
116         return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
117                 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
118     }
119
120     @Override
121     public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
122             final String serviceInstanceId) {
123         final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
124
125         final GenericResourceApiServicemodelinfrastructureService value =
126                 cache.get(serviceInstanceId, GenericResourceApiServicemodelinfrastructureService.class);
127         if (value != null) {
128             LOGGER.info("Found {} in cahce for service instance id: {}", value, serviceInstanceId);
129             return Optional.of(value);
130         }
131         LOGGER.error("Unable to find GenericResourceApiServiceModelInfrastructure in cache for service instance id: {}",
132                 serviceInstanceId);
133         return Optional.empty();
134     }
135
136     @Override
137     public Output putVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
138
139         final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
140         final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
141
142         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
143         final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
144
145         if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
146                 && isValid(vnfInformation.getVnfId())) {
147             final String serviceInstanceId = serviceInformation.getServiceInstanceId();
148             final String vnfId = vnfInformation.getVnfId();
149             final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
150                     getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
151             if (optional.isPresent()) {
152                 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
153                 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
154                 if (serviceData != null) {
155                     final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
156                     final GenericResourceApiLastRpcActionEnumeration svcAction =
157                             GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(requestHeader));
158
159                     if (ifVnfNotExists(vnfId, svcAction, vnfsList)) {
160                         vnfsList.add(getGenericResourceApiServicedataVnf(serviceInstanceId, vnfId, input));
161
162                         final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
163
164                         return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
165                                 .responseCode(serviceStatus.getResponseCode())
166                                 .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
167                                 .serviceResponseInformation(new GenericResourceApiInstanceReference()
168                                         .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
169                                 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
170                                         .objectPath(getObjectPath(serviceInstanceId, vnfId)));
171                     }
172                     LOGGER.error("vnfId: {} already exists with SVC Action: {}", vnfId, svcAction);
173                     return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
174                             .responseMessage("vnfId: " + vnfId + " already exists").svcRequestId(svcRequestId);
175                 }
176             }
177             LOGGER.error(
178                     "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
179                     serviceInstanceId);
180
181         }
182         LOGGER.error(
183                 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
184                 input);
185         return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
186                 .responseMessage("Unable to add vnf").svcRequestId(svcRequestId);
187     }
188
189     @Override
190     public void clearAll() {
191         clearCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
192     }
193
194     private String getObjectPath(final String serviceInstanceId, final String vnfId) {
195         return getObjectPath(serviceInstanceId) + SERVICE_DATA_VNFS_VNF + vnfId + VNF_DATA_VNF_TOPOLOGY;
196     }
197
198     private String getObjectPath(final String serviceInstanceId) {
199         return RESTCONF_CONFIG_END_POINT + serviceInstanceId;
200     }
201
202
203     private boolean ifVnfNotExists(final String vnfId, final GenericResourceApiLastRpcActionEnumeration svcAction,
204             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
205         final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional = getExistingVnf(vnfId, vnfsList);
206         if (optional.isPresent()) {
207             final GenericResourceApiServicedataServicedataVnfsVnf existingVnf = optional.get();
208             final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = existingVnf.getVnfData();
209
210             if (vnfData != null && vnfData.getVnfLevelOperStatus() != null
211                     && vnfData.getVnfLevelOperStatus().getLastRpcAction() != null) {
212                 final GenericResourceApiLastRpcActionEnumeration existingVnflastRpcAction =
213                         vnfData.getVnfLevelOperStatus().getLastRpcAction();
214                 if (existingVnflastRpcAction.equals(svcAction)) {
215                     LOGGER.error("Found vnf with id: {} and LastRpcAction: {} same as SvcAction:  {}", vnfId,
216                             existingVnflastRpcAction, svcAction);
217                     return false;
218                 }
219                 LOGGER.warn("Will remove and replace existing vnf with id: {} as SvcAction is changed from {} to {}",
220                         vnfId, existingVnflastRpcAction, svcAction);
221                 vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
222
223             }
224         }
225
226         return true;
227     }
228
229     private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getExistingVnf(final String vnfId,
230             final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
231         final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional =
232                 vnfsList.stream().filter(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId)).findFirst();
233         return optional;
234     }
235
236     private List<GenericResourceApiServicedataServicedataVnfsVnf> getVnfs(
237             final GenericResourceApiServicedataServiceData serviceData) {
238         GenericResourceApiServicedataServicedataVnfs vnfs = serviceData.getVnfs();
239         if (vnfs == null) {
240             vnfs = new GenericResourceApiServicedataServicedataVnfs();
241             serviceData.setVnfs(vnfs);
242         }
243
244         List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = vnfs.getVnf();
245         if (vnfsList == null) {
246             vnfsList = new ArrayList<>();
247             vnfs.setVnf(vnfsList);
248         }
249         return vnfsList;
250     }
251
252     private GenericResourceApiServicedataServicedataVnfsVnf getGenericResourceApiServicedataVnf(
253             final String serviceInstanceId, final String vnfId, final GenericResourceApiVnfOperationInformation input) {
254         return new GenericResourceApiServicedataServicedataVnfsVnf().vnfId(vnfId).vnfData(getVnfData(input));
255     }
256
257     private GenericResourceApiServicedataServicedataVnfsVnfVnfData getVnfData(
258             final GenericResourceApiVnfOperationInformation input) {
259
260         final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData =
261                 new GenericResourceApiServicedataServicedataVnfsVnfVnfData();
262
263         vnfData.vnfLevelOperStatus(
264                 getServiceLevelOperStatus(PENDINGCREATE, input.getRequestInformation(), input.getSdncRequestHeader()));
265         vnfData.serviceInformation(input.getServiceInformation());
266         vnfData.sdncRequestHeader(input.getSdncRequestHeader());
267         vnfData.vnfInformation(input.getVnfInformation());
268         vnfData.requestInformation(input.getRequestInformation());
269         vnfData.vnfRequestInput(input.getVnfRequestInput());
270
271         vnfData.vnfTopology(getVnfTopology(input.getVnfInformation(), input.getVnfRequestInput()));
272
273         return vnfData;
274     }
275
276     private GenericResourceApiVnftopologyVnfTopology getVnfTopology(
277             final GenericResourceApiVnfinformationVnfInformation vnfInformation,
278             final GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput) {
279
280         final GenericResourceApiVnftopologyVnfTopology apiVnftopologyVnfTopology =
281                 new GenericResourceApiVnftopologyVnfTopology();
282
283         if (vnfInformation != null) {
284             apiVnftopologyVnfTopology.onapModelInformation(vnfInformation.getOnapModelInformation());
285             apiVnftopologyVnfTopology.vnfTopologyIdentifierStructure(getTopologyIdentifierStructure(vnfInformation));
286         }
287         if (vnfRequestInput != null) {
288             apiVnftopologyVnfTopology.tenant(vnfRequestInput.getTenant());
289             apiVnftopologyVnfTopology.aicClli(vnfRequestInput.getAicClli());
290             apiVnftopologyVnfTopology.aicCloudRegion(vnfRequestInput.getAicCloudRegion());
291         }
292         return apiVnftopologyVnfTopology;
293     }
294
295     private GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure getTopologyIdentifierStructure(
296             @Valid final GenericResourceApiVnfinformationVnfInformation vnfInformation) {
297         return new GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure()
298                 .vnfId(vnfInformation.getVnfId()).vnfName(vnfInformation.getVnfName())
299                 .vnfType(vnfInformation.getVnfType());
300     }
301
302     private GenericResourceApiServicemodelinfrastructureService getServiceItem(
303             final GenericResourceApiServiceOperationInformation input, final String serviceInstanceId) {
304
305         final GenericResourceApiServicedataServiceData apiServicedataServiceData =
306                 new GenericResourceApiServicedataServiceData();
307
308         apiServicedataServiceData.requestInformation(input.getRequestInformation());
309         apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
310         apiServicedataServiceData.serviceInformation(input.getServiceInformation());
311         apiServicedataServiceData.serviceTopology(getServiceTopology(input));
312         apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
313         apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
314
315         final GenericResourceApiServicestatusServiceStatus serviceStatus =
316                 getServiceStatus(getSvcAction(input.getSdncRequestHeader()), getAction(input.getRequestInformation()),
317                         HttpStatus.OK.toString());
318
319         return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
320                 .serviceStatus(serviceStatus).serviceInstanceId(serviceInstanceId);
321     }
322
323     private String getAction(final GenericResourceApiRequestinformationRequestInformation input) {
324         return getString(input.getRequestAction(), "");
325     }
326
327     private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
328         return input != null ? getStringOrNull(input.getSvcAction()) : null;
329     }
330
331     private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
332             final String responseCode) {
333         return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
334                 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
335                 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
336                 .responseCode(responseCode).requestStatus(SYNCCOMPLETE).responseMessage("").action(action);
337     }
338
339     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
340             final GenericResourceApiServiceOperationInformation input) {
341         return getServiceLevelOperStatus(CREATED, input.getRequestInformation(), input.getSdncRequestHeader());
342     }
343
344     private GenericResourceApiOperStatusData getServiceLevelOperStatus(
345             final GenericResourceApiOrderStatusEnumeration statusEnumeration,
346             final GenericResourceApiRequestinformationRequestInformation requestInformation,
347             final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader) {
348         return new GenericResourceApiOperStatusData().orderStatus(statusEnumeration)
349                 .lastAction(GenericResourceApiLastActionEnumeration.fromValue(getRequestAction(requestInformation)))
350                 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(sdncRequestHeader)));
351     }
352
353     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
354         return input != null ? getStringOrNull(input.getRequestAction()) : null;
355     }
356
357     private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
358             final GenericResourceApiServiceOperationInformation input) {
359         final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
360                 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
361         return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
362                 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
363     }
364
365     private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
366             final GenericResourceApiServiceOperationInformation input) {
367         final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
368                 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
369
370         if (input.getServiceInformation() != null) {
371             final GenericResourceApiServiceinformationServiceInformation serviceInformation =
372                     input.getServiceInformation();
373             identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
374                     .serviceType(input.getServiceInformation().getSubscriptionServiceType())
375                     .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
376         }
377
378         if (input.getServiceRequestInput() != null) {
379             identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());
380         }
381
382         return identifier;
383
384     }
385
386 }