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.openecomp.appc.oam.messageadapter;
27 import org.openecomp.appc.oam.AppcOam;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31 import com.fasterxml.jackson.annotation.JsonInclude;
32 import com.fasterxml.jackson.annotation.JsonProperty;
33 import com.fasterxml.jackson.annotation.JsonValue;
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.MapperFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.ObjectWriter;
38 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.*;
39 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
40 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataContainer;
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);
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(Converter.class);
53 isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
57 private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) {
58 Builder<?> outObj = null;
60 throw new IllegalArgumentException("empty asyncResponse.rpcName");
62 if(commonHeader == null){
63 throw new IllegalArgumentException("empty asyncResponse.commonHeader");
66 throw new IllegalArgumentException("empty asyncResponse.status");
70 outObj = new StopOutputBuilder();
71 ((StopOutputBuilder)outObj).setCommonHeader(commonHeader);
72 ((StopOutputBuilder)outObj).setStatus(status);
76 outObj = new StartOutputBuilder();
77 ((StartOutputBuilder)outObj).setCommonHeader(commonHeader);
78 ((StartOutputBuilder)outObj).setStatus(status);
81 throw new IllegalArgumentException(rpcName+" action is not supported");
85 public static String convAsyncResponseToUebOutgoingMessageJsonString(OAMContext oamContext) throws JsonProcessingException {
86 AppcOam.RPC rpcName = oamContext.getRpcName();
87 CommonHeader commonHeader = oamContext.getCommonHeader();
88 Status status = oamContext.getStatus();
90 DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName,commonHeader,status);
91 ObjectMapper objectMapper = new ObjectMapper();
92 objectMapper.addMixInAnnotations(dmaapOutgoingMessage.getBody().getOutput().getClass(), MixInFlagsMessage.class);
93 objectMapper.addMixInAnnotations(Status.class, MixIn.class);
94 objectMapper.addMixInAnnotations(CommonHeader.class, MixInCommonHeader.class);
95 ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY,true).writer();
96 return writer.writeValueAsString(dmaapOutgoingMessage);
99 private static DmaapOutgoingMessage convAsyncResponseToUebOutgoingMessage(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) throws JsonProcessingException {
100 DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
101 String correlationID = commonHeader.getRequestId();
102 outObj.setCorrelationID(correlationID);
103 outObj.setType("response");
104 outObj.setRpcName(rpcName.name());
105 Builder<?> builder = Converter.convAsyncResponseToBuilder1(rpcName,commonHeader,status);
106 Object messageBody = builder.build();
108 DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
109 outObj.setBody(body);
114 abstract class MixIn {
116 abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
119 abstract java.lang.String getValue();
121 abstract class MixInCommonHeader extends MixIn {
122 @JsonProperty("request-id")
123 abstract java.lang.String getRequestId();
125 @JsonProperty("originator-id")
126 abstract java.lang.String getOriginatorId();
129 abstract class MixInFlagsMessage extends MixIn {
130 @JsonProperty("common-header")
131 abstract CommonHeader getCommonHeader();