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;
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;
50 public class PolicyClientImpl implements PolicyClient {
52 private static Logger logger = LoggerFactory.getLogger(PolicyClientImpl.class);
53 private PolicyRestProperties props;
54 private ObjectMapper mapper = new ObjectMapper();
56 public PolicyClientImpl() {
57 props = RestPropertiesLoader.getInstance().getNewImpl(PolicyRestProperties.class);
59 logger.error("No RestProperty.PolicyRestProperties implementation found on classpath");
60 props = new PolicyRestPropertiesImpl();
64 public PolicyDecision getDecision(String serviceType, String vnfType, String bbID, String workStep,
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);
73 return this.getDecision(decisionAttributes);
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);
82 return client.post(decisionRequest, PolicyDecision.class);
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)) {
102 logger.error("There is no AllowedTreatments with that specified parameter set");
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);
118 policyConfig = policyConfigList[0];
119 return this.getConfigFromStringJson(policyConfig.getConfig());
120 } catch (IOException e) {
121 logger.error(e.getMessage());
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);
134 protected PolicyRestClient getPolicyRestClient(PolicyServiceType policyServiceType) {
135 return new PolicyRestClient(this.props, policyServiceType);