Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sniro / SniroClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.sniro;
24
25 import java.util.LinkedHashMap;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.onap.so.bpmn.common.baseclient.BaseClient;
28 import org.onap.so.bpmn.core.UrnPropertiesReader;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.onap.so.client.sniro.beans.ManagerProperties;
31 import org.onap.so.client.sniro.beans.SniroConductorRequest;
32 import org.onap.so.client.sniro.beans.SniroManagerRequest;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.core.ParameterizedTypeReference;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.MediaType;
39 import org.springframework.stereotype.Component;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41
42
43 @Component
44 public class SniroClient {
45
46     private static final Logger logger = LoggerFactory.getLogger(SniroClient.class);
47
48     @Autowired
49     private ManagerProperties managerProperties;
50
51     @Autowired
52     private SniroValidator validator;
53
54
55     /**
56      * Makes a rest call to sniro manager 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(SniroManagerRequest homingRequest) throws BadResponseException, JsonProcessingException {
64         logger.trace("Started Sniro Client Post Demands");
65         String url = managerProperties.getHost() + managerProperties.getUri().get("v2");
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("Authorization", managerProperties.getHeaders().get("auth"));
72         header.set("X-patchVersion", managerProperties.getHeaders().get("patchVersion"));
73         header.set("X-minorVersion", managerProperties.getHeaders().get("minorVersion"));
74         header.set("X-latestVersion", managerProperties.getHeaders().get("latestVersion"));
75         BaseClient<String, LinkedHashMap<String, Object>> baseClient = new BaseClient<>();
76
77         baseClient.setTargetUrl(url);
78         baseClient.setHttpHeader(header);
79
80         LinkedHashMap<String, Object> response = baseClient.post(homingRequest.toJsonString(),
81                 new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
82         validator.validateDemandsResponse(response);
83         logger.trace("Completed Sniro Client Post Demands");
84     }
85
86     /**
87      * Makes a rest call to sniro conductor to notify them of successful or unsuccessful vnf creation for previously
88      * homed resources
89      *
90      * TODO Temporarily being used in groovy therefore can not utilize autowire. Once java "release" subflow is
91      * developed it will be refactored to use autowire.
92      *
93      * @param releaseRequest
94      * @return
95      * @throws BadResponseException
96      */
97     public void postRelease(SniroConductorRequest releaseRequest) throws BadResponseException {
98         logger.trace("Started Sniro Client Post Release");
99         String url = UrnPropertiesReader.getVariable("sniro.conductor.host")
100                 + UrnPropertiesReader.getVariable("sniro.conductor.uri");
101         logger.debug("Post release url: {}", url);
102         logger.debug("Post release payload: {}", releaseRequest.toJsonString());
103
104         HttpHeaders header = new HttpHeaders();
105         header.setContentType(MediaType.APPLICATION_JSON);
106         header.set("Authorization", UrnPropertiesReader.getVariable("sniro.conductor.headers.auth"));
107         BaseClient<String, LinkedHashMap<String, Object>> baseClient = new BaseClient<>();
108
109         baseClient.setTargetUrl(url);
110         baseClient.setHttpHeader(header);
111
112         LinkedHashMap<String, Object> response = baseClient.post(releaseRequest.toJsonString(),
113                 new ParameterizedTypeReference<LinkedHashMap<String, Object>>() {});
114         SniroValidator v = new SniroValidator();
115         v.validateReleaseResponse(response);
116         logger.trace("Completed Sniro Client Post Release");
117     }
118
119 }