2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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============================================
20 * ===================================================================
24 package org.onap.clamp.loop.components.external;
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;
32 import java.util.ArrayList;
33 import java.util.List;
35 import javax.persistence.Transient;
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;
42 public class PolicyComponent extends ExternalComponent {
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class);
47 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
48 "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100);
49 public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT",
50 "The policies defined have NOT yet been created on the policy engine", 90);
51 public static final ExternalComponentState SENT = new ExternalComponentState("SENT",
52 "The policies defined have been created but NOT deployed on the policy engine", 50);
53 public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
54 "The policies defined have been created and deployed on the policy engine", 10);
56 public PolicyComponent() {
58 * We assume it's good by default as we will receive the state for each policy
59 * on by one, each time we increase the level we can't decrease it anymore.
60 * That's why it starts with the lowest one SENT_AND_DEPLOYED.
62 super(SENT_AND_DEPLOYED);
66 public String getComponentName() {
71 * Generates the Json that must be sent to policy to add all policies to Active
74 * @return The json, payload to send
76 public static String createPoliciesPayloadPdpGroup(Loop loop) {
77 JsonObject jsonObject = new JsonObject();
78 JsonArray jsonArray = new JsonArray();
79 jsonObject.add("policies", jsonArray);
81 for (String policyName : PolicyComponent.listPolicyNamesPdpGroup(loop)) {
82 JsonObject policyNode = new JsonObject();
83 jsonArray.add(policyNode);
84 policyNode.addProperty("policy-id", policyName);
86 String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
87 logger.info("PdpGroup policy payload: " + payload);
88 return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
92 * Generates the list of policy names that must be send/remove to/from active
95 * @return A list of policy names
97 public static List<String> listPolicyNamesPdpGroup(Loop loop) {
98 List<String> policyNamesList = new ArrayList<>();
99 for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
100 policyNamesList.add(opPolicy.getName());
101 for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) {
102 policyNamesList.add(guardName);
105 for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) {
106 policyNamesList.add(microServicePolicy.getName());
108 return policyNamesList;
111 private static ExternalComponentState findNewState(boolean found, boolean deployed) {
113 ExternalComponentState newState = NOT_SENT;
114 if (found && deployed) {
115 newState = SENT_AND_DEPLOYED;
118 } else if (deployed) {
124 private static ExternalComponentState mergeStates(ExternalComponentState oldState,
125 ExternalComponentState newState) {
126 return (oldState.compareTo(newState) < 0) ? newState : oldState;
130 * This is a method that expect the results of the queries getPolicy and
131 * getPolicyDeployed for a unique policy (op,guard, config, etc ...). It
132 * re-computes the global policy state for each policy results given. Therefore
133 * this method is called multiple times from the camel route and must be reset
134 * for a new global policy state retrieval. The state to compute the global
135 * policy state is stored in this class.
139 public ExternalComponentState computeState(Exchange camelExchange) {
140 this.setState(mergeStates(this.getState(),
141 findNewState((boolean) camelExchange.getIn().getExchange().getProperty("policyFound"),
142 (boolean) camelExchange.getIn().getExchange().getProperty("policyDeployed"))));
143 return this.getState();