2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.ruby;
23 import java.io.IOException;
24 import java.time.ZoneOffset;
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
28 import org.onap.so.client.dmaap.DmaapPublisher;
29 import org.onap.so.client.ruby.beans.Event;
30 import org.onap.so.client.ruby.beans.MsoRequest;
31 import org.onap.so.client.ruby.beans.Ruby;
32 import org.onap.so.client.ruby.dmaap.RubyCreateTicketRequestPublisher;
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
38 public class RubyClient {
40 private static final String REQUEST_CLIENT_NAME = "MSO";
41 private static final String ACTION = "Create Ticket";
43 protected String buildRequest(String requestId, String sourceName, String reason, String workflowId, String notification) throws JsonProcessingException {
44 final MsoRequest request = new MsoRequest();
45 request.withRequestClientName(REQUEST_CLIENT_NAME)
46 .withRequestId(requestId)
47 .withSourceName(sourceName)
48 .withWorkflowId(workflowId)
51 request.withRequestTime(this.getTime());
53 if(reason.length() <= 255){
54 request.withReason(reason);
56 throw new IllegalArgumentException("reason exceeds 255 characters");
58 if(notification.length() <= 1024){
59 request.withNotification(notification);
61 throw new IllegalArgumentException("notification exceeds 1024 characters");
63 final Event event = new Event();
64 event.setMsoRequest(request);
65 final Ruby ruby = new Ruby();
67 return this.getJson(ruby);
70 protected String getJson(Ruby obj) throws JsonProcessingException {
71 final ObjectMapper mapper = new ObjectMapper();
72 return mapper.writeValueAsString(obj);
75 protected DmaapPublisher getPublisher() throws IOException {
76 return new RubyCreateTicketRequestPublisher();
79 protected String getTime() {
80 final ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneOffset.UTC);
81 final DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z");
82 return currentDateTime.format(format);
85 public void rubyCreateTicketCheckRequest(String requestId, String sourceName, String reason, String workflowId, String notification) throws Exception {
86 String request = this.buildRequest(requestId, sourceName, reason, workflowId, notification);
87 final DmaapPublisher publisher = this.getPublisher();
88 publisher.send(request);