Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / ruby / RubyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.ruby;
22
23 import java.io.IOException;
24 import java.time.ZoneOffset;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27 import org.onap.so.client.dmaap.DmaapPublisher;
28 import org.onap.so.client.ruby.beans.Event;
29 import org.onap.so.client.ruby.beans.MsoRequest;
30 import org.onap.so.client.ruby.beans.Ruby;
31 import org.onap.so.client.ruby.dmaap.RubyCreateTicketRequestPublisher;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35
36 public class RubyClient {
37
38     private static final String REQUEST_CLIENT_NAME = "MSO";
39     private static final String ACTION = "Create Ticket";
40
41     protected String buildRequest(String requestId, String sourceName, String reason, String workflowId,
42             String notification) throws JsonProcessingException {
43         final MsoRequest request = new MsoRequest();
44         request.withRequestClientName(REQUEST_CLIENT_NAME).withRequestId(requestId).withSourceName(sourceName)
45                 .withWorkflowId(workflowId).withAction(ACTION);
46
47         request.withRequestTime(this.getTime());
48
49         if (reason.length() <= 255) {
50             request.withReason(reason);
51         } else {
52             throw new IllegalArgumentException("reason exceeds 255 characters");
53         }
54         if (notification.length() <= 1024) {
55             request.withNotification(notification);
56         } else {
57             throw new IllegalArgumentException("notification exceeds 1024 characters");
58         }
59         final Event event = new Event();
60         event.setMsoRequest(request);
61         final Ruby ruby = new Ruby();
62         ruby.setEvent(event);
63         return this.getJson(ruby);
64     }
65
66     protected String getJson(Ruby obj) throws JsonProcessingException {
67         final ObjectMapper mapper = new ObjectMapper();
68         return mapper.writeValueAsString(obj);
69     }
70
71     protected DmaapPublisher getPublisher() throws IOException {
72         return new RubyCreateTicketRequestPublisher();
73     }
74
75     protected String getTime() {
76         final ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneOffset.UTC);
77         final DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z");
78         return currentDateTime.format(format);
79     }
80
81     public void rubyCreateTicketCheckRequest(String requestId, String sourceName, String reason, String workflowId,
82             String notification) throws Exception {
83         String request = this.buildRequest(requestId, sourceName, reason, workflowId, notification);
84         final DmaapPublisher publisher = this.getPublisher();
85         publisher.send(request);
86     }
87 }