aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/NssiCDSRequestProvider.java
blob: d283092e0af2515aa9624ca67b48183868603bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (c) 2022 Deutsche telekom
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.client.cds;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import org.onap.aai.domain.yang.ServiceInstances;
import org.onap.aaiclient.client.aai.entities.CustomQuery;
import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri;
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
import org.onap.aaiclient.client.aai.entities.uri.NodesSingleUri;
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
import org.onap.aaiclient.client.graphinventory.Format;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.InjectionHelper;
import org.onap.so.bpmn.core.json.JsonUtils;
import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
import org.onap.so.client.exception.BBObjectNotFoundException;
import org.onap.so.client.exception.PayloadGenerationException;
import org.onap.so.moi.Attributes;
import org.onap.so.moi.Snssai;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NssiCDSRequestProvider implements CDSRequestProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(NssiCDSRequestProvider.class);

    private String blueprintName;
    private String blueprintVersion;
    private BuildingBlockExecution execution;

    private static final String DELETE_SLICE_PROFILE_ACTION = "delete-sliceprofile";
    private static final String CREATE_SLICE_PROFILE_ACTION = "create-sliceprofile";
    private static final String MODIFY_SLICE_PROFILE_ACTION = "modify-sliceprofile";
    private static final String SERVICE_INSTANCE_KEY = "service-instance";
    private static final String SERVICE_INSTANCE_ID_KEY = "service-instance-id";
    private static final String NSSI = "nssi";
    private static final String NSSI_ID = "nssiId";
    private static final String NSSI_NAME = "nssiName";
    private static final String NSI_NAME = "nsiName";
    private static final String NSI_ID = "nsiId";
    private static final String SLICE_PROFILE_INSTANCE_ID = "sliceProfileInstanceId";
    private static final String SLICE_INSTANCE_FROM_PROFILE_ID_CUSTOM_QUERY =
            "related-to?startingNodeType=slice-profile&relatedToNodeType=service-instance";
    private static final String ENVIRONMENT_CONTEXT_QUERY_PARAM = "environment-context";
    private static final String AAI_SUPPORTED_SLICE_PROFILE =
            "latency|maxNumberofUEs|coverageAreaTAList|areaTrafficCapDL|resourceSharingLevel|serviceType|uEMobilityLevel|expDataRateUL|expDataRateDL";
    private static final ObjectMapper mapper = new ObjectMapper();

    JsonUtils jsonUtil = new JsonUtils();

    @Autowired
    private ExtractPojosForBB extractPojosForBB;

    @Autowired
    private InjectionHelper injectionHelper;

    @Override
    public String getBlueprintName() {
        return blueprintName;
    }

    @Override
    public String getBlueprintVersion() {
        return blueprintVersion;
    }

    @Override
    public <T> void setExecutionObject(T executionObject) {
        execution = (BuildingBlockExecution) executionObject;
    }

    @Override
    public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
        JsonObject cdsPropertyObject = null;

        final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
        List<Map<String, Object>> userParamsFromRequest =
                buildingBlock.getRequestContext().getRequestParameters().getUserParams();

        try {
            ServiceInstance serviceInstance =
                    extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
            // get values from service Instance
            blueprintName = serviceInstance.getModelInfoServiceInstance().getBlueprintName();
            blueprintVersion = serviceInstance.getModelInfoServiceInstance().getBlueprintVersion();

        } catch (Exception e) {
            throw new PayloadGenerationException("Failed to buildPropertyObjectForNssi", e);
        }

        cdsPropertyObject = setCreateSliceProfileRequestValues(action, buildingBlock);

        return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
    }

    private JsonObject setCreateSliceProfileRequestValues(String action, GeneralBuildingBlock buildingBlock) {

        JsonObject cdsRequestObject = new JsonObject();

        ServiceInstance serviceInstance = null;
        try {
            serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
        } catch (BBObjectNotFoundException e) {
            e.printStackTrace();
        }

        List<Map<String, Object>> userParamsFromRequest =
                buildingBlock.getRequestContext().getRequestParameters().getUserParams();

        Attributes attributes = null;

        for (Map<String, Object> userParamData : userParamsFromRequest) {
            if (userParamData.get(NSSI) != null) {
                attributes = mapper.convertValue(userParamData.get(NSSI), Attributes.class);
            }
        }
        // Value to come as a request == Hardcoded for now
        String nssiId = serviceInstance.getServiceInstanceId();
        String nssiName = getNssiName(nssiId);
        String nsiId = "nssi-id";
        String nsiName = "nssi-name";

        cdsRequestObject.addProperty(NSSI_ID, nssiId);

        if (!DELETE_SLICE_PROFILE_ACTION.equals(action)) {
            cdsRequestObject.addProperty(NSSI_NAME, nssiName);
            cdsRequestObject.addProperty(NSI_NAME, nsiName);
        }

        String sliceProfileInstanceId = null;
        if (CREATE_SLICE_PROFILE_ACTION.equalsIgnoreCase(action)) {
            Snssai snssai = attributes.getSliceProfileList().get(0).getPlmnInfoList().get(0).getSnssai();
            String sNssaiString = snssai.getSst() + SEPARATOR + snssai.getSd();
            sliceProfileInstanceId = getSliceProfileInstanceIdForNssi(sNssaiString);
        } else {
            String sliceProfileId = attributes.getSliceProfileList().get(0).getSliceProfileId();
            sliceProfileInstanceId = getSliceProfileInstanceFromSliceProfileId(sliceProfileId);
        }

        cdsRequestObject.addProperty(SLICE_PROFILE_INSTANCE_ID, sliceProfileInstanceId);

        cdsRequestObject.addProperty(NSI_ID, nsiId);
        /*
         * JsonObject nssiPropertyObject = setSliceProfileProperties(getSliceProfilesFromUserParams(buildingBlock));
         * cdsRequestObject.add(action + SEPARATOR + PROPERTIES, nssiPropertyObject);
         */
        return cdsRequestObject;
    }

    private Map<String, Object> getSliceProfilesFromUserParams(GeneralBuildingBlock gBB) {


        List<Map<String, Object>> mapUserParams = gBB.getRequestContext().getRequestParameters().getUserParams();

        Map<String, Object> sliceProfileMap = null;
        try {
            String userParamsJson = mapper.writeValueAsString(mapUserParams.get(0));
            String rANSliceSubnetProfile =
                    jsonUtil.getJsonParamValue(userParamsJson, "nssi.sliceProfileList", "RANSliceSubnetProfile");

            if (rANSliceSubnetProfile != null) {
                sliceProfileMap = mapper.readValue(rANSliceSubnetProfile, Map.class);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return sliceProfileMap;
    }

    // May be needed later

    /*
     * private JsonObject setSliceProfileProperties(Map<String, Object> userParamsMap) { JsonObject
     * sliceProfilePropertiesNotPresentInAai = new JsonObject();
     * 
     * if (userParamsMap != null) { userParamsMap.forEach((k, v) -> { if (!AAI_SUPPORTED_SLICE_PROFILE.contains((String)
     * k)) { sliceProfilePropertiesNotPresentInAai.addProperty(k, v.toString()); } }); }
     * 
     * return sliceProfilePropertiesNotPresentInAai; }
     */

    private String getSliceProfileInstanceFromSliceProfileId(String sliceProfileId) {

        List<AAIResourceUri> startNodes = new ArrayList<>();
        startNodes.add(
                AAIUriFactory.createNodesUri(AAIFluentTypeBuilder.Types.SLICE_PROFILE.getFragment(sliceProfileId)));

        CustomQuery customQuery = new CustomQuery(startNodes, SLICE_INSTANCE_FROM_PROFILE_ID_CUSTOM_QUERY);

        String results = injectionHelper.getAaiQueryClient().query(Format.RESOURCE, customQuery);

        if (results == null || results.isEmpty()) {
            throw new RuntimeException("Slice Profile Instance Not found");
        }

        Map<String, List<Map<String, Object>>> serviceInstancesMap = null;
        try {
            serviceInstancesMap = mapper.readValue(results, Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        Map<String, Object> serviceInstance =
                (Map<String, Object>) serviceInstancesMap.get("results").get(0).get(SERVICE_INSTANCE_KEY);

        return (String) serviceInstance.get(SERVICE_INSTANCE_ID_KEY);
    }

    private String getSliceProfileInstanceIdForNssi(String sNssai) {

        String sliceProfileInstanceId = null;

        try {
            AAIPluralResourceUri uriSI =
                    AAIUriFactory.createNodesUri(AAIFluentTypeBuilder.Types.SERVICE_INSTANCES.getFragment())
                            .queryParam(ENVIRONMENT_CONTEXT_QUERY_PARAM, sNssai);
            Optional<ServiceInstances> sliceProfileInstancesOptional =
                    injectionHelper.getAaiClient().get(ServiceInstances.class, uriSI);

            if (sliceProfileInstancesOptional.isPresent()) {
                sliceProfileInstanceId =
                        sliceProfileInstancesOptional.get().getServiceInstance().get(0).getServiceInstanceId();
            }
        } catch (Exception e) {
            LOGGER.error("Error in getting sliceProfile Instance" + e.getMessage());
        }
        return sliceProfileInstanceId;
    }

    private String getNssiName(String nssiId) {

        String nssiName = null;

        try {
            NodesSingleUri uriSI =
                    AAIUriFactory.createNodesUri(AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(nssiId));
            Optional<org.onap.aai.domain.yang.ServiceInstance> sliceProfileInstancesOptional =
                    injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.ServiceInstance.class, uriSI);

            if (sliceProfileInstancesOptional.isPresent()) {
                nssiName = sliceProfileInstancesOptional.get().getServiceInstanceName();
            }
        } catch (Exception e) {
            LOGGER.error("Error in getting Nssi Instance" + e.getMessage());
        }
        return nssiName;
    }
}