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
|
/*-/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* 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.
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.onap.appc.oam.messageadapter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.RestartOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StartOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.onap.appc.oam.AppcOam;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class Converter {
private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
static {
isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName,
CommonHeader commonHeader,
Status status) {
Builder<?> outObj;
if (rpcName == null) {
throw new IllegalArgumentException("empty asyncResponse.rpcName");
}
if (commonHeader == null) {
throw new IllegalArgumentException("empty asyncResponse.commonHeader");
}
if (status == null) {
throw new IllegalArgumentException("empty asyncResponse.status");
}
switch (rpcName) {
case maintenance_mode:
outObj = new MaintenanceModeOutputBuilder();
((MaintenanceModeOutputBuilder) outObj).setCommonHeader(commonHeader);
((MaintenanceModeOutputBuilder) outObj).setStatus(status);
return outObj;
case start:
outObj = new StartOutputBuilder();
((StartOutputBuilder) outObj).setCommonHeader(commonHeader);
((StartOutputBuilder) outObj).setStatus(status);
return outObj;
case stop:
outObj = new StopOutputBuilder();
((StopOutputBuilder) outObj).setCommonHeader(commonHeader);
((StopOutputBuilder) outObj).setStatus(status);
return outObj;
case restart:
outObj = new RestartOutputBuilder();
((RestartOutputBuilder) outObj).setCommonHeader(commonHeader);
((RestartOutputBuilder) outObj).setStatus(status);
return outObj;
default:
throw new IllegalArgumentException(rpcName + " action is not supported");
}
}
static String convAsyncResponseToUebOutgoingMessageJsonString(OAMContext oamContext) throws
JsonProcessingException {
AppcOam.RPC rpcName = oamContext.getRpcName();
CommonHeader commonHeader = oamContext.getCommonHeader();
Status status = oamContext.getStatus();
DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName, commonHeader,
status);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixInAnnotations(dmaapOutgoingMessage.getBody().getOutput().getClass(),
MixInFlagsMessage.class);
objectMapper.addMixInAnnotations(Status.class, MixIn.class);
objectMapper.addMixInAnnotations(CommonHeader.class, MixInCommonHeader.class);
ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer();
return writer.writeValueAsString(dmaapOutgoingMessage);
}
private static DmaapOutgoingMessage convAsyncResponseToUebOutgoingMessage(AppcOam.RPC rpcName, CommonHeader
commonHeader, Status status) throws JsonProcessingException {
DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
String correlationID = commonHeader.getRequestId();
outObj.setCorrelationID(correlationID);
outObj.setType("response");
outObj.setRpcName(rpcName.name());
Builder<?> builder = Converter.convAsyncResponseToBuilder1(rpcName, commonHeader, status);
Object messageBody = builder.build();
DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
outObj.setBody(body);
return outObj;
}
abstract class MixIn {
// to be removed during serialization
@JsonIgnore
abstract Class<? extends DataContainer> getImplementedInterface();
@JsonValue
abstract java.lang.String getValue();
}
abstract class MixInCommonHeader extends MixIn {
@JsonProperty("request-id")
abstract java.lang.String getRequestId();
@JsonProperty("originator-id")
abstract java.lang.String getOriginatorId();
}
abstract class MixInFlagsMessage extends MixIn {
@JsonProperty("common-header")
abstract CommonHeader getCommonHeader();
}
}
|