Remove duplicated code
[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-2020 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.Arrays;
26 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
27 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
28 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This class implements the onap.policies.controlloop.Guard policy implementations.
34  *
35  * @author pameladragosh
36  *
37  */
38 public class GuardPdpApplication extends StdXacmlApplicationServiceProvider {
39     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplication.class);
40     private static final String STRING_VERSION100 = "1.0.0";
41
42     private GuardTranslator guardTranslator = new GuardTranslator();
43     private CoordinationGuardTranslator coordinationTranslator = new CoordinationGuardTranslator();
44
45     /**
46      * Constructor.
47      *
48      */
49     public GuardPdpApplication() {
50         super();
51
52         applicationName = "guard";
53         actions = Arrays.asList("guard");
54
55         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
56                 GuardTranslator.POLICYTYPE_FREQUENCY,
57                 STRING_VERSION100));
58         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
59                 GuardTranslator.POLICYTYPE_MINMAX,
60                 STRING_VERSION100));
61         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
62                 GuardTranslator.POLICYTYPE_BLACKLIST,
63                 STRING_VERSION100));
64         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
65                 GuardTranslator.POLICYTYPE_FILTER,
66                 STRING_VERSION100));
67         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
68                 "onap.policies.controlloop.guard.coordination.FirstBlocksSecond",
69                 STRING_VERSION100));
70     }
71
72     @Override
73     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
74         //
75         // For the time being, restrict this if the version isn't known.
76         // Could be too difficult to support changing of versions dynamically.
77         //
78         for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
79             if (policyTypeId.equals(supported)) {
80                 return true;
81             }
82         }
83         return false;
84     }
85
86     @Override
87     protected ToscaPolicyTranslator getTranslator(String type) {
88         LOGGER.debug("Policy type {}", type);
89         if (type.contains("coordination")) {
90             LOGGER.debug("returning coordinationTranslator");
91             return coordinationTranslator;
92         } else {
93             LOGGER.debug("returning guardTranslator");
94             return guardTranslator;
95         }
96     }
97
98 }