Merge of new rebased code
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / messageadapter / Converter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.oam.messageadapter;
23
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;
40
41
42 import java.text.SimpleDateFormat;
43 import java.util.TimeZone;
44
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);
49     static {
50         isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
51     }
52
53
54     private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) {
55         Builder<?> outObj = null;
56         if(rpcName == null){
57             throw new IllegalArgumentException("empty asyncResponse.rpcName");
58         }
59         if(commonHeader == null){
60             throw new IllegalArgumentException("empty asyncResponse.commonHeader");
61         }
62         if(status == null){
63             throw new IllegalArgumentException("empty asyncResponse.status");
64         }
65         switch (rpcName){
66             case stop:
67                 outObj = new StopOutputBuilder();
68                 ((StopOutputBuilder)outObj).setCommonHeader(commonHeader);
69                 ((StopOutputBuilder)outObj).setStatus(status);
70                 return outObj;
71
72             case start:
73                 outObj = new StartOutputBuilder();
74                 ((StartOutputBuilder)outObj).setCommonHeader(commonHeader);
75                 ((StartOutputBuilder)outObj).setStatus(status);
76                 return outObj;
77             default:
78                 throw new IllegalArgumentException(rpcName+" action is not supported");
79         }
80     }
81
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();
86
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);
94     }
95
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();
104
105         DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
106         outObj.setBody(body);
107         return outObj;
108     }
109
110
111     abstract class MixIn {
112         @JsonIgnore
113         abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
114
115         @JsonValue
116         abstract java.lang.String getValue();
117     }
118     abstract class MixInCommonHeader extends MixIn {
119         @JsonProperty("request-id")
120         abstract java.lang.String getRequestId();
121
122         @JsonProperty("originator-id")
123         abstract java.lang.String getOriginatorId();
124
125     }
126     abstract class MixInFlagsMessage extends MixIn {
127         @JsonProperty("common-header")
128         abstract  CommonHeader getCommonHeader();
129     }
130
131
132
133 }