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.att.research.xacml.util.XACMLPolicyWriter;
 
  28 import com.google.common.collect.Lists;
 
  30 import java.io.IOException;
 
  31 import java.nio.file.Path;
 
  32 import java.util.Arrays;
 
  33 import java.util.HashMap;
 
  34 import java.util.List;
 
  36 import java.util.Properties;
 
  38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 
  40 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 
  41 import org.onap.policy.models.decisions.concepts.DecisionResponse;
 
  42 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
 
  43 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
 
  44 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
 
  45 import org.slf4j.Logger;
 
  46 import org.slf4j.LoggerFactory;
 
  49  * This class implements the onap.policies.controlloop.Guard policy implementations.
 
  51  * @author pameladragosh
 
  54 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
 
  56     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
 
  57     private static final String STRING_VERSION100 = "1.0.0";
 
  58     private Map<String, String> supportedPolicyTypes = new HashMap<>();
 
  59     private LegacyGuardTranslator translator = new LegacyGuardTranslator();
 
  64     public GuardPdpApplication() {
 
  65         this.supportedPolicyTypes.put("onap.policies.controlloop.guard.FrequencyLimiter", STRING_VERSION100);
 
  66         this.supportedPolicyTypes.put("onap.policies.controlloop.guard.MinMax", STRING_VERSION100);
 
  70     public String applicationName() {
 
  71         return "Guard Application";
 
  75     public List<String> actionDecisionsSupported() {
 
  76         return Arrays.asList("guard");
 
  80     public List<String> supportedPolicyTypes() {
 
  81         return Lists.newArrayList(supportedPolicyTypes.keySet());
 
  85     public boolean canSupportPolicyType(String policyType, String policyTypeVersion) {
 
  87         // For the time being, restrict this if the version isn't known.
 
  88         // Could be too difficult to support changing of versions dynamically.
 
  90         if (! this.supportedPolicyTypes.containsKey(policyType)) {
 
  94         // Must match version exactly
 
  96         return this.supportedPolicyTypes.get(policyType).equals(policyTypeVersion);
 
 100     public void loadPolicies(Map<String, Object> toscaPolicies) {
 
 103             // Convert the policies first
 
 105             List<PolicyType> listPolicies = translator.scanAndConvertPolicies(toscaPolicies);
 
 106             if (listPolicies.isEmpty()) {
 
 107                 throw new ToscaPolicyConversionException("Converted 0 policies");
 
 110             // Create a copy of the properties object
 
 112             Properties newProperties = this.getProperties();
 
 114             // Iterate through the policies
 
 116             for (PolicyType newPolicy : listPolicies) {
 
 118                 // Construct the filename
 
 120                 Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(newPolicy, this.getDataPath());
 
 122                 // Write the policy to disk
 
 123                 // Maybe check for an error
 
 125                 XACMLPolicyWriter.writePolicyFile(refPath, newPolicy);
 
 127                 // Add root policy to properties object
 
 129                 XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
 
 132             // Write the properties to disk
 
 134             XacmlPolicyUtils.storeXacmlProperties(newProperties,
 
 135                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
 
 139             this.createEngine(newProperties);
 
 140         } catch (IOException | ToscaPolicyConversionException e) {
 
 141             LOGGER.error("Failed to loadPolicies {}", e);
 
 146     public DecisionResponse makeDecision(DecisionRequest request) {
 
 148         // Convert to a XacmlRequest
 
 150         Request xacmlRequest = translator.convertRequest(request);
 
 152         // Now get a decision
 
 154         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
 
 156         // Convert to a DecisionResponse
 
 158         return translator.convertResponse(xacmlResponse);