2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.oam.messageadapter;
27 import com.fasterxml.jackson.annotation.JsonIgnore;
28 import com.fasterxml.jackson.annotation.JsonInclude;
29 import com.fasterxml.jackson.annotation.JsonProperty;
30 import com.fasterxml.jackson.annotation.JsonValue;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.MapperFeature;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.ObjectWriter;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeOutputBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.RestartOutputBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StartOutputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopOutputBuilder;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataContainer;
43 import org.onap.appc.oam.AppcOam;
45 import java.text.SimpleDateFormat;
46 import java.util.TimeZone;
48 public class Converter {
49 private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
50 private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
53 isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
57 private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName,
58 CommonHeader commonHeader,
61 if (rpcName == null) {
62 throw new IllegalArgumentException("empty asyncResponse.rpcName");
64 if (commonHeader == null) {
65 throw new IllegalArgumentException("empty asyncResponse.commonHeader");
68 throw new IllegalArgumentException("empty asyncResponse.status");
71 case maintenance_mode:
72 outObj = new MaintenanceModeOutputBuilder();
73 ((MaintenanceModeOutputBuilder) outObj).setCommonHeader(commonHeader);
74 ((MaintenanceModeOutputBuilder) outObj).setStatus(status);
78 outObj = new StartOutputBuilder();
79 ((StartOutputBuilder) outObj).setCommonHeader(commonHeader);
80 ((StartOutputBuilder) outObj).setStatus(status);
84 outObj = new StopOutputBuilder();
85 ((StopOutputBuilder) outObj).setCommonHeader(commonHeader);
86 ((StopOutputBuilder) outObj).setStatus(status);
90 outObj = new RestartOutputBuilder();
91 ((RestartOutputBuilder) outObj).setCommonHeader(commonHeader);
92 ((RestartOutputBuilder) outObj).setStatus(status);
96 throw new IllegalArgumentException(rpcName + " action is not supported");
100 static String convAsyncResponseToUebOutgoingMessageJsonString(OAMContext oamContext) throws
101 JsonProcessingException {
102 AppcOam.RPC rpcName = oamContext.getRpcName();
103 CommonHeader commonHeader = oamContext.getCommonHeader();
104 Status status = oamContext.getStatus();
106 DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName, commonHeader,
108 ObjectMapper objectMapper = new ObjectMapper();
109 objectMapper.addMixInAnnotations(dmaapOutgoingMessage.getBody().getOutput().getClass(),
110 MixInFlagsMessage.class);
111 objectMapper.addMixInAnnotations(Status.class, MixIn.class);
112 objectMapper.addMixInAnnotations(CommonHeader.class, MixInCommonHeader.class);
113 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
114 (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer();
115 return writer.writeValueAsString(dmaapOutgoingMessage);
118 private static DmaapOutgoingMessage convAsyncResponseToUebOutgoingMessage(AppcOam.RPC rpcName, CommonHeader
119 commonHeader, Status status) throws JsonProcessingException {
120 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
121 String correlationID = commonHeader.getRequestId();
122 outObj.setCorrelationID(correlationID);
123 outObj.setType("response");
124 outObj.setRpcName(rpcName.name());
125 Builder<?> builder = Converter.convAsyncResponseToBuilder1(rpcName, commonHeader, status);
126 Object messageBody = builder.build();
128 DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
129 outObj.setBody(body);
134 abstract class MixIn {
135 // to be removed during serialization
137 abstract Class<? extends DataContainer> getImplementedInterface();
140 abstract java.lang.String getValue();
143 abstract class MixInCommonHeader extends MixIn {
144 @JsonProperty("request-id")
145 abstract java.lang.String getRequestId();
147 @JsonProperty("originator-id")
148 abstract java.lang.String getOriginatorId();
152 abstract class MixInFlagsMessage extends MixIn {
153 @JsonProperty("common-header")
154 abstract CommonHeader getCommonHeader();