fa039c235cf6c80020a2b20b565a1b2cc408561b
[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.common.baseclient.BaseClient;
26 import org.onap.so.bpmn.core.UrnPropertiesReader;
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
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import java.util.LinkedHashMap;
40
41 @Component
42 public class OofClient {
43
44     private static final Logger logger = LoggerFactory.getLogger(OofClient.class);
45     public static final String X_MINOR_VERSION = "X-MinorVersion";
46     public static final String X_PATCH_VERSION = "X-PatchVersion";
47     public static final String X_LATEST_VERSION = "X-LatestVersion";
48
49     @Autowired
50     private OofProperties oofProperties;
51
52     @Autowired
53     private OofValidator validator;
54
55
56     /**
57      * Makes a rest call to oof to perform homing and licensing for a
58      * list of demands
59      *
60      * @param homingRequest
61      * @return
62      * @throws JsonProcessingException
63      * @throws BpmnError
64      */
65     public void postDemands(OofRequest homingRequest) throws BadResponseException, JsonProcessingException{
66         logger.trace("Started oof Client Post Demands");
67         String url = oofProperties.getHost() + oofProperties.getUri();
68         logger.debug("Post demands url: " + url);
69         logger.debug("Post demands payload: " + homingRequest.toJsonString());
70
71         HttpHeaders header = new HttpHeaders();
72         header.setContentType(MediaType.APPLICATION_JSON);
73         header.set(HttpHeaders.AUTHORIZATION, oofProperties.getHeaders().get("auth"));
74         header.set(X_PATCH_VERSION, oofProperties.getHeaders().get("patchVersion"));
75         header.set(X_MINOR_VERSION, oofProperties.getHeaders().get("minorVersion"));
76         header.set(X_LATEST_VERSION, oofProperties.getHeaders().get("latestVersion"));
77         BaseClient<String, LinkedHashMap<?, ?>> baseClient = new BaseClient<>();
78
79         baseClient.setTargetUrl(url);
80         baseClient.setHttpHeader(header);
81
82         LinkedHashMap<?, ?> response = baseClient.post(homingRequest.toJsonString(), new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
83         validator.validateDemandsResponse(response);
84         logger.trace("Completed OOF Client Post Demands");
85     }
86 }