Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / onap / 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.onap.appc.oam.messageadapter;
26
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;
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
52     static {
53         isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
54     }
55
56
57     private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName,
58                                                           CommonHeader commonHeader,
59                                                           Status status) {
60         Builder<?> outObj;
61         if (rpcName == null) {
62             throw new IllegalArgumentException("empty asyncResponse.rpcName");
63         }
64         if (commonHeader == null) {
65             throw new IllegalArgumentException("empty asyncResponse.commonHeader");
66         }
67         if (status == null) {
68             throw new IllegalArgumentException("empty asyncResponse.status");
69         }
70         switch (rpcName) {
71             case maintenance_mode:
72                 outObj = new MaintenanceModeOutputBuilder();
73                 ((MaintenanceModeOutputBuilder) outObj).setCommonHeader(commonHeader);
74                 ((MaintenanceModeOutputBuilder) outObj).setStatus(status);
75                 return outObj;
76
77             case start:
78                 outObj = new StartOutputBuilder();
79                 ((StartOutputBuilder) outObj).setCommonHeader(commonHeader);
80                 ((StartOutputBuilder) outObj).setStatus(status);
81                 return outObj;
82
83             case stop:
84                 outObj = new StopOutputBuilder();
85                 ((StopOutputBuilder) outObj).setCommonHeader(commonHeader);
86                 ((StopOutputBuilder) outObj).setStatus(status);
87                 return outObj;
88
89             case restart:
90                 outObj = new RestartOutputBuilder();
91                 ((RestartOutputBuilder) outObj).setCommonHeader(commonHeader);
92                 ((RestartOutputBuilder) outObj).setStatus(status);
93                 return outObj;
94
95             default:
96                 throw new IllegalArgumentException(rpcName + " action is not supported");
97         }
98     }
99
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();
105
106         DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName, commonHeader,
107                 status);
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);
116     }
117
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();
127
128         DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
129         outObj.setBody(body);
130         return outObj;
131     }
132
133
134     abstract class MixIn {
135         // to be removed during serialization
136         @JsonIgnore
137         abstract Class<? extends DataContainer> getImplementedInterface();
138
139         @JsonValue
140         abstract java.lang.String getValue();
141     }
142
143     abstract class MixInCommonHeader extends MixIn {
144         @JsonProperty("request-id")
145         abstract java.lang.String getRequestId();
146
147         @JsonProperty("originator-id")
148         abstract java.lang.String getOriginatorId();
149
150     }
151
152     abstract class MixInFlagsMessage extends MixIn {
153         @JsonProperty("common-header")
154         abstract CommonHeader getCommonHeader();
155     }
156 }