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.policy;
26 import java.io.IOException;
29 import org.onap.clamp.clds.config.ClampProperties;
30 import org.onap.clamp.policy.microservice.MicroServicePolicy;
31 import org.onap.clamp.util.HttpConnectionManager;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.google.gson.JsonArray;
38 import com.google.gson.JsonObject;
41 public class PolicyOperation {
42 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyOperation.class);
43 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
44 public static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type";
45 public static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name";
46 public static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix";
47 public static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix";
48 public static final String POLICY_MS_NAME_PREFIX_PROPERTY_NAME = "policy.ms.policyNamePrefix";
49 public static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type";
50 public static final String POLICY_GUARD_SUFFIX = "_Guard";
51 public static final String POLICY_URL_PROPERTY_NAME = "clamp.config.policy.url";
52 public static final String POLICY_URL_SUFFIX = "/versions/1.0.0/policies";
53 public static final String POLICY_USER_NAME = "clamp.config.policy.userName";
54 public static final String POLICY_PASSWORD = "clamp.config.policy.password";
56 public static final String TOSCA_DEF_VERSION = "tosca_definitions_version";
57 public static final String TOSCA_DEF_VERSION_VALUE = "tosca_simple_yaml_1_0_0";
58 public static final String TEMPLATE = "topology_template";
59 public static final String POLICIES = "policies";
60 public static final String MS_TYPE = "type";
61 public static final String MS_VERSION = "version";
62 public static final String MS_VERSION_VALUE = "1.0.0";
63 public static final String MS_METADATA = "metadata";
64 public static final String MS_POLICY_ID = "policy_id";
65 public static final String MS_PROPERTIES = "properties";
66 public static final String MS_policy = "tca_policy";
68 private final ClampProperties refProp;
69 private final HttpConnectionManager httpConnectionManager;
72 public PolicyOperation(ClampProperties refProp, HttpConnectionManager httpConnectionManager) {
73 this.refProp = refProp;
74 this.httpConnectionManager = httpConnectionManager;
77 public void createMsPolicy(Set<MicroServicePolicy> policyList) throws IOException {
78 // Get policy first? if exist delete???
80 for (MicroServicePolicy msPolicy:policyList) {
81 JsonObject payload = createMsPolicyPayload(msPolicy);
82 String policyType = msPolicy.getModelType();
83 String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX;
84 String userName = refProp.getStringValue(POLICY_USER_NAME);
85 String encodedPass = refProp.getStringValue(POLICY_PASSWORD);
86 httpConnectionManager.doHttpRequest(url, "POST", payload.toString(), "application/json", "POLICY", userName, encodedPass);
90 public void deleteMsPolicy(Set<MicroServicePolicy> policyList) throws IOException {
91 for (MicroServicePolicy msPolicy:policyList) {
92 String policyType = msPolicy.getModelType();
93 String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX + "/" + msPolicy.getName();
94 String userName = refProp.getStringValue(POLICY_USER_NAME);
95 String encodedPass = refProp.getStringValue(POLICY_PASSWORD);
96 httpConnectionManager.doHttpRequest(url, "POST", null, null, "POLICY", userName, encodedPass);
100 private JsonObject createMsPolicyPayload(MicroServicePolicy microService) {
101 JsonObject policyConfig = new JsonObject();
102 policyConfig.add(MS_policy, microService.getProperties());
104 JsonObject properties = new JsonObject();
105 properties.add(MS_policy, policyConfig);
107 JsonObject msPolicy = new JsonObject();
108 msPolicy.addProperty(MS_TYPE, microService.getModelType());
109 msPolicy.addProperty(MS_VERSION, MS_VERSION_VALUE);
110 JsonObject metaData = new JsonObject();
111 metaData.addProperty(MS_POLICY_ID, microService.getName());
112 msPolicy.add(MS_METADATA, metaData);
113 msPolicy.add(MS_PROPERTIES, properties);
115 JsonObject msPolicyWithName = new JsonObject();
116 msPolicyWithName.add(microService.getName(), msPolicy);
118 JsonArray policyArray = new JsonArray();
119 policyArray.add(msPolicyWithName);
121 JsonObject template = new JsonObject();
122 template.add(POLICIES, policyArray);
124 JsonObject configPolicy = new JsonObject();
125 configPolicy.addProperty(TOSCA_DEF_VERSION, TOSCA_DEF_VERSION_VALUE);
126 configPolicy.add(TEMPLATE, template);