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;
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;
36 public class RubyClient {
38 private static final String REQUEST_CLIENT_NAME = "MSO";
39 private static final String ACTION = "Create Ticket";
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);
47 request.withRequestTime(this.getTime());
49 if (reason.length() <= 255) {
50 request.withReason(reason);
52 throw new IllegalArgumentException("reason exceeds 255 characters");
54 if (notification.length() <= 1024) {
55 request.withNotification(notification);
57 throw new IllegalArgumentException("notification exceeds 1024 characters");
59 final Event event = new Event();
60 event.setMsoRequest(request);
61 final Ruby ruby = new Ruby();
63 return this.getJson(ruby);
66 protected String getJson(Ruby obj) throws JsonProcessingException {
67 final ObjectMapper mapper = new ObjectMapper();
68 return mapper.writeValueAsString(obj);
71 protected DmaapPublisher getPublisher() throws IOException {
72 return new RubyCreateTicketRequestPublisher();
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);
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);