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.Output;
50 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.cache.Cache;
55 import org.springframework.cache.CacheManager;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.stereotype.Service;
60 * @author Waqas Ikram (waqas.ikram@est.tech)
64 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
65 implements ServiceOperationsCacheServiceProvider {
68 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
71 public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
76 public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
78 final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
79 final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
81 final GenericResourceApiServiceinformationServiceInformation serviceInformation = input.getServiceInformation();
82 if (serviceInformation != null && isValid(serviceInformation.getServiceInstanceId())) {
83 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
84 final String serviceInstanceId = serviceInformation.getServiceInstanceId();
85 LOGGER.info("Adding GenericResourceApiServiceOperationInformation to cache with key: {}",
88 final GenericResourceApiServiceModelInfrastructure serviceModelInfrastructure =
89 new GenericResourceApiServiceModelInfrastructure();
91 final GenericResourceApiServicemodelinfrastructureService service = getServiceItem(input);
92 serviceModelInfrastructure.addServiceItem(service);
93 cache.put(serviceInstanceId, serviceModelInfrastructure);
95 final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
97 return new Output().ackFinalIndicator(serviceStatus.getFinalIndicator())
98 .responseCode(serviceStatus.getResponseCode()).responseMessage(serviceStatus.getResponseMessage())
99 .svcRequestId(svcRequestId).serviceResponseInformation(new GenericResourceApiInstanceReference()
100 .instanceId(serviceInstanceId).objectPath(RESTCONF_CONFIG_END_POINT + serviceInstanceId));
103 return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
104 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
108 public Optional<GenericResourceApiServiceModelInfrastructure> getGenericResourceApiServiceModelInfrastructure(
109 final String serviceInstanceId) {
110 final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
112 final GenericResourceApiServiceModelInfrastructure value =
113 cache.get(serviceInstanceId, GenericResourceApiServiceModelInfrastructure.class);
115 return Optional.of(value);
117 return Optional.empty();
121 public void clearAll() {
122 clearCahce(SERVICE_TOPOLOGY_OPERATION_CACHE);
125 private GenericResourceApiServicemodelinfrastructureService getServiceItem(
126 final GenericResourceApiServiceOperationInformation input) {
128 final GenericResourceApiServicedataServiceData apiServicedataServiceData =
129 new GenericResourceApiServicedataServiceData();
131 apiServicedataServiceData.requestInformation(input.getRequestInformation());
132 apiServicedataServiceData.serviceRequestInput(input.getServiceRequestInput());
133 apiServicedataServiceData.serviceInformation(input.getServiceInformation());
134 apiServicedataServiceData.serviceTopology(getServiceTopology(input));
135 apiServicedataServiceData.sdncRequestHeader(input.getSdncRequestHeader());
136 apiServicedataServiceData.serviceLevelOperStatus(getServiceLevelOperStatus(input));
138 final GenericResourceApiServicestatusServiceStatus serviceStatus =
139 getServiceStatus(getSvcAction(input.getSdncRequestHeader()), getAction(input.getRequestInformation()),
140 HttpStatus.OK.toString());
142 return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
143 .serviceStatus(serviceStatus);
146 private String getAction(final GenericResourceApiRequestinformationRequestInformation input) {
147 return getString(input.getRequestAction(), "");
150 private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
151 return input != null ? getStringOrNull(input.getSvcAction()) : null;
154 private GenericResourceApiServicestatusServiceStatus getServiceStatus(final String rpcAction, final String action,
155 final String responseCode) {
156 return new GenericResourceApiServicestatusServiceStatus().finalIndicator(YES)
157 .rpcAction(GenericResourceApiRpcActionEnumeration.fromValue(rpcAction))
158 .rpcName(SERVICE_TOPOLOGY_OPERATION).responseTimestamp(LocalDateTime.now().toString())
159 .responseCode(responseCode).requestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE)
160 .responseMessage("").action(action);
163 private GenericResourceApiOperStatusData getServiceLevelOperStatus(
164 final GenericResourceApiServiceOperationInformation input) {
165 return new GenericResourceApiOperStatusData().orderStatus(GenericResourceApiOrderStatusEnumeration.CREATED)
166 .lastAction(GenericResourceApiLastActionEnumeration
167 .fromValue(getRequestAction(input.getRequestInformation())))
168 .lastRpcAction(GenericResourceApiLastRpcActionEnumeration
169 .fromValue(getSvcAction(input.getSdncRequestHeader())));
172 private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
173 return input != null ? getStringOrNull(input.getRequestAction()) : null;
176 private GenericResourceApiServicetopologyServiceTopology getServiceTopology(
177 final GenericResourceApiServiceOperationInformation input) {
178 final GenericResourceApiOnapmodelinformationOnapModelInformation modelInformation =
179 input.getServiceInformation() != null ? input.getServiceInformation().getOnapModelInformation() : null;
180 return new GenericResourceApiServicetopologyServiceTopology().onapModelInformation(modelInformation)
181 .serviceTopologyIdentifier(getServiceTopologyIdentifier(input));
184 private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
185 final GenericResourceApiServiceOperationInformation input) {
186 final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
187 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
189 if (input.getServiceInformation() != null) {
190 final GenericResourceApiServiceinformationServiceInformation serviceInformation =
191 input.getServiceInformation();
192 identifier.globalCustomerId(serviceInformation.getGlobalCustomerId())
193 .serviceType(input.getServiceInformation().getSubscriptionServiceType())
194 .serviceInstanceId(input.getServiceInformation().getServiceInstanceId());;
197 if (input.getServiceRequestInput() != null) {
198 identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());