aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/onap/so/bpmn/common/scripts/OofHoming.groovy
blob: 6583ded41325ef7bfd64f76a6135120af9944bec (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2018 Intel Corp. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.bpmn.common.scripts

import org.onap.so.bpmn.core.UrnPropertiesReader
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution

import org.onap.so.bpmn.core.domain.InventoryType
import org.onap.so.bpmn.core.domain.Resource
import org.onap.so.bpmn.core.domain.ResourceType
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.domain.Subscriber
import org.onap.so.bpmn.core.domain.VnfResource
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.client.DefaultProperties
import org.onap.so.client.HttpClient
import org.onap.so.client.HttpClientFactory
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.db.catalog.beans.AuthenticationType
import org.onap.so.db.catalog.beans.CloudIdentity
import org.onap.so.db.catalog.beans.CloudSite
import org.onap.so.db.catalog.beans.HomingInstance
import org.onap.so.db.catalog.beans.ServerType
import org.onap.so.utils.TargetEntity
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import org.json.JSONArray
import org.json.JSONObject
import org.springframework.web.util.UriUtils

import static org.onap.so.bpmn.common.scripts.GenericUtils.*

import javax.ws.rs.core.Response

/**
 * This class contains the scripts used
 * by the OOF Homing Subflow building block. The
 * subflow attempts to home the provided
 * resources by calling OOF.
 */
class OofHoming extends AbstractServiceTaskProcessor {
    private static final Logger logger = LoggerFactory.getLogger( OofHoming.class);

    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    OofUtils oofUtils = new OofUtils(this)

    /**
     * This method validates the incoming variables.
     * The method then prepares the OOF request
     * and posts it to OOF's rest api.
     *
     * @param execution
     */
    public void callOof(DelegateExecution execution) {
        execution.setVariable("prefix", "HOME_")
        logger.debug( "*** Started Homing Call OOF ***")
        try {
            execution.setVariable("rollbackData", null)
            execution.setVariable("rolledBack", false)

            String requestId = execution.getVariable("msoRequestId")
            logger.debug( "Incoming Request Id is: " + requestId)
            String serviceInstanceId = execution.getVariable("serviceInstanceId")
            logger.debug( "Incoming Service Instance Id is: " + serviceInstanceId)
            String serviceInstanceName = execution.getVariable("serviceInstanceName")
            logger.debug( "Incoming Service Instance Name is: " + serviceInstanceName)
            ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
            logger.debug( "Incoming Service Decomposition is: " + serviceDecomposition)
            String subscriberInfo = execution.getVariable("subscriberInfo")
            logger.debug( "Incoming Subscriber Information is: " + subscriberInfo)
            Map customerLocation = execution.getVariable("customerLocation")
            logger.debug( "Incoming Customer Location is: " + customerLocation.toString())
            String cloudOwner = execution.getVariable("cloudOwner")
            logger.debug( "Incoming cloudOwner is: " + cloudOwner)
            String cloudRegionId = execution.getVariable("cloudRegionId")
            logger.debug( "Incoming cloudRegionId is: " + cloudRegionId)

            if (isBlank(requestId) ||
                    isBlank(serviceInstanceId) ||
                    isBlank(serviceInstanceName) ||
                    isBlank(serviceDecomposition.toString()) ||
                    isBlank(customerLocation.toString())) {
                exceptionUtil.buildAndThrowWorkflowException(execution, 4000,
                        "A required input variable is missing or null")
            } else {
                Subscriber subscriber = null
                if (isBlank(subscriberInfo)) {
                    subscriber = new Subscriber("", "", "")
                } else {
                    String subId = jsonUtil.getJsonValue(subscriberInfo, "globalSubscriberId")
                    String subName = jsonUtil.getJsonValue(subscriberInfo, "subscriberName")
                    String subCommonSiteId = ""
                    if (jsonUtil.jsonElementExist(subscriberInfo, "subscriberCommonSiteId")) {
                        subCommonSiteId = jsonUtil.getJsonValue(subscriberInfo, "subscriberCommonSiteId")
                    }
                    subscriber = new Subscriber(subId, subName, subCommonSiteId)
                }

                //Authentication
                def authHeader = ""
                String basicAuth = UrnPropertiesReader.getVariable("mso.oof.auth", execution)
                String msokey = UrnPropertiesReader.getVariable("mso.msoKey", execution)

                String basicAuthValue = utils.encrypt(basicAuth, msokey)
                if (basicAuthValue != null) {
                    logger.debug( "Obtained BasicAuth username and password for OOF Adapter: " + basicAuthValue)
                    try {
                        authHeader = utils.getBasicAuth(basicAuthValue, msokey)
                        execution.setVariable("BasicAuthHeaderValue", authHeader)
                    } catch (Exception ex) {
                        logger.debug( "Unable to encode username and password string: " + ex)
                        exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - Unable to " +
                                "encode username and password string")
                    }
                } else {
                    logger.debug( "Unable to obtain BasicAuth - BasicAuth value null")
                    exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Internal Error - BasicAuth " +
                            "value null")
                }

                //Prepare Callback
                String timeout = execution.getVariable("timeout")
                if (isBlank(timeout)) {
                    timeout = UrnPropertiesReader.getVariable("mso.oof.timeout", execution);
                    if (isBlank(timeout)) {
                        timeout = "PT30M"
                    }
                }
                logger.debug( "Async Callback Timeout will be: " + timeout)

                execution.setVariable("timeout", timeout)
                execution.setVariable("correlator", requestId)
                execution.setVariable("messageType", "oofResponse")

                //Build Request & Call OOF
                String oofRequest = oofUtils.buildRequest(execution, requestId, serviceDecomposition,
                        subscriber, customerLocation)
                execution.setVariable("oofRequest", oofRequest)
                logger.debug( "OOF Request is: " + oofRequest)

                String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
                logger.debug( "Posting to OOF Url: " + urlString)


                URL url = new URL(urlString)
                HttpClient httpClient = new HttpClientFactory().newJsonClient(url, TargetEntity.OOF)
                httpClient.addAdditionalHeader("Authorization", authHeader)
                Response httpResponse = httpClient.post(oofRequest)

                int responseCode = httpResponse.getStatus()
                logger.debug("OOF sync response code is: " + responseCode)

                if(responseCode != 202){
                    exceptionUtil.buildAndThrowWorkflowException(execution, responseCode, "Received a Bad Sync Response from OOF.")
                }

                logger.debug( "*** Completed Homing Call OOF ***")
            }
        } catch (BpmnError b) {
            throw b
        } catch (Exception e) {
            logger.error(e);
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500,
                    "Internal Error - Occured in Homing callOof: " + e.getMessage())
        }
    }

    /**
     * This method processes the callback response
     * and the contained homing solution. It sets
     * homing solution assignment and license
     * information to the corresponding resources
     *
     * @param execution
     */
    public void processHomingSolution(DelegateExecution execution) {

        logger.debug( "*** Started Homing Process Homing Solution ***")
        try {
            String response = execution.getVariable("asyncCallbackResponse")
            logger.debug( "OOF Async Callback Response is: " + response)

            oofUtils.validateCallbackResponse(execution, response)
            String placements = jsonUtil.getJsonValue(response, "solutions.placementSolutions")
            logger.debug( "****** Solution Placements: " + placements + " *****")

            ServiceDecomposition decomposition = execution.getVariable("serviceDecomposition")
            logger.debug( "Service Decomposition: " + decomposition)

            List<Resource> resourceList = decomposition.getServiceResources()
            JSONArray arr = new JSONArray(placements)
            for (int i = 0; i < arr.length(); i++) {
                JSONArray arrSol = arr.getJSONArray(i)
                for (int j = 0; j < arrSol.length(); j++) {
                    JSONObject placement = arrSol.getJSONObject(j)
                    logger.debug( "****** Placement Solution is: " + placement + " *****")
                    String jsonServiceResourceId = jsonUtil.getJsonValue( placement.toString(), "serviceResourceId")
                    logger.debug( "****** homing serviceResourceId is: " + jsonServiceResourceId + " *****")
                    for (Resource resource : resourceList) {
                        String serviceResourceId = resource.getResourceId()
                        logger.debug( "****** decomp serviceResourceId is: " + serviceResourceId + " *****")
                        if (serviceResourceId.equalsIgnoreCase(jsonServiceResourceId)) {
                            JSONObject solution = placement.getJSONObject("solution")
                            String solutionType = solution.getString("identifierType")
                            String inventoryType = ""
                            if (solutionType.equalsIgnoreCase("serviceInstanceId")) {
                                inventoryType = "service"
                            } else {
                                inventoryType = "cloud"
                            }
                            logger.debug( "****** homing inventoryType is: " + inventoryType + " *****")
                            resource.getHomingSolution().setInventoryType(InventoryType.valueOf(inventoryType))

                            JSONArray assignmentArr = placement.getJSONArray("assignmentInfo")
                            logger.debug( "****** assignmentInfo is: " + assignmentArr.toString() + " *****")

                            Map<String, String> assignmentMap = jsonUtil.entryArrayToMap(execution,
                                    assignmentArr.toString(), "key", "value")
                            String oofDirectives = null
                            assignmentMap.each { key, value ->
                                logger.debug( "****** element: " + key + " *****")
                                if (key == "oof_directives") {
                                    oofDirectives = value
                                    logger.debug( "****** homing oofDirectives: " + oofDirectives + " *****")
                                }
                            }
                            String cloudOwner = assignmentMap.get("cloudOwner")
                            logger.debug( "****** homing cloudOwner: " + cloudOwner + " *****")
                            String cloudRegionId = assignmentMap.get("locationId")
                            logger.debug( "****** homing cloudRegionId: " + cloudRegionId + " *****")
                            resource.getHomingSolution().setCloudOwner(cloudOwner)
                            resource.getHomingSolution().setCloudRegionId(cloudRegionId)

                            CloudSite cloudSite = new CloudSite()
                            cloudSite.setId(cloudRegionId)
                            cloudSite.setRegionId(cloudRegionId)
                            String orchestrator = execution.getVariable("orchestrator")
                            if ((orchestrator != null) && (orchestrator != "")) {
                                cloudSite.setOrchestrator(orchestrator)
                                logger.debug( "****** orchestrator: " + orchestrator + " *****")
                            } else {
                                cloudSite.setOrchestrator("multicloud")
                            }

                            CloudIdentity cloudIdentity = new CloudIdentity()
                            cloudIdentity.setId(cloudRegionId)
                            cloudIdentity.setIdentityServerType(ServerType."KEYSTONE")
                            cloudIdentity.setAdminTenant("service")
                            cloudIdentity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD)
                            String msoMulticloudUserName = UrnPropertiesReader
                                    .getVariable("mso.multicloud.api.username", execution,
                                    "apih")
                            String msoMulticloudPassword = UrnPropertiesReader
                                    .getVariable("mso.multicloud.api.password", execution,
                                    "abc123")
                            cloudIdentity.setMsoId(msoMulticloudUserName)
                            cloudIdentity.setMsoPass(msoMulticloudPassword)
                            // Get MSB Url
                            String msbHost = oofUtils.getMsbHost(execution)
                            String multicloudApiEndpoint = UrnPropertiesReader
                                    .getVariable("mso.multicloud.api.endpoint", execution,
                                    "/api/multicloud-titaniumcloud/v1")
                            cloudIdentity.setIdentityUrl(msbHost + multicloudApiEndpoint
                                    + "/" + cloudOwner + "/" +
                                    cloudRegionId + "/infra_workload")
                            logger.debug( "****** Cloud IdentityUrl: " + msbHost + multicloudApiEndpoint
                                    + "/" + cloudOwner + "/" +
                                    cloudRegionId + "/infra_workload"
                                    + " *****")
                            logger.debug( "****** CloudIdentity: " + cloudIdentity.toString()
                                    + " *****")
                            cloudSite.setIdentityService(cloudIdentity)
                            logger.debug( "****** CloudSite: " + cloudSite.toString()
                                    + " *****")

                            // Set cloudsite in catalog DB here
                            oofUtils.createCloudSite(cloudSite, execution)

                            if (oofDirectives != null && oofDirectives != "") {
                                resource.getHomingSolution().setOofDirectives(oofDirectives)
                                execution.setVariable("oofDirectives", oofDirectives)
                                logger.debug( "***** OofDirectives set to: " + oofDirectives +
                                        " *****", "true")
                            }

                            // Set Homing Instance
                            String serviceInstanceId = decomposition.getServiceInstance().getInstanceId()
                            HomingInstance homingInstance = new HomingInstance()
                            homingInstance.setServiceInstanceId(serviceInstanceId)
                            homingInstance.setCloudOwner(cloudOwner)
                            homingInstance.setCloudRegionId(cloudRegionId)
                            if (oofDirectives != null && oofDirectives != "") {
                                homingInstance.setOofDirectives(oofDirectives)}
                            else {
                                homingInstance.setOofDirectives("{}")
                            }
                            oofUtils.createHomingInstance(homingInstance, execution)

                            if (inventoryType.equalsIgnoreCase("service")) {
                                resource.getHomingSolution().setRehome(assignmentMap.get("isRehome").toBoolean())
                                VnfResource vnf = new VnfResource()
                                vnf.setVnfHostname(assignmentMap.get("vnfHostName"))
                                resource.getHomingSolution().setVnf(vnf)
                                resource.getHomingSolution().setServiceInstanceId(solution.getJSONArray("identifiers")[0].toString())
                            }
                        } else {
                            logger.debug( "ProcessHomingSolution Exception: no matching serviceResourceIds returned in " +
                                    "homing solution")
                            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - " +
                                    "Occurred in Homing ProcessHomingSolution: no matching serviceResourceIds returned")

                        }
                    }
                }
                if (JsonUtils.jsonElementExist(response, "solutions.licenseSolutions")) {
                    String licenseSolutions = jsonUtil.getJsonValue(response, "solutions.licenseSolutions")
                    JSONArray licenseArr = new JSONArray(licenseSolutions)
                    for (int l = 0; l < licenseArr.length(); l++) {
                        JSONObject license = licenseArr.getJSONObject(l)
                        String jsonServiceResourceId = license.getString("serviceResourceId")
                        for (Resource resource : resourceList) {
                            String serviceResourceId = resource.getResourceId()
                            if (serviceResourceId.equalsIgnoreCase(jsonServiceResourceId)) {
                                String jsonEntitlementPoolList = jsonUtil.getJsonValue(license.toString(), "entitlementPoolUUID")
                                List<String> entitlementPoolList = jsonUtil.StringArrayToList(execution, jsonEntitlementPoolList)
                                resource.getHomingSolution().getLicense().setEntitlementPoolList(entitlementPoolList)

                                String jsonLicenseKeyGroupList = jsonUtil.getJsonValue(license.toString(), "licenseKeyGroupUUID")
                                List<String> licenseKeyGroupList = jsonUtil.StringArrayToList(execution, jsonLicenseKeyGroupList)
                                resource.getHomingSolution().getLicense().setLicenseKeyGroupList(licenseKeyGroupList)
                            }
                        }
                    }
                }
            }
            execution.setVariable("serviceDecomposition", decomposition)
            execution.setVariable("homingSolution", placements) //TODO - can be removed as output variable

            logger.debug( "*** Completed Homing Process Homing Solution ***")
        } catch (BpmnError b) {
            logger.debug( "ProcessHomingSolution Error: " + b)
            throw b
        } catch (Exception e) {
            logger.debug( "ProcessHomingSolution Exception: " + e)
            logger.error(e);
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occurred in Homing ProcessHomingSolution")
        }
    }

    /**
     * This method logs the start of DHVCreateService
     * to make debugging easier.
     *
     * @param - execution
     */
    public String logStart(DelegateExecution execution) {

        String requestId = execution.getVariable("testReqId")
        if (isBlank(requestId)) {
            requestId = execution.getVariable("msoRequestId")
        }
        execution.setVariable("DHVCS_requestId", requestId)
        logger.debug( "***** STARTED Homing Subflow for request: " + requestId + " *****")
    }

    /**
     * Auto-generated method stub
     */
    public void preProcessRequest(DelegateExecution execution) {}
        // Not Implemented Method

    /**
     * Constructs a workflow message callback URL for the specified message type and correlator.
     * This type of callback URL is used when a workflow wants an MSO adapter (like the SDNC
     * adapter) to call it back.  In other words, this is for callbacks internal to the MSO
     * complex.  Use <code>createWorkflowMessageAdapterCallbackURL</code> if the callback
     * will come from outside the MSO complex.
     * @param endpoint endpoint address to contruct URL from
     * @param messageType the message type (e.g. SDNCAResponse or VNFAResponse)
     * @param correlator the correlator value (e.g. a request ID)
     */
    public String createHomingCallbackURL(String endpoint, String messageType, String correlator) {
        try {
            if (endpoint == null || endpoint.isEmpty()) {
                ExceptionUtil exceptionUtil = new ExceptionUtil()
                exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
                        'mso:workflow:message:endpoint was not passed in')
            }

            logger.debug( "passed in endpoint: " + endpoint + " *****")

            while (endpoint.endsWith('/')) {
                endpoint = endpoint.substring(0, endpoint.length() - 1)
            }
            logger.debug( "processed endpoint: " + endpoint + " *****")

            return endpoint +
                    '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
                    '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
        } catch (Exception ex) {
            logger.debug( "createCallbackURL Exception: " + ex + " *****")
        }
    }
}