Merge "INT:1183 fix csit for sdnc_netconf_tls_post_deploy"
[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.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;
58
59 /**
60  * @author Waqas Ikram (waqas.ikram@est.tech)
61  *
62  */
63 @Service
64 public class ServiceOperationsCacheServiceProviderimpl extends AbstractCacheServiceProvider
65         implements ServiceOperationsCacheServiceProvider {
66
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOperationsCacheServiceProviderimpl.class);
69
70     @Autowired
71     public ServiceOperationsCacheServiceProviderimpl(final CacheManager cacheManager) {
72         super(cacheManager);
73     }
74
75     @Override
76     public Output putServiceOperationInformation(final GenericResourceApiServiceOperationInformation input) {
77
78         final GenericResourceApiSdncrequestheaderSdncRequestHeader requestHeader = input.getSdncRequestHeader();
79         final String svcRequestId = requestHeader != null ? requestHeader.getSvcRequestId() : null;
80
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: {}",
86                     serviceInstanceId);
87
88             final GenericResourceApiServiceModelInfrastructure serviceModelInfrastructure =
89                     new GenericResourceApiServiceModelInfrastructure();
90
91             final GenericResourceApiServicemodelinfrastructureService service = getServiceItem(input);
92             serviceModelInfrastructure.addServiceItem(service);
93             cache.put(serviceInstanceId, serviceModelInfrastructure);
94
95             final GenericResourceApiServicestatusServiceStatus serviceStatus = service.getServiceStatus();
96
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));
101
102         }
103         return new Output().ackFinalIndicator(YES).responseCode(HttpStatus.BAD_REQUEST.toString())
104                 .responseMessage("Service instance not found").svcRequestId(svcRequestId);
105     }
106
107     @Override
108     public Optional<GenericResourceApiServiceModelInfrastructure> getGenericResourceApiServiceModelInfrastructure(
109             final String serviceInstanceId) {
110         final Cache cache = getCache(SERVICE_TOPOLOGY_OPERATION_CACHE);
111
112         final GenericResourceApiServiceModelInfrastructure value =
113                 cache.get(serviceInstanceId, GenericResourceApiServiceModelInfrastructure.class);
114         if (value != null) {
115             return Optional.of(value);
116         }
117         return Optional.empty();
118     }
119
120     @Override
121     public void clearAll() {
122         clearCahce(SERVICE_TOPOLOGY_OPERATION_CACHE);
123     }
124
125     private GenericResourceApiServicemodelinfrastructureService getServiceItem(
126             final GenericResourceApiServiceOperationInformation input) {
127
128         final GenericResourceApiServicedataServiceData apiServicedataServiceData =
129                 new GenericResourceApiServicedataServiceData();
130
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));
137
138         final GenericResourceApiServicestatusServiceStatus serviceStatus =
139                 getServiceStatus(getSvcAction(input.getSdncRequestHeader()), getAction(input.getRequestInformation()),
140                         HttpStatus.OK.toString());
141
142         return new GenericResourceApiServicemodelinfrastructureService().serviceData(apiServicedataServiceData)
143                 .serviceStatus(serviceStatus);
144     }
145
146     private String getAction(final GenericResourceApiRequestinformationRequestInformation input) {
147         return getString(input.getRequestAction(), "");
148     }
149
150     private String getSvcAction(final GenericResourceApiSdncrequestheaderSdncRequestHeader input) {
151         return input != null ? getStringOrNull(input.getSvcAction()) : null;
152     }
153
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);
161     }
162
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())));
170     }
171
172     private String getRequestAction(final GenericResourceApiRequestinformationRequestInformation input) {
173         return input != null ? getStringOrNull(input.getRequestAction()) : null;
174     }
175
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));
182     }
183
184     private GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier getServiceTopologyIdentifier(
185             final GenericResourceApiServiceOperationInformation input) {
186         final GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier identifier =
187                 new GenericResourceApiServicetopologyidentifierServiceTopologyIdentifier();
188
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());;
195         }
196
197         if (input.getServiceRequestInput() != null) {
198             identifier.serviceInstanceName(input.getServiceRequestInput().getServiceInstanceName());
199         }
200
201         return identifier;
202
203     }
204
205 }