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