2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.lifecycle;
24 import lombok.AllArgsConstructor;
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;
41 public class PolicyTypeNativeArtifactController implements PolicyTypeController {
42 private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeArtifactController.class);
45 protected final ToscaConceptIdentifier policyType;
48 protected final LifecycleFsm fsm;
51 public boolean deploy(ToscaPolicy policy) {
52 NativeArtifactPolicy nativePolicy;
53 PolicyController controller;
55 nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
57 new DroolsConfiguration(
58 nativePolicy.getProperties().getRulesArtifact().getArtifactId(),
59 nativePolicy.getProperties().getRulesArtifact().getGroupId(),
60 nativePolicy.getProperties().getRulesArtifact().getVersion());
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);
69 return update(nativePolicy, droolsConfig);
70 } catch (CoderException | RuntimeException e) {
71 logger.warn("Invalid Policy: {}", policy);
77 public boolean undeploy(ToscaPolicy policy) {
79 NativeArtifactPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
81 new DroolsConfiguration(
82 DroolsControllerConstants.NO_ARTIFACT_ID,
83 DroolsControllerConstants.NO_GROUP_ID,
84 DroolsControllerConstants.NO_VERSION);
86 return update(nativePolicy, noConfig);
87 } catch (RuntimeException | CoderException e) {
88 logger.warn("Invalid Policy: {}", policy);
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;