Rework the activate pdp group payload
[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.clds.util.JsonUtils;
39 import org.onap.clamp.loop.Loop;
40 import org.onap.clamp.policy.microservice.MicroServicePolicy;
41 import org.onap.clamp.policy.operational.OperationalPolicy;
42
43 public class PolicyComponent extends ExternalComponent {
44
45     @Transient
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class);
47
48     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
49             "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100);
50     public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT",
51             "The policies defined have NOT yet been created on the policy engine", 90);
52     public static final ExternalComponentState SENT = new ExternalComponentState("SENT",
53             "The policies defined have been created but NOT deployed on the policy engine", 50);
54     public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
55             "The policies defined have been created and deployed on the policy engine", 10);
56     public static final ExternalComponentState UNKNOWN = new ExternalComponentState("UNKNOWN",
57             "The current status is not clear. Need to regresh the status to get the current status.", 0);
58
59     /**
60      * Default constructor.
61      */
62     public PolicyComponent() {
63         /*
64          * We assume it's good by default as we will receive the state for each policy
65          * on by one, each time we increase the level we can't decrease it anymore.
66          * That's why it starts with the lowest one SENT_AND_DEPLOYED.
67          */
68         super(UNKNOWN);
69     }
70
71     @Override
72     public String getComponentName() {
73         return "POLICY";
74     }
75
76     /**
77      * Generates the Json that must be sent to policy to add all policies to Active
78      * PDP group.
79      *
80      * @return The json, payload to send
81      */
82     public static String createPoliciesPayloadPdpGroup(Loop loop) {
83         JsonObject jsonObject = new JsonObject();
84         JsonArray jsonArray = new JsonArray();
85         jsonObject.add("groups", jsonArray);
86
87         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
88             jsonArray.add(createPdpDeploymentPayload(opPolicy.getPdpGroup(), opPolicy.getPdpSubGroup(),
89                     opPolicy.getPolicyModel().getPolicyModelType(), opPolicy.getPolicyModel().getVersion()));
90         }
91
92         for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) {
93             jsonArray.add(createPdpDeploymentPayload(msPolicy.getPdpGroup(), msPolicy.getPdpSubGroup(),
94                     msPolicy.getPolicyModel().getPolicyModelType(), msPolicy.getPolicyModel().getVersion()));
95         }
96
97         String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
98         logger.info("PdpGroup policy payload: " + payload);
99         return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
100     }
101
102     private static JsonObject createPdpDeploymentPayload(String pdpGroup, String pdpSubGroup,
103             String policyType, String version) {
104         JsonObject pdpGroupNode = new JsonObject();
105         JsonArray subPdpArray = new JsonArray();
106         pdpGroupNode.addProperty("name", pdpGroup);
107         pdpGroupNode.add("deploymentSubgroups", subPdpArray);
108
109         JsonObject pdpSubGroupNode = new JsonObject();
110         subPdpArray.add(pdpSubGroupNode);
111         pdpSubGroupNode.addProperty("pdpType", pdpSubGroup);
112         pdpSubGroupNode.addProperty("action", "POST");
113
114         JsonArray policyArray = new JsonArray();
115         pdpSubGroupNode.add("policies", policyArray);
116         JsonObject policyNode = new JsonObject();
117         policyNode.addProperty("name", policyType);
118         policyNode.addProperty("version", version);
119         policyArray.add(policyNode);
120         return pdpGroupNode;
121     }
122
123     /**
124      * Generates the list of policy names that must be send/remove to/from active
125      * PDP group.
126      *
127      * @return A list of policy names
128      */
129     public static List<String> listPolicyNamesPdpGroup(Loop loop) {
130         List<String> policyNamesList = new ArrayList<>();
131         for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
132             policyNamesList.add(opPolicy.getName());
133             for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
134                 policyNamesList.add(guardName);
135             }
136         }
137         for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) {
138             policyNamesList.add(microServicePolicy.getName());
139         }
140         return policyNamesList;
141     }
142
143     private static ExternalComponentState findNewState(boolean found, boolean deployed) {
144
145         ExternalComponentState newState = NOT_SENT;
146         if (found && deployed) {
147             newState = SENT_AND_DEPLOYED;
148         } else if (found) {
149             newState = SENT;
150         } else if (deployed) {
151             newState = IN_ERROR;
152         }
153         return newState;
154     }
155
156     private static ExternalComponentState mergeStates(ExternalComponentState oldState,
157             ExternalComponentState newState) {
158         return (oldState.compareTo(newState) < 0) ? newState : oldState;
159     }
160
161     /**
162      * This is a method that expect the results of the queries getPolicy and
163      * getPolicyDeployed for a unique policy (op,guard, config, etc ...). It
164      * re-computes the global policy state for each policy results given. Therefore
165      * this method is called multiple times from the camel route and must be reset
166      * for a new global policy state retrieval. The state to compute the global
167      * policy state is stored in this class.
168      * 
169      */
170     @Override
171     public ExternalComponentState computeState(Exchange camelExchange) {
172         this.setState(mergeStates(this.getState(),
173                 findNewState((boolean) camelExchange.getIn().getExchange().getProperty("policyFound"),
174                         (boolean) camelExchange.getIn().getExchange().getProperty("policyDeployed"))));
175         return this.getState();
176     }
177 }