AT&T 1712 and 1802 release code
[so.git] / common / src / main / java / org / openecomp / mso / 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.openecomp.mso.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
28 import org.openecomp.mso.client.dmaap.DmaapPublisher;
29 import org.openecomp.mso.client.ruby.beans.Event;
30 import org.openecomp.mso.client.ruby.beans.MsoRequest;
31 import org.openecomp.mso.client.ruby.beans.Ruby;
32 import org.openecomp.mso.client.ruby.dmaap.RubyCreateTicketRequestPublisher;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37  
38 public class RubyClient {
39         
40         private static final String REQUEST_CLIENT_NAME = "MSO";
41         private static final String ACTION = "Create Ticket";
42         
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)
49                            .withAction(ACTION);
50                  
51                 request.withRequestTime(this.getTime());
52                 
53                 if(reason.length() <= 255){
54                         request.withReason(reason);
55                 } else {
56                         throw new IllegalArgumentException("reason exceeds 255 characters");
57                 }
58                 if(notification.length() <= 1024){
59                         request.withNotification(notification);
60                 } else {
61                         throw new IllegalArgumentException("notification exceeds 1024 characters");
62                 }
63                 final Event event = new Event();
64                 event.setMsoRequest(request);
65                 final Ruby ruby = new Ruby();   
66                 ruby.setEvent(event);   
67                 return this.getJson(ruby);
68         }
69         
70         protected String getJson(Ruby obj) throws JsonProcessingException {
71                 final ObjectMapper mapper = new ObjectMapper();
72                 return mapper.writeValueAsString(obj);
73         }
74         
75         protected DmaapPublisher getPublisher() throws IOException {
76                 return new RubyCreateTicketRequestPublisher();
77         }       
78         
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);
83         }
84         
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);
89         }
90 }