Containerization feature of SO
[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  * 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.sniro;
22
23 import java.util.LinkedHashMap;
24
25 import org.camunda.bpm.engine.delegate.BpmnError;
26 import org.onap.so.bpmn.core.UrnPropertiesReader;
27 import org.onap.so.client.exception.BadResponseException;
28 import org.onap.so.client.sdnc.BaseClient;
29 import org.onap.so.client.sniro.beans.ManagerProperties;
30 import org.onap.so.client.sniro.beans.SniroConductorRequest;
31 import org.onap.so.client.sniro.beans.SniroManagerRequest;
32 import org.onap.so.logger.MsoLogger;
33 import org.springframework.beans.factory.annotation.Autowired;
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
40
41 @Component
42 public class SniroClient {
43
44         private static final MsoLogger log = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroClient.class);
45
46         @Autowired
47         private ManagerProperties managerProperties;
48
49         @Autowired
50         private SniroValidator validator;
51
52
53         /**
54          * Makes a rest call to sniro manager to perform homing and licensing for a
55          * list of demands
56          *
57          * @param homingRequest
58          * @return
59          * @throws JsonProcessingException
60          * @throws BpmnError
61          */
62         public void postDemands(SniroManagerRequest homingRequest) throws BadResponseException, JsonProcessingException{
63                 log.trace("Started Sniro Client Post Demands");
64                 String url = managerProperties.getHost() + managerProperties.getUri().get("v2");
65                 log.debug("Post demands url: " + url);
66                 log.debug("Post demands payload: " + homingRequest.toJsonString());
67
68                 HttpHeaders header = new HttpHeaders();
69                 header.setContentType(MediaType.APPLICATION_JSON);
70                 header.set("Authorization", managerProperties.getHeaders().get("auth"));
71                 header.set("X-patchVersion", managerProperties.getHeaders().get("patchVersion"));
72                 header.set("X-minorVersion", managerProperties.getHeaders().get("minorVersion"));
73                 header.set("X-latestVersion", managerProperties.getHeaders().get("latestVersion"));
74                 BaseClient<String, LinkedHashMap<?, ?>> baseClient = new BaseClient<>();
75
76                 baseClient.setTargetUrl(url);
77                 baseClient.setHttpHeader(header);
78
79                 LinkedHashMap<?, ?> response = baseClient.post(homingRequest.toJsonString());
80                 validator.validateDemandsResponse(response);
81                 log.trace("Completed Sniro Client Post Demands");
82         }
83
84         /**
85          * Makes a rest call to sniro conductor to notify them of successful or unsuccessful vnf
86          * creation for previously homed resources
87          *
88          * TODO Temporarily being used in groovy therefore can not utilize autowire. Once java "release"
89          * subflow is developed it will be refactored to use autowire.
90          *
91          * @param releaseRequest
92          * @return
93          * @throws BadResponseException
94          */
95         public void postRelease(SniroConductorRequest releaseRequest) throws BadResponseException {
96                 log.trace("Started Sniro Client Post Release");
97                 String url = UrnPropertiesReader.getVariable("sniro.conductor.host") + UrnPropertiesReader.getVariable("sniro.conductor.uri");
98                 log.debug("Post release url: " + url);
99                 log.debug("Post release payload: " + releaseRequest.toJsonString());
100
101                 HttpHeaders header = new HttpHeaders();
102                 header.setContentType(MediaType.APPLICATION_JSON);
103                 header.set("Authorization", UrnPropertiesReader.getVariable("sniro.conductor.headers.auth"));
104                 BaseClient<String, LinkedHashMap<?, ?>> baseClient = new BaseClient<>();
105
106                 baseClient.setTargetUrl(url);
107                 baseClient.setHttpHeader(header);
108
109                 LinkedHashMap<?, ?> response = baseClient.post(releaseRequest.toJsonString());
110                 SniroValidator v = new SniroValidator();
111                 v.validateReleaseResponse(response);
112                 log.trace("Completed Sniro Client Post Release");
113         }
114
115 }