Integrate using Policy Type to find Matchable
[policy/xacml-pdp.git] / applications / optimization / src / main / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationPdpApplication.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.optimization;
24
25 import java.nio.file.Path;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
32 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
33 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
34 import org.onap.policy.pdp.xacml.application.common.std.StdMatchableTranslator;
35 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class OptimizationPdpApplication extends StdXacmlApplicationServiceProvider {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplication.class);
42     private static final String STRING_VERSION100 = "1.0.0";
43
44     private StdMatchableTranslator translator = new StdMatchableTranslator();
45     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
46
47     /**
48      * Constructor.
49      */
50     public OptimizationPdpApplication() {
51         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
52                 "onap.policies.optimization.AffinityPolicy", STRING_VERSION100));
53         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
54                 "onap.policies.optimization.DistancePolicy", STRING_VERSION100));
55         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
56                 "onap.policies.optimization.HpaPolicy", STRING_VERSION100));
57         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
58                 "onap.policies.optimization.OptimizationPolicy", STRING_VERSION100));
59         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
60                 "onap.policies.optimization.PciPolicy", STRING_VERSION100));
61         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
62                 "onap.policies.optimization.QueryPolicy", STRING_VERSION100));
63         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
64                 "onap.policies.optimization.SubscriberPolicy", STRING_VERSION100));
65         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
66                 "onap.policies.optimization.Vim_fit", STRING_VERSION100));
67         this.supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(
68                 "onap.policies.optimization.VnfPolicy", STRING_VERSION100));
69     }
70
71     @Override
72     public String applicationName() {
73         return "optimization";
74     }
75
76     @Override
77     public List<String> actionDecisionsSupported() {
78         return Arrays.asList("optimize");
79     }
80
81     @Override
82     public void initialize(Path pathForData, RestServerParameters policyApiParameters)
83             throws XacmlApplicationException {
84         //
85         // Store our API parameters and path for translator so it
86         // can go get Policy Types
87         //
88         this.translator.setPathForData(pathForData);
89         this.translator.setApiRestParameters(policyApiParameters);
90         //
91         // Let our super class do its thing
92         //
93         super.initialize(pathForData, policyApiParameters);
94     }
95
96     @Override
97     public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
98         return Collections.unmodifiableList(supportedPolicyTypes);
99     }
100
101     @Override
102     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
103         //
104         // For the time being, restrict this if the version isn't known.
105         // Could be too difficult to support changing of versions dynamically.
106         //
107         //
108         // For the time being, restrict this if the version isn't known.
109         // Could be too difficult to support changing of versions dynamically.
110         //
111         for (ToscaPolicyTypeIdentifier supported : this.supportedPolicyTypes) {
112             if (policyTypeId.equals(supported)) {
113                 LOGGER.info("optimization can support {}", supported);
114                 return true;
115             }
116         }
117         return false;
118     }
119
120     @Override
121     protected ToscaPolicyTranslator getTranslator(String type) {
122         //
123         // Return translator
124         //
125         return translator;
126     }
127
128 }