Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / policy / PolicyClientImpl.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.onap.so.client.policy;
22
23 import java.io.IOException;
24 import java.util.List;
25 import org.onap.so.client.RestClient;
26 import org.onap.so.client.RestPropertiesLoader;
27 import org.onap.so.client.defaultproperties.PolicyRestPropertiesImpl;
28 import org.onap.so.client.policy.entities.AllowedTreatments;
29 import org.onap.so.client.policy.entities.Bbid;
30 import org.onap.so.client.policy.entities.Config;
31 import org.onap.so.client.policy.entities.ConfigRequestParameters;
32 import org.onap.so.client.policy.entities.DecisionAttributes;
33 import org.onap.so.client.policy.entities.DictionaryData;
34 import org.onap.so.client.policy.entities.DictionaryItemsRequest;
35 import org.onap.so.client.policy.entities.DictionaryJson;
36 import org.onap.so.client.policy.entities.PolicyConfig;
37 import org.onap.so.client.policy.entities.PolicyDecision;
38 import org.onap.so.client.policy.entities.PolicyDecisionRequest;
39 import org.onap.so.client.policy.entities.PolicyServiceType;
40 import org.onap.so.client.policy.entities.Workstep;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import com.fasterxml.jackson.core.JsonParseException;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonMappingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.SerializationFeature;
48
49
50 public class PolicyClientImpl implements PolicyClient {
51
52     private static Logger logger = LoggerFactory.getLogger(PolicyClientImpl.class);
53     private PolicyRestProperties props;
54     private ObjectMapper mapper = new ObjectMapper();
55
56     public PolicyClientImpl() {
57         props = RestPropertiesLoader.getInstance().getNewImpl(PolicyRestProperties.class);
58         if (props == null) {
59             logger.error("No RestProperty.PolicyRestProperties implementation found on classpath");
60             props = new PolicyRestPropertiesImpl();
61         }
62     }
63
64     public PolicyDecision getDecision(String serviceType, String vnfType, String bbID, String workStep,
65             String errorCode) {
66         DecisionAttributes decisionAttributes = new DecisionAttributes();
67         decisionAttributes.setServiceType(serviceType);
68         decisionAttributes.setvNFType(vnfType);
69         decisionAttributes.setBbID(bbID);
70         decisionAttributes.setWorkStep(workStep);
71         decisionAttributes.setErrorCode(errorCode);
72
73         return this.getDecision(decisionAttributes);
74     }
75
76     protected PolicyDecision getDecision(DecisionAttributes decisionAttributes) {
77         PolicyRestClient client = this.getPolicyRestClient(PolicyServiceType.GET_DECISION);
78         PolicyDecisionRequest decisionRequest = new PolicyDecisionRequest();
79         decisionRequest.setDecisionAttributes(decisionAttributes);
80         decisionRequest.setEcompcomponentName(RestClient.ECOMP_COMPONENT_NAME);
81
82         return client.post(decisionRequest, PolicyDecision.class);
83     }
84
85     public DictionaryData getAllowedTreatments(String bbID, String workStep) {
86         PolicyRestClient client = this.getPolicyRestClient(PolicyServiceType.GET_DICTIONARY_ITEMS);
87         DictionaryItemsRequest dictionaryItemsRequest = new DictionaryItemsRequest();
88         dictionaryItemsRequest.setDictionaryType("Decision");
89         dictionaryItemsRequest.setDictionary("RainyDayTreatments");
90         final AllowedTreatments response = client.post(dictionaryItemsRequest, AllowedTreatments.class);
91         final DictionaryJson dictionaryJson = response.getDictionaryJson();
92         final List<DictionaryData> dictionaryDataList = dictionaryJson.getDictionaryDatas();
93         for (DictionaryData dictData : dictionaryDataList) {
94             Bbid bBid = dictData.getBbid();
95             Workstep workstep = dictData.getWorkstep();
96             String bBidString = bBid.getString();
97             String workstepString = workstep.getString();
98             if (bbID.equals(bBidString) && workStep.equals(workstepString)) {
99                 return dictData;
100             }
101         }
102         logger.error("There is no AllowedTreatments with that specified parameter set");
103         return null;
104     }
105
106     @Override
107     public Config getConfigWithPolicyName(String policyName) {
108         PolicyRestClient client = this.getPolicyRestClient(PolicyServiceType.GET_CONFIG);
109         ConfigRequestParameters configReqParameters = new ConfigRequestParameters();
110         configReqParameters.setPolicyName(policyName);
111         PolicyConfig[] policyConfigList = client.post(configReqParameters, PolicyConfig[].class);
112         PolicyConfig policyConfig = null;
113         if (policyConfigList.length > 1) {
114             logger.debug("Too many configs for policyName: " + policyName);
115             return null;
116         }
117         try {
118             policyConfig = policyConfigList[0];
119             return this.getConfigFromStringJson(policyConfig.getConfig());
120         } catch (IOException e) {
121             logger.error(e.getMessage());
122             return null;
123         }
124     }
125
126     protected Config getConfigFromStringJson(String configJson)
127             throws JsonParseException, JsonMappingException, IOException {
128         String unescapedJson = configJson.replaceAll("\\\\", "");
129         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
130         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
131         return mapper.readValue(unescapedJson, Config.class);
132     }
133
134     protected PolicyRestClient getPolicyRestClient(PolicyServiceType policyServiceType) {
135         return new PolicyRestClient(this.props, policyServiceType);
136     }
137 }