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
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=========================================================
20 package org.onap.so.sdncsimulator.providers;
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;
74 * @author Waqas Ikram (waqas.ikram@est.tech)
78 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
79 implements ServiceOperationsCacheServiceProvider {
81 private static final String EMPTY_STRING = "";
82 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
85 public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
90 public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
92 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
93 final String svcRequestId = getSvcRequestId(requestHeader);
95 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
96 if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
97 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
98 final String serviceInstanceId = serviceInformation.getServiceInstanceId();
99 LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
102 final GenericResourceApiServicemodelinfrastructureService service =
103 getServiceItem(input, serviceInstanceId);
104 cache.put(serviceInstanceId, service);
106 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
108 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
109 .responseCode(serviceStatus.getResponseCode()).responseMessage(serviceStatus.getResponseMessage())
110 .svcRequestId(svcRequestId).serviceResponseInformation(new GenericResourceApiInstanceReference()
111 .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)));
115 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
117 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
118 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
122 public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
123 final String serviceInstanceId) {
124 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
126 final GenericResourceApiServicemodelinfrastructureService value =
127 cache.get(serviceInstanceId, GenericResourceApiServicemodelinfrastructureService.class);
129 LOGGER.info("Found {} in cahce for service instance id: {}", value, serviceInstanceId);
130 return Optional.of(value);
132 LOGGER.error("Unable to find GenericResourceApiServiceModelInfrastructure in cache for service instance id: {}",
134 return Optional.empty();
138 public Output putVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
140 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
141 final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
143 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
144 final String svcRequestId = getSvcRequestId(requestHeader);
146 if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
147 && isValid(vnfInformation.getVnfId())) {
148 final String serviceInstanceId = serviceInformation.getServiceInstanceId();
149 final String vnfId = vnfInformation.getVnfId();
150 final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
151 getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
152 if (optional.isPresent()) {
153 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
154 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
155 if (serviceData != null) {
156 final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
157 final GenericResourceApiLastRpcActionEnumeration svcAction =
158 GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(requestHeader));
160 if (ifVnfNotExists(vnfId, svcAction, vnfsList)) {
161 vnfsList.add(getGenericResourceApiServicedataVnf(serviceInstanceId, vnfId, input));
163 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
165 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
166 .responseCode(serviceStatus.getResponseCode())
167 .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
168 .serviceResponseInformation(new GenericResourceApiInstanceReference()
169 .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
170 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
171 .objectPath(getObjectPath(serviceInstanceId, vnfId)));
173 LOGGER.error("vnfId: {} already exists with SVC Action: {}", vnfId, svcAction);
174 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
175 .responseMessage("vnfId: " + vnfId + " already exists").svcRequestId(svcRequestId);
179 "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
184 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
186 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
187 .responseMessage("Unable to add vnf").svcRequestId(svcRequestId);
191 public Output deleteVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
192 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
193 final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
195 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
196 final String svcRequestId = getSvcRequestId(requestHeader);
198 if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId()) && vnfInformation != null
199 && isValid(vnfInformation.getVnfId())) {
200 final String serviceInstanceId = serviceInformation.getServiceInstanceId();
201 final String vnfId = vnfInformation.getVnfId();
202 final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
203 getGenericResourceApiServicemodelinfrastructureService(serviceInstanceId);
204 if (optional.isPresent()) {
205 final GenericResourceApiServicemodelinfrastructureService service = optional.get();
206 final GenericResourceApiServicedataServiceData serviceData = service.getServiceData();
207 if (serviceData != null) {
208 final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = getVnfs(serviceData);
209 final Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfInstanceOptional =
210 getExistingVnf(vnfId, vnfsList);
212 if (vnfInstanceOptional.isPresent()) {
213 vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
215 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.OK.toString())
216 .responseMessage(EMPTY_STRING).svcRequestId(svcRequestId)
217 .serviceResponseInformation(
218 new GenericResourceApiInstanceReference().instanceId(serviceInstanceId))
219 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId));
225 "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
229 LOGGER.error("Unable to remove vnf instance from cache due to invalid input: {}... ", input);
230 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
231 .responseMessage("Unable to remove vnf").svcRequestId(svcRequestId);
235 private String getSvcRequestId(final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader) {
236 return requestHeader != null ? requestHeader.getSvcRequestId() : null;
240 public void clearAll() {
241 clearCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
244 private String getObjectPath(final String serviceInstanceId, final String vnfId) {
245 return getObjectPath(serviceInstanceId) + SERVICE_DATA_VNFS_VNF + vnfId + VNF_DATA_VNF_TOPOLOGY;
248 private String getObjectPath(final String serviceInstanceId) {
249 return RESTCONF_CONFIG_END_POINT + serviceInstanceId;
253 private boolean ifVnfNotExists(final String vnfId, final GenericResourceApiLastRpcActionEnumeration svcAction,
254 final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
255 final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional = getExistingVnf(vnfId, vnfsList);
256 if (optional.isPresent()) {
257 final GenericResourceApiServicedataServicedataVnfsVnf existingVnf = optional.get();
258 final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = existingVnf.getVnfData();
260 if (vnfData != null && vnfData.getVnfLevelOperStatus() != null
261 && vnfData.getVnfLevelOperStatus().getLastRpcAction() != null) {
262 final GenericResourceApiLastRpcActionEnumeration existingVnflastRpcAction =
263 vnfData.getVnfLevelOperStatus().getLastRpcAction();
264 if (existingVnflastRpcAction.equals(svcAction)) {
265 LOGGER.error("Found vnf with id: {} and LastRpcAction: {} same as SvcAction: {}", vnfId,
266 existingVnflastRpcAction, svcAction);
269 LOGGER.warn("Will remove and replace existing vnf with id: {} as SvcAction is changed from {} to {}",
270 vnfId, existingVnflastRpcAction, svcAction);
271 vnfsList.removeIf(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId));
279 private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getExistingVnf(final String vnfId,
280 final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
281 return vnfsList.stream().filter(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId)).findFirst();
284 private List<GenericResourceApiServicedataServicedataVnfsVnf> getVnfs(
285 final GenericResourceApiServicedataServiceData serviceData) {
286 GenericResourceApiServicedataServicedataVnfs vnfs = serviceData.getVnfs();
288 vnfs = new GenericResourceApiServicedataServicedataVnfs();
289 serviceData.setVnfs(vnfs);
292 List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = vnfs.getVnf();
293 if (vnfsList == null) {
294 vnfsList = new ArrayList<>();
295 vnfs.setVnf(vnfsList);
300 private GenericResourceApiServicedataServicedataVnfsVnf getGenericResourceApiServicedataVnf(
301 final String serviceInstanceId, final String vnfId, final GenericResourceApiVnfOperationInformation input) {
302 return new GenericResourceApiServicedataServicedataVnfsVnf().vnfId(vnfId).vnfData(getVnfData(input));
305 private GenericResourceApiServicedataServicedataVnfsVnfVnfData getVnfData(
306 final GenericResourceApiVnfOperationInformation input) {
308 final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData =
309 new GenericResourceApiServicedataServicedataVnfsVnfVnfData();
311 vnfData.vnfLevelOperStatus(
312 getServiceLevelOperStatus(PENDINGCREATE, input.getRequestInformation(), input.getSdncRequestHeader()));
313 vnfData.serviceInformation(input.getServiceInformation());
314 vnfData.sdncRequestHeader(input.getSdncRequestHeader());
315 vnfData.vnfInformation(input.getVnfInformation());
316 vnfData.requestInformation(input.getRequestInformation());
317 vnfData.vnfRequestInput(input.getVnfRequestInput());
319 vnfData.vnfTopology(getVnfTopology(input.getVnfInformation(), input.getVnfRequestInput()));
324 private GenericResourceApiVnftopologyVnfTopology getVnfTopology(
325 final GenericResourceApiVnfinformationVnfInformation vnfInformation,
326 final GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput) {
328 final GenericResourceApiVnftopologyVnfTopology apiVnftopologyVnfTopology =
329 new GenericResourceApiVnftopologyVnfTopology();
331 if (vnfInformation != null) {
332 apiVnftopologyVnfTopology.onapModelInformation(vnfInformation.getOnapModelInformation());
333 apiVnftopologyVnfTopology.vnfTopologyIdentifierStructure(getTopologyIdentifierStructure(vnfInformation));
335 if (vnfRequestInput != null) {
336 apiVnftopologyVnfTopology.tenant(vnfRequestInput.getTenant());
337 apiVnftopologyVnfTopology.aicClli(vnfRequestInput.getAicClli());
338 apiVnftopologyVnfTopology.aicCloudRegion(vnfRequestInput.getAicCloudRegion());
340 return apiVnftopologyVnfTopology;
343 private GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure getTopologyIdentifierStructure(
344 @Valid final GenericResourceApiVnfinformationVnfInformation vnfInformation) {
345 return new GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure()
346 .vnfId(vnfInformation.getVnfId()).vnfName(vnfInformation.getVnfName())
347 .vnfType(vnfInformation.getVnfType());
350 private GenericResourceApiServicemodelinfrastructureService getServiceItem(
351 final GenericResourceApiServiceOperationInformation input, final String serviceInstanceId) {
353 final GenericResourceApiServicedataServiceData apiServicedataServiceData =
354 new GenericResourceApiServicedataServiceData();
356 apiServicedataServiceData.requestInformation(input.getRequestInformation());
357 apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
358 apiServicedataServiceData.serviceInformation(input.getServiceInformation());
359 apiServicedataServiceData.serviceTopology(getServiceTopology(input));
360 apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
361 apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
363 final GenericResourceApiServicestatusServiceStatus serviceStatus =
364 getServiceStatus(getSvcAction(input.getSdncRequestHeader()),
365 getRequestAction(input.getRequestInformation()), HttpStatus.OK.toString());
367 return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
368 .serviceStatus(serviceStatus).serviceInstanceId(serviceInstanceId);
371 private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
372 return input != null ? getStringOrNull(input.getSvcAction()) : null;
375 private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
376 final String responseCode) {
377 return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
378 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
379 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
380 .responseCode(responseCode).requestStatus(SYNCCOMPLETE).responseMessage(EMPTY_STRING).action(action);
383 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
384 final GenericResourceApiServiceOperationInformation input) {
385 return getServiceLevelOperStatus(CREATED, input.getRequestInformation(), input.getSdncRequestHeader());
388 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
389 final GenericResourceApiOrderStatusEnumeration statusEnumeration,
390 final GenericResourceApiRequestinformationRequestInformation requestInformation,
391 final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader) {
392 return new GenericResourceApiOperStatusData().orderStatus(statusEnumeration)
393 .lastAction(GenericResourceApiLastActionEnumeration.fromValue(getRequestAction(requestInformation)))
394 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(sdncRequestHeader)));
397 private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
398 return getRequestAction(input, EMPTY_STRING);
401 private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input,
402 final String defaultValue) {
403 return input != null ? getString(input.getRequestAction(), defaultValue) : defaultValue;
406 private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
407 final GenericResourceApiServiceOperationInformation input) {
408 final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
409 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
410 return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
411 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
414 private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
415 final GenericResourceApiServiceOperationInformation input) {
416 final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
417 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
419 if (input.getServiceInformation() != null) {
420 final GenericResourceApiServiceinformationServiceInformation serviceInformation =
421 input.getServiceInformation();
422 identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
423 .serviceType(input.getServiceInformation().getSubscriptionServiceType())
424 .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
427 if (input.getServiceRequestInput() != null) {
428 identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());