Moved base client to new location
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / oof / OofClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Intel Corp.  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.client.oof;
22
23
24 import org.camunda.bpm.engine.delegate.BpmnError;
25 import org.onap.so.bpmn.core.UrnPropertiesReader;
26 import org.onap.so.client.BaseClient;
27 import org.onap.so.client.exception.BadResponseException;
28 import org.onap.so.client.oof.beans.OofProperties;
29 import org.onap.so.client.oof.beans.OofRequest;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.ParameterizedTypeReference;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.stereotype.Component;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import java.util.LinkedHashMap;
39
40 @Component
41 public class OofClient {
42
43     private static final Logger logger = LoggerFactory.getLogger(OofClient.class);
44     public static final String X_MINOR_VERSION = "X-MinorVersion";
45     public static final String X_PATCH_VERSION = "X-PatchVersion";
46     public static final String X_LATEST_VERSION = "X-LatestVersion";
47
48     @Autowired
49     private OofProperties oofProperties;
50
51     @Autowired
52     private OofValidator validator;
53
54
55     /**
56      * Makes a rest call to oof to perform homing and licensing for a list of demands
57      *
58      * @param homingRequest
59      * @return
60      * @throws JsonProcessingException
61      * @throws BpmnError
62      */
63     public void postDemands(OofRequest homingRequest) throws BadResponseException, JsonProcessingException {
64         logger.trace("Started oof Client Post Demands");
65         String url = oofProperties.getHost() + oofProperties.getUri();
66         logger.debug("Post demands url: " + url);
67         logger.debug("Post demands payload: " + homingRequest.toJsonString());
68
69         HttpHeaders header = new HttpHeaders();
70         header.setContentType(MediaType.APPLICATION_JSON);
71         header.set(HttpHeaders.AUTHORIZATION, oofProperties.getHeaders().get("auth"));
72         header.set(X_PATCH_VERSION, oofProperties.getHeaders().get("patchVersion"));
73         header.set(X_MINOR_VERSION, oofProperties.getHeaders().get("minorVersion"));
74         header.set(X_LATEST_VERSION, oofProperties.getHeaders().get("latestVersion"));
75         BaseClient<String, LinkedHashMap<?, ?>> baseClient = new BaseClient<>();
76
77         baseClient.setTargetUrl(url);
78         baseClient.setHttpHeader(header);
79
80         LinkedHashMap<?, ?> response =
81                 baseClient.post(homingRequest.toJsonString(), new ParameterizedTypeReference<LinkedHashMap<?, ?>>() {});
82         validator.validateDemandsResponse(response);
83         logger.trace("Completed OOF Client Post Demands");
84     }
85 }