12c841a2c6d7d362d4cd03996c3ede82b6168c53
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ServiceCDSRequestProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Bell Canada
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
23 import com.google.gson.JsonObject;
24 import org.onap.so.bpmn.common.BuildingBlockExecution;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
26 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.exception.PayloadGenerationException;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
31 import org.springframework.context.annotation.Scope;
32 import org.springframework.stereotype.Component;
33 import java.util.Optional;
34 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
35 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
36
37 @Component
38 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
39 public class ServiceCDSRequestProvider implements CDSRequestProvider {
40
41     private static final String EMPTY_STRING = "";
42     private String resolutionKey;
43     private BuildingBlockExecution execution;
44
45     @Autowired
46     private ExtractPojosForBB extractPojosForBB;
47
48     @Override
49     public String getBlueprintName() {
50         return EMPTY_STRING;
51     }
52
53     @Override
54     public String getBlueprintVersion() {
55         return EMPTY_STRING;
56     }
57
58     @Override
59     public <T> void setExecutionObject(T executionObject) {
60         execution = (BuildingBlockExecution) executionObject;
61     }
62
63     @Override
64     public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
65         JsonObject cdsPropertyObject = new JsonObject();
66         JsonObject serviceObject = new JsonObject();
67         try {
68             ServiceInstance serviceInstance =
69                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
70
71             resolutionKey = serviceInstance.getServiceInstanceName();
72
73             // TODO Need to figure out how to populate blueprint name and version for service.
74
75             serviceObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
76             serviceObject.addProperty("service-model-uuid",
77                     serviceInstance.getModelInfoServiceInstance().getModelUuid());
78
79         } catch (Exception e) {
80             throw new PayloadGenerationException("Failed to buildPropertyObjectForService", e);
81         }
82
83         cdsPropertyObject.addProperty("resolution-key", resolutionKey);
84         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, serviceObject);
85
86         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
87     }
88
89 }