Add statistics and sonar cleanup and blacklist
[policy/xacml-pdp.git] / applications / guard / src / main / java / org / onap / policy / xacml / pdp / application / guard / GuardPdpApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.guard;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
30 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
31 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class implements the onap.policies.controlloop.Guard policy implementations.
37  *
38  * @author pameladragosh
39  *
40  */
41 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
42     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
43     private static final String STRING_VERSION100 = "1.0.0";
44     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
45     private LegacyGuardTranslator legacyTranslator = new LegacyGuardTranslator();
46     private CoordinationGuardTranslator coordinationTranslator = new CoordinationGuardTranslator();
47
48
49     /**
50      * Constructor.
51      *
52      */
53     public GuardPdpApplication() {
54         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
55                 "onap.policies.controlloop.guard.FrequencyLimiter",
56                 STRING_VERSION100));
57         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
58                 "onap.policies.controlloop.guard.MinMax",
59                 STRING_VERSION100));
60         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
61                 "onap.policies.controlloop.guard.Blacklist",
62                 STRING_VERSION100));
63         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
64                 "onap.policies.controlloop.guard.coordination.FirstBlocksSecond",
65                 STRING_VERSION100));
66     }
67
68     @Override
69     public String applicationName() {
70         return "guard";
71     }
72
73     @Override
74     public List<String> actionDecisionsSupported() {
75         return Arrays.asList("guard");
76     }
77
78     @Override
79     public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
80         return supportedPolicyTypes;
81     }
82
83     @Override
84     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
85         //
86         // For the time being, restrict this if the version isn't known.
87         // Could be too difficult to support changing of versions dynamically.
88         //
89         for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
90             if (policyTypeId.equals(supported)) {
91                 return true;
92             }
93         }
94         return false;
95     }
96
97     @Override
98     protected ToscaPolicyTranslator getTranslator(String type) {
99         LOGGER.debug("Policy type {}", type);
100         if ( type.contains("coordination") ) {
101             LOGGER.debug("returning coordinationTranslator");
102             return coordinationTranslator;
103         } else {
104             LOGGER.debug("returning legacyTranslator");
105             return legacyTranslator;
106         }
107     }
108
109 }