AT&T 1712 and 1802 release code
[so.git] / common / src / main / java / org / openecomp / mso / 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.openecomp.mso.client.policy;
22
23 import org.openecomp.mso.client.RestPropertiesLoader;
24 import org.openecomp.mso.client.defaultproperties.PolicyRestPropertiesImpl;
25 import org.openecomp.mso.client.policy.entities.AllowedTreatments;
26 import org.openecomp.mso.client.policy.entities.Bbid;
27 import org.openecomp.mso.client.policy.entities.DecisionAttributes;
28 import org.openecomp.mso.client.policy.entities.DictionaryData;
29 import org.openecomp.mso.client.policy.entities.DictionaryItemsRequest;
30 import org.openecomp.mso.client.policy.entities.DictionaryJson;
31 import org.openecomp.mso.client.policy.entities.PolicyDecision;
32 import org.openecomp.mso.client.policy.entities.PolicyDecisionRequest;
33 import org.openecomp.mso.client.policy.entities.PolicyServiceType;
34 import org.openecomp.mso.client.policy.entities.Workstep;
35
36 import java.util.List;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40 public class PolicyClientImpl implements PolicyClient {
41
42         protected final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
43         private PolicyRestProperties props;
44         public PolicyClientImpl() {
45                 props = RestPropertiesLoader.getInstance().getNewImpl(PolicyRestProperties.class);
46                 if (props == null) {
47                         metricsLogger.error("No RestProperty.PolicyRestProperties implementation found on classpath");
48                         props = new PolicyRestPropertiesImpl();
49                 }
50         }
51         public PolicyDecision getDecision(String serviceType, String vnfType, String bbID, String workStep,
52                         String errorCode) {
53                 DecisionAttributes decisionAttributes = new DecisionAttributes();
54                 decisionAttributes.setServiceType(serviceType);
55                 decisionAttributes.setVNFType(vnfType);
56                 decisionAttributes.setBBID(bbID);
57                 decisionAttributes.setWorkStep(workStep);
58                 decisionAttributes.setErrorCode(errorCode);
59
60                 return this.getDecision(decisionAttributes);
61         }
62
63         private PolicyDecision getDecision(DecisionAttributes decisionAttributes) {
64                 PolicyRestClient client = new PolicyRestClient(this.props, PolicyServiceType.GET_DECISION);
65                 PolicyDecisionRequest decisionRequest = new PolicyDecisionRequest();
66                 decisionRequest.setDecisionAttributes(decisionAttributes);
67                 decisionRequest.setEcompcomponentName(RestClient.ECOMP_COMPONENT_NAME);
68                 
69                 return client.post(decisionRequest, PolicyDecision.class);
70         }
71         
72         public DictionaryData getAllowedTreatments(String bbID, String workStep)
73         {
74                 PolicyRestClient client = new PolicyRestClient(this.props, PolicyServiceType.GET_DICTIONARY_ITEMS);
75                 DictionaryItemsRequest dictionaryItemsRequest = new DictionaryItemsRequest();
76                 dictionaryItemsRequest.setDictionaryType("Decision");
77                 dictionaryItemsRequest.setDictionary("RainyDayTreatments");
78                 final AllowedTreatments response = client.post(dictionaryItemsRequest, AllowedTreatments.class);
79                 final DictionaryJson dictionaryJson = response.getDictionaryJson();
80                 final List<DictionaryData> dictionaryDataList = dictionaryJson.getDictionaryDatas();
81                 for(DictionaryData dictData : dictionaryDataList){
82                         Bbid bBid = dictData.getBbid();
83                         Workstep workstep = dictData.getWorkstep();
84                         String bBidString = bBid.getString();
85                         String workstepString = workstep.getString();
86                         if(bbID.equals(bBidString) && workStep.equals(workstepString)){
87                                 return dictData;
88                         }
89                 }
90                 metricsLogger.error("There is no AllowedTreatments with that specified parameter set");
91                 return null;
92         }
93
94 }