aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/BluePrintMappings.kt
blob: c344ca00ae793b5d2e7e62f46baf8743066e5d4f (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
/*
 * Copyright (C) 2019 Bell Canada.
 *
 * 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.
 */
package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils

import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.common.base.Strings
import com.google.protobuf.Struct
import com.google.protobuf.util.JsonFormat
import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers
import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
import org.onap.ccsdk.apps.controllerblueprints.common.api.EventType
import org.onap.ccsdk.apps.controllerblueprints.common.api.Flag
import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput
import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput
import java.text.SimpleDateFormat
import java.util.*

private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

// ACTION IDENTIFIER

fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
    val actionIdentifier = ActionIdentifiers.newBuilder()
    actionIdentifier.actionName = this.actionName
    actionIdentifier.blueprintName = this.blueprintName
    actionIdentifier.blueprintVersion = this.blueprintVersion
    actionIdentifier.mode = this.mode
    return actionIdentifier.build()
}

fun ActionIdentifiers.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers {
    val actionIdentifier = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers()
    actionIdentifier.actionName = this.actionName
    actionIdentifier.blueprintName = this.blueprintName
    actionIdentifier.blueprintVersion = this.blueprintVersion
    actionIdentifier.mode = this.mode
    return actionIdentifier
}

// COMMON HEADER

fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader.toProto(): CommonHeader {
    val commonHeader = CommonHeader.newBuilder()
    commonHeader.originatorId = this.originatorId
    commonHeader.requestId = this.requestId
    commonHeader.subRequestId = this.subRequestId
    commonHeader.timestamp = this.timestamp.toString()
    commonHeader.flag = this.flags?.toProto()
    return commonHeader.build()
}

fun CommonHeader.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader {
    val commonHeader = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader()
    commonHeader.originatorId = this.originatorId
    commonHeader.requestId = this.requestId
    commonHeader.subRequestId = this.subRequestId
    commonHeader.timestamp = if (!Strings.isNullOrEmpty(this.timestamp)) {
        formatter.parse(this.timestamp)
    } else {
        Date()
    }
    commonHeader.flags = this.flag?.toJava()
    return commonHeader
}

// FLAG

fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
    val flag = Flag.newBuilder()
    flag.isForce = this.isForce
    flag.ttl = this.ttl
    return flag.build()
}

fun Flag.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags {
    val flag = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags()
    flag.isForce = this.isForce
    flag.ttl = this.ttl
    return flag
}

// STATUS

fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status.toProto(): Status {
    val status = Status.newBuilder()
    status.code = this.code
    status.errorMessage = this.errorMessage ?: ""
    status.message = this.message
    status.timestamp = this.timestamp.toString()
    status.eventType = EventType.valueOf(this.eventType)
    return status.build()
}

// EXECUTION INPUT

fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput {
    val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
    executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
    executionServiceInput.commonHeader = this.commonHeader.toJava()
    executionServiceInput.payload = JacksonUtils.jsonNode(JsonFormat.printer().print(this.payload)) as ObjectNode
    return executionServiceInput
}

// EXECUTION OUPUT

fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(): ExecutionServiceOutput {
    val executionServiceOuput = ExecutionServiceOutput.newBuilder()
    executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
    executionServiceOuput.commonHeader = this.commonHeader.toProto()
    executionServiceOuput.status = this.status.toProto()
    val struct = Struct.newBuilder()
    JsonFormat.parser().merge(JacksonUtils.getJson(this.payload), struct)
    executionServiceOuput.payload = struct.build()
    return executionServiceOuput.build()
}