Change the header to SO
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / policy / PolicyRestClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.client.policy;
22
23 import java.util.Map;
24 import java.util.UUID;
25
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28
29 import org.springframework.stereotype.Service;
30
31 @Service
32 public class PolicyRestClient extends RestClient {
33
34         private static final String ENDPOINT_KEY = "policy.endpoint";
35         private static final String X_ECOMP_REQUESTID = String.valueOf(UUID.randomUUID());
36
37         public PolicyRestClient() {
38                 super(ENDPOINT_KEY);
39         }
40
41         @Override
42         protected void initializeHeaderMap(Map<String, String> headerMap) {
43                 headerMap.put("ClientAuth", properties.get("policy.client.auth"));
44                 headerMap.put("Authorization", properties.get("policy.auth"));
45                 headerMap.put("Environment", properties.get("policy.environment"));
46                 headerMap.put("X-ECOMP-RequestID", X_ECOMP_REQUESTID);
47         }
48
49         public PolicyDecision getDecision(String serviceType, String vnfType, String bbID, String workStep,
50                         String errorCode) {
51                 DecisionAttributes decisionAttributes = new DecisionAttributes();
52                 decisionAttributes.setServiceType(serviceType);
53                 decisionAttributes.setVNFType(vnfType);
54                 decisionAttributes.setBBID(bbID);
55                 decisionAttributes.setWorkStep(workStep);
56                 decisionAttributes.setErrorCode(errorCode);
57
58                 return this.getDecision(decisionAttributes);
59         }
60
61         private PolicyDecision getDecision(DecisionAttributes decisionAttributes) {
62                 PolicyDecisionRequest decisionRequest = new PolicyDecisionRequest();
63                 decisionRequest.setDecisionAttributes(decisionAttributes);
64                 decisionRequest.setEcompcomponentName(ECOMP_COMPONENT_NAME);
65
66                 return this.getBuilder().accept(MediaType.APPLICATION_JSON_TYPE)
67                                 .post(Entity.entity(decisionRequest, MediaType.APPLICATION_JSON)).readEntity(PolicyDecision.class);
68         }
69 }