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.so.sdncsimulator.utils.Constants.RESTCONF_CONFIG_END_POINT;
23 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION;
24 import static org.onap.so.sdncsimulator.utils.Constants.SERVICE_TOPOLOGY_OPERATION_CACHE;
25 import static org.onap.so.sdncsimulator.utils.Constants.YES;
26 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getString;
27 import static org.onap.so.sdncsimulator.utils.ObjectUtils.getStringOrNull;
28 import static org.onap.so.sdncsimulator.utils.ObjectUtils.isValid;
29 import java.time.LocalDateTime;
30 import java.util.Optional;
31 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastActionEnumeration;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastRpcActionEnumeration;
34 import org.onap.sdnc.northbound.client.model.GenericResourceApiOnapmodelinformationOnapModelInformation;
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiOperStatusData;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiOrderStatusEnumeration;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestStatusEnumeration;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiRpcActionEnumeration;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceModelInfrastructure;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
43 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServiceData;
44 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceinformationServiceInformation;
45 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicemodelinfrastructureService;
46 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicestatusServiceStatus;
47 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyServiceTopology;
48 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier;
49 import org.onap.so.sdncsimulator.models.OutputRequest;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.cache.Cache;
54 import org.springframework.cache.CacheManager;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.stereotype.Service;
59 * @author Waqas Ikram (waqas.ikram@est.tech)
63 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
64 implements ServiceOperationsCacheServiceProvider {
67 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
70 public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
75 public OutputRequest putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
77 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
78 final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
80 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
81 if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
82 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
83 final String serviceInstanceId = serviceInformation.getServiceInstanceId();
84 LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
87 final GenericResourceApiServiceModelInfrastructure serviceModelInfrastructure =
88 new GenericResourceApiServiceModelInfrastructure();
90 final GenericResourceApiServicemodelinfrastructureService service = getServiceItem(input);
91 serviceModelInfrastructure.addServiceItem(service);
92 cache.put(serviceInstanceId, serviceModelInfrastructure);
94 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
96 return new OutputRequest().ackFinalIndicator(serviceStatus.getFinalIndicator())
97 .responseCode(serviceStatus.getResponseCode()).responseMessage(serviceStatus.getResponseMessage())
98 .svcRequestId(svcRequestId).serviceResponseInformation(new GenericResourceApiInstanceReference()
99 .instanceId(serviceInstanceId).objectPath(RESTCONF_CONFIG_END_POINT + serviceInstanceId));
102 return new OutputRequest().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
103 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
107 public Optional<GenericResourceApiServiceModelInfrastructure> getGenericResourceApiServiceModelInfrastructure(
108 final String serviceInstanceId) {
109 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
111 final GenericResourceApiServiceModelInfrastructure value =
112 cache.get(serviceInstanceId, GenericResourceApiServiceModelInfrastructure.class);
114 return Optional.of(value);
116 return Optional.empty();
120 public void clearAll() {
121 clearCahce(SERVICE_TOPOLOGY_OPERATION_CACHE);
124 private GenericResourceApiServicemodelinfrastructureService getServiceItem(
125 final GenericResourceApiServiceOperationInformation input) {
127 final GenericResourceApiServicedataServiceData apiServicedataServiceData =
128 new GenericResourceApiServicedataServiceData();
130 apiServicedataServiceData.requestInformation(input.getRequestInformation());
131 apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
132 apiServicedataServiceData.serviceInformation(input.getServiceInformation());
133 apiServicedataServiceData.serviceTopology(getServiceTopology(input));
134 apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
135 apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
137 final GenericResourceApiServicestatusServiceStatus serviceStatus =
138 getServiceStatus(getSvcAction(input.getSdncRequestHeader()), getAction(input.getRequestInformation()),
139 HttpStatus.OK.toString());
141 return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
142 .serviceStatus(serviceStatus);
145 private String getAction(final GenericResourceApiRequestinformationRequestInformation input) {
146 return getString(input.getRequestAction(), "");
149 private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
150 return input != null ? getStringOrNull(input.getSvcAction()) : null;
153 private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
154 final String responseCode) {
155 return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
156 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
157 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
158 .responseCode(responseCode).requestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE)
159 .responseMessage("").action(action);
162 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
163 final GenericResourceApiServiceOperationInformation input) {
164 return new GenericResourceApiOperStatusData().orderStatus(GenericResourceApiOrderStatusEnumeration.CREATED)
165 .lastAction(GenericResourceApiLastActionEnumeration
166 .fromValue(getRequestAction(input.getRequestInformation())))
167 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration
168 .fromValue(getSvcAction(input.getSdncRequestHeader())));
171 private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
172 return input != null ? getStringOrNull(input.getRequestAction()) : null;
175 private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
176 final GenericResourceApiServiceOperationInformation input) {
177 final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
178 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
179 return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
180 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
183 private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
184 final GenericResourceApiServiceOperationInformation input) {
185 final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
186 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
188 if (input.getServiceInformation() != null) {
189 final GenericResourceApiServiceinformationServiceInformation serviceInformation =
190 input.getServiceInformation();
191 identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
192 .serviceType(input.getServiceInformation().getSubscriptionServiceType())
193 .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
196 if (input.getServiceRequestInput() != null) {
197 identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());