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