Set all cross references of policy/drools-pdp
[policy/drools-pdp.git] / feature-lifecycle / src / main / java / org / onap / policy / drools / lifecycle / PolicyTypeNativeArtifactController.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.lifecycle;
23
24 import lombok.AllArgsConstructor;
25 import lombok.Getter;
26 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.drools.controller.DroolsControllerConstants;
29 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
30 import org.onap.policy.drools.protocol.configuration.ControllerConfiguration;
31 import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
32 import org.onap.policy.drools.system.PolicyController;
33 import org.onap.policy.drools.system.PolicyControllerConstants;
34 import org.onap.policy.drools.system.PolicyEngineConstants;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @AllArgsConstructor
41 public class PolicyTypeNativeArtifactController implements PolicyTypeController {
42     private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeArtifactController.class);
43
44     @Getter
45     protected final ToscaConceptIdentifier policyType;
46
47     @GsonJsonIgnore
48     protected final LifecycleFsm fsm;
49
50     @Override
51     public boolean deploy(ToscaPolicy policy) {
52         NativeArtifactPolicy nativePolicy;
53         PolicyController controller;
54         try {
55             nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
56             var droolsConfig =
57                     new DroolsConfiguration(
58                             nativePolicy.getProperties().getRulesArtifact().getArtifactId(),
59                             nativePolicy.getProperties().getRulesArtifact().getGroupId(),
60                             nativePolicy.getProperties().getRulesArtifact().getVersion());
61
62             controller =
63                     PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName());
64             if (controller.getDrools().isBrained()) {
65                 logger.warn("upgrade of a live controller is strongly discouraged (undeploy first): {} -> {}",
66                         controller, droolsConfig);
67             }
68
69             return update(nativePolicy, droolsConfig);
70         } catch (CoderException | RuntimeException e) {
71             logger.warn("Invalid Policy: {}", policy);
72             return false;
73         }
74     }
75
76     @Override
77     public boolean undeploy(ToscaPolicy policy) {
78         try {
79             NativeArtifactPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
80             var noConfig =
81                     new DroolsConfiguration(
82                             DroolsControllerConstants.NO_ARTIFACT_ID,
83                             DroolsControllerConstants.NO_GROUP_ID,
84                             DroolsControllerConstants.NO_VERSION);
85
86             return update(nativePolicy, noConfig);
87         } catch (RuntimeException | CoderException e) {
88             logger.warn("Invalid Policy: {}", policy);
89             return false;
90         }
91     }
92
93     private boolean update(NativeArtifactPolicy nativePolicy, DroolsConfiguration droolsConfig) {
94         var controllerConfig =
95                 new ControllerConfiguration(nativePolicy.getProperties().getController().getName(),
96                         ControllerConfiguration.CONFIG_CONTROLLER_OPERATION_UPDATE, droolsConfig);
97         return PolicyEngineConstants.getManager().updatePolicyController(controllerConfig) != null;
98     }
99 }