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.pdp.xacml.application.common.std;
 
  25 import com.att.research.xacml.api.Request;
 
  26 import com.att.research.xacml.api.Response;
 
  27 import com.att.research.xacml.util.XACMLPolicyWriter;
 
  29 import java.io.ByteArrayOutputStream;
 
  30 import java.io.IOException;
 
  31 import java.util.ArrayList;
 
  32 import java.util.List;
 
  34 import java.util.Map.Entry;
 
  36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 
  38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 
  39 import org.onap.policy.models.decisions.concepts.DecisionResponse;
 
  40 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
 
  41 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
 
  42 import org.slf4j.Logger;
 
  43 import org.slf4j.LoggerFactory;
 
  45 public class StdMetadataTranslator implements ToscaPolicyTranslator {
 
  47     private static final Logger LOGGER = LoggerFactory.getLogger(StdMetadataTranslator.class);
 
  49     public StdMetadataTranslator() {
 
  53     @SuppressWarnings("unchecked")
 
  55     public List<PolicyType> scanAndConvertPolicies(Map<String, Object> toscaObject)
 
  56             throws ToscaPolicyConversionException {
 
  60         List<PolicyType> scannedPolicies = new ArrayList<>();
 
  62         // Iterate each of the Policies
 
  64         List<Object> policies = (List<Object>) toscaObject.get("policies");
 
  65         for (Object policyObject : policies) {
 
  69             LOGGER.debug("Found policy {}", policyObject.getClass());
 
  70             Map<String, Object> policyContents = (Map<String, Object>) policyObject;
 
  71             for (Entry<String, Object> entrySet : policyContents.entrySet()) {
 
  72                 LOGGER.debug("Entry set {}", entrySet);
 
  74                 // Convert this policy
 
  76                 PolicyType policy = this.convertPolicy(entrySet);
 
  77                 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
 
  78                     XACMLPolicyWriter.writePolicyFile(os, policy);
 
  79                     LOGGER.debug("{}", os);
 
  80                 } catch (IOException e) {
 
  81                     LOGGER.error("Failed to convert {}", e);
 
  84                 // Convert and add in the new policy
 
  86                 scannedPolicies.add(policy);
 
  90         return scannedPolicies;
 
  94     public Request convertRequest(DecisionRequest request) {
 
  95         // TODO Auto-generated method stub
 
 100     public DecisionResponse convertResponse(Response xacmlResponse) {
 
 101         // TODO Auto-generated method stub
 
 105     private PolicyType convertPolicy(Entry<String, Object> entrySet) throws ToscaPolicyConversionException {