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 Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
84 public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
89 public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
91 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
92 final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
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: {}",
101 final GenericResourceApiServicemodelinfrastructureService service =
102 getServiceItem(input, serviceInstanceId);
103 cache.put(serviceInstanceId, service);
105 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
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)));
114 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
116 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
117 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
121 public Optional<GenericResourceApiServicemodelinfrastructureService> getGenericResourceApiServicemodelinfrastructureService(
122 final String serviceInstanceId) {
123 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
125 final GenericResourceApiServicemodelinfrastructureService value =
126 cache.get(serviceInstanceId, GenericResourceApiServicemodelinfrastructureService.class);
128 LOGGER.info("Found {} in cahce for service instance id: {}", value, serviceInstanceId);
129 return Optional.of(value);
131 LOGGER.error("Unable to find GenericResourceApiServiceModelInfrastructure in cache for service instance id: {}",
133 return Optional.empty();
137 public Output putVnfOperationInformation(final GenericResourceApiVnfOperationInformation input) {
139 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
140 final GenericResourceApiVnfinformationVnfInformation vnfInformation = input.getVnfInformation();
142 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
143 final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
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 if (ifVnfNotExists(vnfId, vnfsList)) {
158 vnfsList.add(getGenericResourceApiServicedataVnf(serviceInstanceId, vnfId, input));
160 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
162 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
163 .responseCode(serviceStatus.getResponseCode())
164 .responseMessage(serviceStatus.getResponseMessage()).svcRequestId(svcRequestId)
165 .serviceResponseInformation(new GenericResourceApiInstanceReference()
166 .instanceId(serviceInstanceId).objectPath(getObjectPath(serviceInstanceId)))
167 .vnfResponseInformation(new GenericResourceApiInstanceReference().instanceId(vnfId)
168 .objectPath(getObjectPath(serviceInstanceId, vnfId)));
170 LOGGER.error("vnfId: {} already exists", vnfId);
171 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
172 .responseMessage("vnfId: " + vnfId + " already exists").svcRequestId(svcRequestId);
176 "Unable to find existing GenericResourceApiServiceModelInfrastructure in cache using service instance id: {}",
181 "Unable to add GenericResourceApiServiceOperationInformation in cache due to invalid input: {}... ",
183 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
184 .responseMessage("Unable to add vnf").svcRequestId(svcRequestId);
188 public void clearAll() {
189 clearCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
192 private String getObjectPath(final String serviceInstanceId, final String vnfId) {
193 return getObjectPath(serviceInstanceId) + SERVICE_DATA_VNFS_VNF + vnfId + VNF_DATA_VNF_TOPOLOGY;
196 private String getObjectPath(final String serviceInstanceId) {
197 return RESTCONF_CONFIG_END_POINT + serviceInstanceId;
201 private boolean ifVnfNotExists(final String vnfId,
202 final List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList) {
203 final Optional<GenericResourceApiServicedataServicedataVnfsVnf> optional =
204 vnfsList.stream().filter(vnf -> vnf.getVnfId() != null && vnf.getVnfId().equals(vnfId)).findFirst();
205 return !optional.isPresent();
208 private List<GenericResourceApiServicedataServicedataVnfsVnf> getVnfs(
209 final GenericResourceApiServicedataServiceData serviceData) {
210 GenericResourceApiServicedataServicedataVnfs vnfs = serviceData.getVnfs();
212 vnfs = new GenericResourceApiServicedataServicedataVnfs();
213 serviceData.setVnfs(vnfs);
216 List<GenericResourceApiServicedataServicedataVnfsVnf> vnfsList = vnfs.getVnf();
217 if (vnfsList == null) {
218 vnfsList = new ArrayList<>();
219 vnfs.setVnf(vnfsList);
224 private GenericResourceApiServicedataServicedataVnfsVnf getGenericResourceApiServicedataVnf(
225 final String serviceInstanceId, final String vnfId, final GenericResourceApiVnfOperationInformation input) {
226 return new GenericResourceApiServicedataServicedataVnfsVnf().vnfId(vnfId).vnfData(getVnfData(input));
229 private GenericResourceApiServicedataServicedataVnfsVnfVnfData getVnfData(
230 final GenericResourceApiVnfOperationInformation input) {
232 final GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData =
233 new GenericResourceApiServicedataServicedataVnfsVnfVnfData();
235 vnfData.vnfLevelOperStatus(
236 getServiceLevelOperStatus(PENDINGCREATE, input.getRequestInformation(), input.getSdncRequestHeader()));
237 vnfData.serviceInformation(input.getServiceInformation());
238 vnfData.sdncRequestHeader(input.getSdncRequestHeader());
239 vnfData.vnfInformation(input.getVnfInformation());
240 vnfData.requestInformation(input.getRequestInformation());
241 vnfData.vnfRequestInput(input.getVnfRequestInput());
243 vnfData.vnfTopology(getVnfTopology(input.getVnfInformation(), input.getVnfRequestInput()));
248 private GenericResourceApiVnftopologyVnfTopology getVnfTopology(
249 final GenericResourceApiVnfinformationVnfInformation vnfInformation,
250 final GenericResourceApiVnfrequestinputVnfRequestInput vnfRequestInput) {
252 final GenericResourceApiVnftopologyVnfTopology apiVnftopologyVnfTopology =
253 new GenericResourceApiVnftopologyVnfTopology();
255 if (vnfInformation != null) {
256 apiVnftopologyVnfTopology.onapModelInformation(vnfInformation.getOnapModelInformation());
257 apiVnftopologyVnfTopology.vnfTopologyIdentifierStructure(getTopologyIdentifierStructure(vnfInformation));
259 if (vnfRequestInput != null) {
260 apiVnftopologyVnfTopology.tenant(vnfRequestInput.getTenant());
261 apiVnftopologyVnfTopology.aicClli(vnfRequestInput.getAicClli());
262 apiVnftopologyVnfTopology.aicCloudRegion(vnfRequestInput.getAicCloudRegion());
264 return apiVnftopologyVnfTopology;
267 private GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure getTopologyIdentifierStructure(
268 @Valid final GenericResourceApiVnfinformationVnfInformation vnfInformation) {
269 return new GenericResourceApiVnftopologyidentifierstructureVnfTopologyIdentifierStructure()
270 .vnfId(vnfInformation.getVnfId()).vnfName(vnfInformation.getVnfName())
271 .vnfType(vnfInformation.getVnfType());
274 private GenericResourceApiServicemodelinfrastructureService getServiceItem(
275 final GenericResourceApiServiceOperationInformation input, final String serviceInstanceId) {
277 final GenericResourceApiServicedataServiceData apiServicedataServiceData =
278 new GenericResourceApiServicedataServiceData();
280 apiServicedataServiceData.requestInformation(input.getRequestInformation());
281 apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
282 apiServicedataServiceData.serviceInformation(input.getServiceInformation());
283 apiServicedataServiceData.serviceTopology(getServiceTopology(input));
284 apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
285 apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
287 final GenericResourceApiServicestatusServiceStatus serviceStatus =
288 getServiceStatus(getSvcAction(input.getSdncRequestHeader()), getAction(input.getRequestInformation()),
289 HttpStatus.OK.toString());
291 return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
292 .serviceStatus(serviceStatus).serviceInstanceId(serviceInstanceId);
295 private String getAction(final GenericResourceApiRequestinformationRequestInformation input) {
296 return getString(input.getRequestAction(), "");
299 private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
300 return input != null ? getStringOrNull(input.getSvcAction()) : null;
303 private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
304 final String responseCode) {
305 return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
306 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
307 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
308 .responseCode(responseCode).requestStatus(SYNCCOMPLETE).responseMessage("").action(action);
311 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
312 final GenericResourceApiServiceOperationInformation input) {
313 return getServiceLevelOperStatus(CREATED, input.getRequestInformation(), input.getSdncRequestHeader());
316 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
317 final GenericResourceApiOrderStatusEnumeration statusEnumeration,
318 final GenericResourceApiRequestinformationRequestInformation requestInformation,
319 final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader) {
320 return new GenericResourceApiOperStatusData().orderStatus(statusEnumeration)
321 .lastAction(GenericResourceApiLastActionEnumeration.fromValue(getRequestAction(requestInformation)))
322 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration.fromValue(getSvcAction(sdncRequestHeader)));
325 private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
326 return input != null ? getStringOrNull(input.getRequestAction()) : null;
329 private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
330 final GenericResourceApiServiceOperationInformation input) {
331 final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
332 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
333 return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
334 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
337 private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
338 final GenericResourceApiServiceOperationInformation input) {
339 final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
340 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
342 if (input.getServiceInformation() != null) {
343 final GenericResourceApiServiceinformationServiceInformation serviceInformation =
344 input.getServiceInformation();
345 identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
346 .serviceType(input.getServiceInformation().getSubscriptionServiceType())
347 .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
350 if (input.getServiceRequestInput() != null) {
351 identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());