2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.oam.messageadapter;
24 import org.openecomp.appc.oam.AppcOam;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
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.openecomp.appc.oam.rev170303.*;
36 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
37 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
38 import org.opendaylight.yangtools.concepts.Builder;
39 import org.opendaylight.yangtools.yang.binding.DataContainer;
42 import java.text.SimpleDateFormat;
43 import java.util.TimeZone;
45 public class Converter {
46 private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
47 private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Converter.class);
50 isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
54 private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) {
55 Builder<?> outObj = null;
57 throw new IllegalArgumentException("empty asyncResponse.rpcName");
59 if(commonHeader == null){
60 throw new IllegalArgumentException("empty asyncResponse.commonHeader");
63 throw new IllegalArgumentException("empty asyncResponse.status");
67 outObj = new StopOutputBuilder();
68 ((StopOutputBuilder)outObj).setCommonHeader(commonHeader);
69 ((StopOutputBuilder)outObj).setStatus(status);
73 outObj = new StartOutputBuilder();
74 ((StartOutputBuilder)outObj).setCommonHeader(commonHeader);
75 ((StartOutputBuilder)outObj).setStatus(status);
78 throw new IllegalArgumentException(rpcName+" action is not supported");
82 public static String convAsyncResponseToUebOutgoingMessageJsonString(OAMContext oamContext) throws JsonProcessingException {
83 AppcOam.RPC rpcName = oamContext.getRpcName();
84 CommonHeader commonHeader = oamContext.getCommonHeader();
85 Status status = oamContext.getStatus();
87 DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName,commonHeader,status);
88 ObjectMapper objectMapper = new ObjectMapper();
89 objectMapper.addMixInAnnotations(dmaapOutgoingMessage.getBody().getOutput().getClass(), MixInFlagsMessage.class);
90 objectMapper.addMixInAnnotations(Status.class, MixIn.class);
91 objectMapper.addMixInAnnotations(CommonHeader.class, MixInCommonHeader.class);
92 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true).writer();
93 return writer.writeValueAsString(dmaapOutgoingMessage);
96 private static DmaapOutgoingMessage convAsyncResponseToUebOutgoingMessage(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) throws JsonProcessingException {
97 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
98 String correlationID = commonHeader.getRequestId();
99 outObj.setCorrelationID(correlationID);
100 outObj.setType("response");
101 outObj.setRpcName(rpcName.name());
102 Builder<?> builder = Converter.convAsyncResponseToBuilder1(rpcName,commonHeader,status);
103 Object messageBody = builder.build();
105 DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
106 outObj.setBody(body);
111 abstract class MixIn {
113 abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
116 abstract java.lang.String getValue();
118 abstract class MixInCommonHeader extends MixIn {
119 @JsonProperty("request-id")
120 abstract java.lang.String getRequestId();
122 @JsonProperty("originator-id")
123 abstract java.lang.String getOriginatorId();
126 abstract class MixInFlagsMessage extends MixIn {
127 @JsonProperty("common-header")
128 abstract CommonHeader getCommonHeader();