Changed to unmaintained
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.oam.messageadapter;
25
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27 import com.fasterxml.jackson.annotation.JsonInclude;
28 import com.fasterxml.jackson.annotation.JsonProperty;
29 import com.fasterxml.jackson.annotation.JsonValue;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.MapperFeature;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.ObjectWriter;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeOutputBuilder;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.RestartOutputBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StartOutputBuilder;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopOutputBuilder;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.DataContainer;
42 import org.onap.appc.oam.AppcOam;
43
44 import java.text.SimpleDateFormat;
45 import java.util.TimeZone;
46
47 public class Converter {
48     private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
49     private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
50
51     static {
52         isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
53     }
54
55
56     private static Builder<?> convAsyncResponseToBuilder1(AppcOam.RPC rpcName,
57                                                           CommonHeader commonHeader,
58                                                           Status status) {
59         Builder<?> outObj;
60         if (rpcName == null) {
61             throw new IllegalArgumentException("empty asyncResponse.rpcName");
62         }
63         if (commonHeader == null) {
64             throw new IllegalArgumentException("empty asyncResponse.commonHeader");
65         }
66         if (status == null) {
67             throw new IllegalArgumentException("empty asyncResponse.status");
68         }
69         switch (rpcName) {
70             case maintenance_mode:
71                 outObj = new MaintenanceModeOutputBuilder();
72                 ((MaintenanceModeOutputBuilder) outObj).setCommonHeader(commonHeader);
73                 ((MaintenanceModeOutputBuilder) outObj).setStatus(status);
74                 return outObj;
75
76             case start:
77                 outObj = new StartOutputBuilder();
78                 ((StartOutputBuilder) outObj).setCommonHeader(commonHeader);
79                 ((StartOutputBuilder) outObj).setStatus(status);
80                 return outObj;
81
82             case stop:
83                 outObj = new StopOutputBuilder();
84                 ((StopOutputBuilder) outObj).setCommonHeader(commonHeader);
85                 ((StopOutputBuilder) outObj).setStatus(status);
86                 return outObj;
87
88             case restart:
89                 outObj = new RestartOutputBuilder();
90                 ((RestartOutputBuilder) outObj).setCommonHeader(commonHeader);
91                 ((RestartOutputBuilder) outObj).setStatus(status);
92                 return outObj;
93
94             default:
95                 throw new IllegalArgumentException(rpcName + " action is not supported");
96         }
97     }
98
99     static String convAsyncResponseToUebOutgoingMessageJsonString(OAMContext oamContext) throws
100             JsonProcessingException {
101         AppcOam.RPC rpcName = oamContext.getRpcName();
102         CommonHeader commonHeader = oamContext.getCommonHeader();
103         Status status = oamContext.getStatus();
104
105         DmaapOutgoingMessage dmaapOutgoingMessage = convAsyncResponseToUebOutgoingMessage(rpcName, commonHeader,
106                 status);
107         ObjectMapper objectMapper = new ObjectMapper();
108         objectMapper.addMixIn(dmaapOutgoingMessage.getBody().getOutput().getClass(),
109                 MixInFlagsMessage.class);
110         objectMapper.addMixIn(Status.class, MixIn.class);
111         objectMapper.addMixIn(CommonHeader.class, MixInCommonHeader.class);
112         ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL).configure
113                 (MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true).writer();
114         return writer.writeValueAsString(dmaapOutgoingMessage);
115     }
116
117     private static DmaapOutgoingMessage convAsyncResponseToUebOutgoingMessage(AppcOam.RPC rpcName, CommonHeader
118             commonHeader, Status status) throws JsonProcessingException {
119         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
120         String correlationID = commonHeader.getRequestId();
121         outObj.setCorrelationID(correlationID);
122         outObj.setType("response");
123         outObj.setRpcName(rpcName.name());
124         Builder<?> builder = Converter.convAsyncResponseToBuilder1(rpcName, commonHeader, status);
125         Object messageBody = builder.build();
126
127         DmaapOutgoingMessage.Body body = new DmaapOutgoingMessage.Body(messageBody);
128         outObj.setBody(body);
129         return outObj;
130     }
131
132
133     abstract class MixIn {
134         // to be removed during serialization
135         @JsonIgnore
136         abstract Class<? extends DataContainer> getImplementedInterface();
137
138         @JsonValue
139         abstract java.lang.String getValue();
140     }
141
142     abstract class MixInCommonHeader extends MixIn {
143         @JsonProperty("request-id")
144         abstract java.lang.String getRequestId();
145
146         @JsonProperty("originator-id")
147         abstract java.lang.String getOriginatorId();
148
149     }
150
151     abstract class MixInFlagsMessage extends MixIn {
152         @JsonProperty("common-header")
153         abstract CommonHeader getCommonHeader();
154     }
155 }