d91ecccff096e71a5f4ed1756bd4256a4bd97ac7
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.lifecycle;
22
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import lombok.Getter;
25 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
26 import org.onap.policy.common.utils.coder.CoderException;
27 import org.onap.policy.drools.controller.DroolsControllerConstants;
28 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
29 import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
30 import org.onap.policy.drools.system.PolicyController;
31 import org.onap.policy.drools.system.PolicyControllerConstants;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class PolicyTypeNativeArtifactController implements PolicyTypeController {
38     private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeArtifactController.class);
39
40     @Getter
41     protected final ToscaPolicyTypeIdentifier policyType;
42
43     @GsonJsonIgnore
44     @JsonIgnore
45     protected final transient LifecycleFsm fsm;
46
47     public PolicyTypeNativeArtifactController(LifecycleFsm fsm, ToscaPolicyTypeIdentifier policyType) {
48         this.policyType = policyType;
49         this.fsm = fsm;
50     }
51
52     @Override
53     public boolean deploy(ToscaPolicy policy) {
54         NativeArtifactPolicy nativePolicy;
55         PolicyController controller;
56         try {
57             nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
58             controller =
59                     PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName());
60         } catch (CoderException | RuntimeException e) {
61             logger.warn("Invalid Policy: {}", policy);
62             return false;
63         }
64
65         DroolsConfiguration newConfig =
66                 new DroolsConfiguration(
67                         nativePolicy.getProperties().getRulesArtifact().getArtifactId(),
68                         nativePolicy.getProperties().getRulesArtifact().getGroupId(),
69                         nativePolicy.getProperties().getRulesArtifact().getVersion());
70
71         PolicyControllerConstants.getFactory().patch(controller, newConfig);
72         return true;
73     }
74
75     @Override
76     public boolean undeploy(ToscaPolicy policy) {
77         PolicyController controller;
78         try {
79             NativeArtifactPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
80             controller =
81                     PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getController().getName());
82         } catch (RuntimeException | CoderException e) {
83             logger.warn("Invalid Policy: {}", policy);
84             return false;
85         }
86
87         DroolsConfiguration noConfig =
88                 new DroolsConfiguration(
89                         DroolsControllerConstants.NO_ARTIFACT_ID,
90                         DroolsControllerConstants.NO_GROUP_ID,
91                         DroolsControllerConstants.NO_VERSION);
92
93         PolicyControllerConstants.getFactory().patch(controller, noConfig);
94         return true;
95     }
96 }