Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / tenantisolation / dmaap / DmaapOperationalEnvClient.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.apihandlerinfra.tenantisolation.dmaap;
22
23 import java.io.IOException;
24
25 import javax.inject.Provider;
26
27 import org.apache.http.HttpStatus;
28 import org.onap.so.apihandler.common.ErrorNumbers;
29 import org.onap.so.apihandlerinfra.exceptions.ApiException;
30 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
31 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
32 import org.onap.so.logger.MessageEnum;
33 import org.onap.so.logger.MsoLogger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 @Component
41 public class DmaapOperationalEnvClient {
42
43         @Autowired 
44         private Provider<OperationalEnvironmentPublisher> dmaapPublisher;
45         
46         protected String buildRequest(String operationalEnvironmentId, String operationalEnvironmentName, String operationalEnvironmentType, String tenantContext, String workloadContext, String action ) 
47                                         throws ApiException {
48                 final CreateEcompOperationEnvironmentBean operationalEnv = new CreateEcompOperationEnvironmentBean();
49                 operationalEnv.withOperationalEnvironmentId(operationalEnvironmentId)
50                    .withOperationalEnvironmentName(operationalEnvironmentName)          
51                            .withOperationalEnvironmentType(operationalEnvironmentType)
52                            .withTenantContext(tenantContext)
53                            .withWorkloadContext(workloadContext)
54                            .withaction(action);
55                 try {
56                         return this.getJson(operationalEnv);
57                 }catch(JsonProcessingException ex){
58                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).build();
59                         ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed : " + ex.getMessage(),
60                                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(ex).errorInfo(errorLoggerInfo).build();
61
62                         throw validateException;
63                 }
64         }
65
66         protected String getJson(CreateEcompOperationEnvironmentBean obj) throws JsonProcessingException {
67                 
68                 final ObjectMapper mapper = new ObjectMapper();
69                 return mapper.writeValueAsString(obj);
70                 
71         }
72         
73         public void dmaapPublishOperationalEnvRequest(String operationalEnvironmentId, String operationalEnvironmentName, String operationalEnvironmentType, 
74                                         String tenantContext, String workloadContext, String action ) throws ApiException, IOException, InterruptedException {
75
76                 String request = this.buildRequest(operationalEnvironmentId, operationalEnvironmentName, operationalEnvironmentType, tenantContext, workloadContext, action);
77                 dmaapPublisher.get().send(request);
78
79         }
80
81 }