acd6115fe3b02abc9a435e8d763267aaa236f4f5
[clamp.git] / src / main / java / org / onap / clamp / loop / components / external / PolicyComponent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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  */
23
24 package org.onap.clamp.loop.components.external;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonObject;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.persistence.Transient;
36
37 import org.apache.camel.Exchange;
38 import org.onap.clamp.loop.Loop;
39 import org.onap.clamp.policy.microservice.MicroServicePolicy;
40 import org.onap.clamp.policy.operational.OperationalPolicy;
41
42 public class PolicyComponent extends ExternalComponent {
43
44     @Transient
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class);
46
47     public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT",
48         "The policies defined have NOT yet been created on the policy engine");
49     public static final ExternalComponentState SENT = new ExternalComponentState("SENT",
50         "The policies defined have been created but NOT deployed on the policy engine");
51     public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
52         "The policies defined have been created and deployed on the policy engine");
53     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
54         "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent");
55
56     public PolicyComponent() {
57         super(NOT_SENT);
58     }
59
60     @Override
61     public String getComponentName() {
62         return "POLICY";
63     }
64
65     /**
66      * Generates the Json that must be sent to policy to add all policies to Active
67      * PDP group.
68      *
69      * @return The json, payload to send
70      */
71     public static String createPoliciesPayloadPdpGroup(Loop loop) {
72         JsonObject jsonObject = new JsonObject();
73         JsonArray jsonArray = new JsonArray();
74         jsonObject.add("policies", jsonArray);
75
76         for (String policyName : PolicyComponent.listPolicyNamesPdpGroup(loop)) {
77             JsonObject policyNode = new JsonObject();
78             jsonArray.add(policyNode);
79             policyNode.addProperty("policy-id", policyName);
80         }
81         String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
82         logger.info("PdpGroup policy payload: " + payload);
83         return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
84     }
85
86     /**
87      * Generates the list of policy names that must be send/remove to/from active
88      * PDP group.
89      *
90      * @return A list of policy names
91      */
92     public static List<String> listPolicyNamesPdpGroup(Loop loop) {
93         List<String> policyNamesList = new ArrayList<>();
94         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
95             policyNamesList.add(opPolicy.getName());
96             for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
97                 policyNamesList.add(guardName);
98             }
99         }
100         for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) {
101             policyNamesList.add(microServicePolicy.getName());
102         }
103         return policyNamesList;
104     }
105
106     @Override
107     public ExternalComponentState computeState(Exchange camelExchange) {
108         boolean oneNotFound = (boolean) camelExchange.getIn().getExchange().getProperty("atLeastOnePolicyNotFound");
109         boolean oneNotDeployed = (boolean) camelExchange.getIn().getExchange()
110             .getProperty("atLeastOnePolicyNotDeployed");
111
112         if (oneNotFound && oneNotDeployed) {
113             this.setState(NOT_SENT);
114         } else if (!oneNotFound && oneNotDeployed) {
115             this.setState(SENT);
116         } else if (!oneNotFound && !oneNotDeployed) {
117             this.setState(SENT_AND_DEPLOYED);
118         } else {
119             this.setState(IN_ERROR);
120         }
121         return this.getState();
122     }
123 }