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