1fd01e65edebf0d2781d31bd58d1cbd04e257cae
[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.util.List;
24
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.DecisionAttributes;
31 import org.onap.so.client.policy.entities.DictionaryData;
32 import org.onap.so.client.policy.entities.DictionaryItemsRequest;
33 import org.onap.so.client.policy.entities.DictionaryJson;
34 import org.onap.so.client.policy.entities.PolicyDecision;
35 import org.onap.so.client.policy.entities.PolicyDecisionRequest;
36 import org.onap.so.client.policy.entities.PolicyServiceType;
37 import org.onap.so.client.policy.entities.Workstep;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 public class PolicyClientImpl implements PolicyClient {
43
44         private static Logger logger = LoggerFactory.getLogger(PolicyClientImpl.class);
45         private PolicyRestProperties props;
46         public PolicyClientImpl() {
47                 props = RestPropertiesLoader.getInstance().getNewImpl(PolicyRestProperties.class);
48                 if (props == null) {
49                         logger.error("No RestProperty.PolicyRestProperties implementation found on classpath");
50                         props = new PolicyRestPropertiesImpl();
51                 }
52         }
53         public PolicyDecision getDecision(String serviceType, String vnfType, String bbID, String workStep,
54                         String errorCode) {
55                 DecisionAttributes decisionAttributes = new DecisionAttributes();
56                 decisionAttributes.setServiceType(serviceType);
57                 decisionAttributes.setvNFType(vnfType);
58                 decisionAttributes.setBbID(bbID);
59                 decisionAttributes.setWorkStep(workStep);
60                 decisionAttributes.setErrorCode(errorCode);
61
62                 return this.getDecision(decisionAttributes);
63         }
64
65         protected PolicyDecision getDecision(DecisionAttributes decisionAttributes) {
66                 PolicyRestClient client = new PolicyRestClient(this.props, PolicyServiceType.GET_DECISION);
67                 PolicyDecisionRequest decisionRequest = new PolicyDecisionRequest();
68                 decisionRequest.setDecisionAttributes(decisionAttributes);
69                 decisionRequest.setEcompcomponentName(RestClient.ECOMP_COMPONENT_NAME);
70                 
71                 return client.post(decisionRequest, PolicyDecision.class);
72         }
73         
74         public DictionaryData getAllowedTreatments(String bbID, String workStep)
75         {
76                 PolicyRestClient client = new PolicyRestClient(this.props, PolicyServiceType.GET_DICTIONARY_ITEMS);
77                 DictionaryItemsRequest dictionaryItemsRequest = new DictionaryItemsRequest();
78                 dictionaryItemsRequest.setDictionaryType("Decision");
79                 dictionaryItemsRequest.setDictionary("RainyDayTreatments");
80                 final AllowedTreatments response = client.post(dictionaryItemsRequest, AllowedTreatments.class);
81                 final DictionaryJson dictionaryJson = response.getDictionaryJson();
82                 final List<DictionaryData> dictionaryDataList = dictionaryJson.getDictionaryDatas();
83                 for(DictionaryData dictData : dictionaryDataList){
84                         Bbid bBid = dictData.getBbid();
85                         Workstep workstep = dictData.getWorkstep();
86                         String bBidString = bBid.getString();
87                         String workstepString = workstep.getString();
88                         if(bbID.equals(bBidString) && workStep.equals(workstepString)){
89                                 return dictData;
90                         }
91                 }
92                 logger.error("There is no AllowedTreatments with that specified parameter set");
93                 return null;
94         }
95
96 }