Update css file name in conf.py
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / MicroServicesPolicyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policyengine;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.StringReader;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.UUID;
34
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.json.JsonReader;
38
39 import org.onap.policy.api.PolicyChangeResponse;
40 import org.onap.policy.api.PolicyConfigType;
41 import org.onap.policy.api.PolicyEngine;
42 import org.onap.policy.api.PolicyParameters;
43 import org.onap.policy.api.PolicyType;
44
45 public class MicroServicesPolicyClient {
46
47     // For updating a Micro Services policy set the "isEdit" flag to true.
48     // For creating a Micro Services policy set the "isEdit" flag to false.
49     static Boolean isEdit = false;
50
51     // Builds JSONObject from File
52     private static JsonObject buildJson(File jsonInput, String jsonString) throws FileNotFoundException {
53         JsonObject json = null;;
54         JsonReader jsonReader = null;
55         if (jsonString != null && jsonInput == null) {
56             StringReader in = null;
57             in = new StringReader(jsonString);
58             jsonReader = Json.createReader(in);
59             json = jsonReader.readObject();
60             in.close();
61         } else {
62             InputStream in = null;
63             in = new FileInputStream(jsonInput);
64             jsonReader = Json.createReader(in);
65             json = jsonReader.readObject();
66             try {
67                 in.close();
68             } catch (IOException e) {
69                 System.err.println("Exception Occured while closing input stream" + e);
70             }
71         }
72         jsonReader.close();
73         return json;
74     }
75
76     /**
77      * main.
78      *
79      * @param args String[] args
80      */
81     public static void main(String[] args) {
82         try {
83             PolicyEngine policyEngine = new PolicyEngine("config.properties");
84             PolicyParameters policyParameters = new PolicyParameters();
85             // Set Policy Type
86             policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
87             policyParameters.setPolicyName("Katrina.configuration_dcae_microservice_stringmatcher");
88             policyParameters.setOnapName("DCAE");
89
90             // Set up Micro Services Attributes
91             File jsonFile = null;
92             String msJsonString = null;
93             Path file = Paths.get("C:\\policyAPI\\MicroServicesJSON\\testStringMatching.json");
94             jsonFile = file.toFile();
95
96             policyParameters.setConfigBody(buildJson(jsonFile, msJsonString).toString());
97             policyParameters.setConfigBodyType(PolicyType.JSON);
98
99             policyParameters.setRequestID(UUID.randomUUID());
100             // Set Safe Policy value for Risk Type
101             SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
102             Date date = dateformat3.parse("15/10/2016");
103             policyParameters.setTtlDate(date);
104             // Set Safe Policy value for Guard
105             policyParameters.setGuard(true);
106             // Set Safe Policy value for Risk Level
107             policyParameters.setRiskLevel("5");
108             // Set Safe Policy value for Risk Type
109             policyParameters.setRiskType("PROD");
110
111             // API method to create or update Policy.
112             PolicyChangeResponse response = null;
113             if (!isEdit) {
114                 response = policyEngine.createPolicy(policyParameters);
115             } else {
116                 response = policyEngine.updatePolicy(policyParameters);
117             }
118
119             if (response.getResponseCode() == 200) {
120                 System.out.println(response.getResponseMessage());
121                 System.out.println("Policy Created Successfully!");
122             } else {
123                 System.out.println("Error! " + response.getResponseMessage());
124             }
125         } catch (Exception e) {
126             System.err.println(e.getMessage());
127         }
128     }
129 }