Applying license changes to all files
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / messageadapter / Converter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.oam.messageadapter;
26
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;
43
44
45 import java.text.SimpleDateFormat;
46 import java.util.TimeZone;
47
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);
52     static {
53         isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
54     }
55
56
57     private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName, CommonHeader commonHeader, Status status) {
58         Builder<?> outObj = null;
59         if(rpcName == null){
60             throw new IllegalArgumentException("empty asyncResponse.rpcName");
61         }
62         if(commonHeader == null){
63             throw new IllegalArgumentException("empty asyncResponse.commonHeader");
64         }
65         if(status == null){
66             throw new IllegalArgumentException("empty asyncResponse.status");
67         }
68         switch (rpcName){
69             case stop:
70                 outObj = new StopOutputBuilder();
71                 ((StopOutputBuilder)outObj).setCommonHeader(commonHeader);
72                 ((StopOutputBuilder)outObj).setStatus(status);
73                 return outObj;
74
75             case start:
76                 outObj = new StartOutputBuilder();
77                 ((StartOutputBuilder)outObj).setCommonHeader(commonHeader);
78                 ((StartOutputBuilder)outObj).setStatus(status);
79                 return outObj;
80             default:
81                 throw new IllegalArgumentException(rpcName+" action is not supported");
82         }
83     }
84
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();
89
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);
97     }
98
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();
107
108         DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
109         outObj.setBody(body);
110         return outObj;
111     }
112
113
114     abstract class MixIn {
115         @JsonIgnore
116         abstract Class<? extends DataContainer> getImplementedInterface(); // to be removed during serialization
117
118         @JsonValue
119         abstract java.lang.String getValue();
120     }
121     abstract class MixInCommonHeader extends MixIn {
122         @JsonProperty("request-id")
123         abstract java.lang.String getRequestId();
124
125         @JsonProperty("originator-id")
126         abstract java.lang.String getOriginatorId();
127
128     }
129     abstract class MixInFlagsMessage extends MixIn {
130         @JsonProperty("common-header")
131         abstract  CommonHeader getCommonHeader();
132     }
133
134
135
136 }