added generic fabric support to 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.common.baseclient.BaseClient;
27 import org.onap.so.bpmn.core.UrnPropertiesReader;
28 import org.onap.so.client.exception.BadResponseException;
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.core.ParameterizedTypeReference;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Component;
38
39 import com.fasterxml.jackson.core.JsonProcessingException;
40
41
42 @Component
43 public class SniroClient {
44
45         private static final MsoLogger log = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroClient.class);
46
47         @Autowired
48         private ManagerProperties managerProperties;
49
50         @Autowired
51         private SniroValidator validator;
52
53
54         /**
55          * Makes a rest call to sniro manager to perform homing and licensing for a
56          * 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                 log.trace("Started Sniro Client Post Demands");
65                 String url = managerProperties.getHost() + managerProperties.getUri().get("v2");
66                 log.debug("Post demands url: " + url);
67                 log.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<?, ?>> baseClient = new BaseClient<>();
76
77                 baseClient.setTargetUrl(url);
78                 baseClient.setHttpHeader(header);
79
80                 LinkedHashMap<?, ?> response = baseClient.post(homingRequest.toJsonString(), new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
81                 validator.validateDemandsResponse(response);
82                 log.trace("Completed Sniro Client Post Demands");
83         }
84
85         /**
86          * Makes a rest call to sniro conductor to notify them of successful or unsuccessful vnf
87          * creation for previously homed resources
88          *
89          * TODO Temporarily being used in groovy therefore can not utilize autowire. Once java "release"
90          * subflow is developed it will be refactored to use autowire.
91          *
92          * @param releaseRequest
93          * @return
94          * @throws BadResponseException
95          */
96         public void postRelease(SniroConductorRequest releaseRequest) throws BadResponseException {
97                 log.trace("Started Sniro Client Post Release");
98                 String url = UrnPropertiesReader.getVariable("sniro.conductor.host") + UrnPropertiesReader.getVariable("sniro.conductor.uri");
99                 log.debug("Post release url: " + url);
100                 log.debug("Post release payload: " + releaseRequest.toJsonString());
101
102                 HttpHeaders header = new HttpHeaders();
103                 header.setContentType(MediaType.APPLICATION_JSON);
104                 header.set("Authorization", UrnPropertiesReader.getVariable("sniro.conductor.headers.auth"));
105                 BaseClient<String, LinkedHashMap<?, ?>> baseClient = new BaseClient<>();
106
107                 baseClient.setTargetUrl(url);
108                 baseClient.setHttpHeader(header);
109
110                 LinkedHashMap<?, ?> response = baseClient.post(releaseRequest.toJsonString(), new ParameterizedTypeReference<LinkedHashMap<? ,?>>() {});
111                 SniroValidator v = new SniroValidator();
112                 v.validateReleaseResponse(response);
113                 log.trace("Completed Sniro Client Post Release");
114         }
115
116 }