2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.guard;
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.google.common.collect.Lists;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.models.decisions.concepts.DecisionResponse;
38 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
39 import org.onap.policy.pdp.xacml.application.common.std.StdMetadataTranslator;
40 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * This class implements the onap.policies.controlloop.Guard policy implementations.
47 * @author pameladragosh
50 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
53 private static final String STRING_VERSION100 = "1.0.0";
54 private Map<String, String> supportedPolicyTypes = new HashMap<>();
55 private StdMetadataTranslator translator = new StdMetadataTranslator();
60 public GuardPdpApplication() {
61 this.supportedPolicyTypes.put("onap.policies.controlloop.guard.FrequencyLimiter", STRING_VERSION100);
62 this.supportedPolicyTypes.put("onap.policies.controlloop.guard.MinMax", STRING_VERSION100);
66 public String applicationName() {
67 return "Guard Application";
71 public List<String> actionDecisionsSupported() {
72 return Arrays.asList("guard");
76 public List<String> supportedPolicyTypes() {
77 return Lists.newArrayList(supportedPolicyTypes.keySet());
81 public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
83 // For the time being, restrict this if the version isn't known.
84 // Could be too difficult to support changing of versions dynamically.
86 if (! this.supportedPolicyTypes.containsKey(policyType)) {
90 // Must match version exactly
92 return this.supportedPolicyTypes.get(policyType).equals(policyTypeVersion);
96 public void loadPolicies(Map<String, Object> toscaPolicies) {
99 // Convert the policies first
101 List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
102 if (listPolicies.isEmpty()) {
103 throw new ToscaPolicyConversionException("Converted 0 policies");
106 // TODO update properties, save to disk, etc.
108 } catch (ToscaPolicyConversionException e) {
109 LOGGER.error("Failed to loadPolicies {}", e);
114 public DecisionResponse makeDecision(DecisionRequest request) {
116 // Convert to a XacmlRequest
118 Request xacmlRequest = translator.convertRequest(request);
120 // Now get a decision
122 Response xacmlResponse = this.xacmlDecision(xacmlRequest);
124 // Convert to a DecisionResponse
126 return translator.convertResponse(xacmlResponse);