55bfcc0f7f777e762063813f2a792170cd41b2ed
[policy/drools-applications.git] / controlloop / common / policy-yaml / README-guard-v2.0.0.md
1 Copyright 2018 AT&T Intellectual Property. All rights reserved.
2 This file is licensed under the CREATIVE COMMONS ATTRIBUTION 4.0 INTERNATIONAL LICENSE
3 Full license text at https://creativecommons.org/licenses/by/4.0/legalcode
4
5 ONAP Control Loop Guard
6
7 A control loop guard is a YAML specification for creating policy guard for ControlLoop.
8
9 ONAP Control Loop Guard Features:
10
11 * The Control Loop Guard can specify the frequency limiter and the blacklist of target entities but not both in the same Guard.
12 * Two parts are incorporated. One is the common guard header including guard version while the other part is a set of guard policies. 
13 * The Control Loop Guard should contain at least one guard policies.
14 * Each guard policy is bound to a specific Actor and Recipe.
15 * Each guard policy should have at least one limit constraints which define how the guard policy should be enforced.
16 * Supported Actors are APPC and SO. 
17
18 This SDK helps build the YAML specification for ONAP Control Loop Guard.
19
20 # Create Builder Object
21
22 To begin with, the ControlLoopGuardBuilder.Factory class has static methods that one should use to begin building a Control Loop Guard. It will return a [ControlLoopGuardBuilder object](src/main/java/org/onap/policy/controlloop/policy/guard/builder/ControlLoopGuardBuilder.java) that can then be used to continue to build and define the Control Loop Guard.
23
24 ```java
25                 ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
26 ```
27
28 # Add Guard Policy
29
30 After a guard builder has been created, the next step would be to add a guard policy to the newly created Control Loop Guard via the builder. To add a guard policy, use the addGuardPolicy() method.
31
32 ```java
33                 GuardPolicy policy = new GuardPolicy(
34                                                                 "unique_guard_vUSP_1", 
35                                                                 "APPC 5 Restart", 
36                                                                 "We only allow 5 restarts over 15 minute window during the day time hours (i.e. avoid midnight to 5am)",
37                                                                 "APPC", 
38                                                                 "Restart");     
39                 builder = builder.addGuardPolicy(policy);
40 ```
41
42 # Add Limit Constraint to a Guard Policy
43
44 The limit constraint defines the details of how to enforce the guard policy. Each limit constraint can contain two types of constraints - frequency limiter and black list. At least one type of constraints should be specified, otherwise the limit constraint will be counted as invalid. To add a limit constraint to an existing guard policy, use the addLimitConstraint() method.
45
46 ```java
47                 Map<String, String> time_in_range = new HashMap<String, String>();
48                 time_in_range.put("arg2", "PT5H");
49                 time_in_range.put("arg3", "PT24H");
50                 List<String> blacklist = new LinkedList<String>();
51                 blacklist.add("vm_name_1");
52                 blacklist.add("vm_name_2");
53                 Constraint cons = new Constraint(5, "PT15M", time_in_range, blacklist);
54                 builder = builder.addLimitConstraint(policy.id, cons);
55 ```
56
57
58 # Build the YAML Specification
59
60 When finished defining the Guard Policies, build the specification and analyze the [Results.java](src/main/java/org/onap/policy/controlloop/policy/builder/Results.java)
61
62 ```java
63                 Results results = builder.buildSpecification();
64                 if (results.isValid()) {
65                         System.out.println(results.getSpecification());
66                 } else {
67                         System.err.println("Builder failed");
68                         for (Message message : results.getMessages()) {
69                                 System.err.println(message.getMessage());
70                         }
71                 }
72 ```
73
74
75 # Use the YAML Specification to Generate the XACML Guard Policies
76
77 Now that you have a valid YAML specification, call the method in [PolicyGuardYamlToXacml.java](guard/src/main/java/org/onap/policy/guard/PolicyGuardYamlToXacml.java) to generate the XACML Guard Policies.
78
79
80 # YAML Specification
81
82 The YAML specification has 2 sections to it: [guard](#guard-object) and [guards](#guards-array). The [guard section](#guard-object) section is simply a header defining the version of this guard. The [guards section](#guards-array) is simply an array of [GuardPolicy objects](#guardpolicy-object).
83
84 ## guard Object
85
86 | Field Name      | Type          | Required   | Description  |
87 | -------------   |:-------------:| -----------| ------------:|
88 | version         | string        | required   | Value for this release if 2.0.0 |
89
90
91 ## guards array
92
93 The guards section is an array of [GuardPolicy objects](#guardpolicy-object).
94
95 ### GuardPolicy Object
96
97 | Field Name      | Type          | Required   | Description  |
98 | -------------   |:-------------:| -----------| ------------:|
99 | id              | string        | required   | Unique ID for the policy. |
100 | name            | string        | required   | Policy name |
101 | description     | string        | optional   | Policy description |
102 | actor           | string        | required   | Name of the actor for this operation: Example: APPC |
103 | recipe          | string        | required   | Name of recipe to be performed. Example "Restart" |
104 | limit_constraints  | array of [constraint](#constraint-object) object | required | Constraints used to enforce the guard policy |
105
106 The guard policy is bound to a specific recipe performed by the actor. When the Control Loop tries to perform the recipe operation by the actor, this guard policy should be evaluated against all the specified constraints. If any of the constraints will be violated, the operation should be abandoned.
107
108 #### constraint Object
109
110 | Field Name      | Type          | Required   | Description  |
111 | -------------   |:-------------:| -----------| ------------:|
112 | num             | integer       | required if blacklist is not specified  | The limited number of the same operations |
113 | duration        | string        | required if blacklist is not specified  | Time window for counting the same operations |
114 | time_in_range   | map<string, string> | optional   | Valid time spans for enforcing the guard policy |
115 | blacklist       | array of string     | required if num and duration are not specified | A list of the entity names that should not be touched by the Control Loop |
116
117 The first three attributes define the frequency limiter which means that only a limited number of the same operations can be allowed within each valid time window. The last attribute defines a blacklist of the target entities on which the Control Loop should not perform the operation.
118   
119 The "duration" parameter should have one of the following values: [5min, 10min, 30min, 1h, 12h, 1d, 5d, 1w, 1mon].
120
121   
122 ## Examples of YAML Control Loop Guards
123
124 [vService-Frequency-Limiter-Guard](src/test/resources/v2.0.0-guard/policy_guard_appc_restart.yaml)
125 [vService-Blacklist-Guard](src/test/resources/v2.0.0-guard/policy_guard_blacklist.yaml)
126 [ONAP-vDNS-Guard](src/test/resources/v2.0.0-guard/policy_guard_ONAP_demo_vDNS.yaml)
127
128
129 ### vService Frequency Limiter Guard
130 ```
131 guard:
132   version: 2.0.0
133
134 guards:
135   - id: unique_guard_vService_frequency_limiter
136     name: APPC 5 Restart
137     description: 
138       We only allow 5 restarts over 15 minute window during the day time hours (i.e. avoid midnight to 5am)
139     actor: APPC
140     recipe: Restart
141     limit_constraints:
142       - num: 5
143         duration: PT15M
144         time_in_range:
145           arg2: PT5H
146           arg3: PT24H   
147 ```
148
149
150 ### vService Blacklist Guard
151 ```
152 guard:
153   version: 2.0.0
154
155 guards:
156   - id: unique_guard_vService_blacklist
157     name: APPC Restart Blacklist
158     description: |
159       We deny restart of the blacklisted targets (avoid midnight to 5am)
160     actor: APPC
161     recipe: Restart
162     limit_constraints:
163       - blacklist:
164           - TargetName1
165           - TargetName2
166         time_in_range:
167           arg2: 00:00:00-05:00
168           arg3: 23:59:59-05:00
169 ```
170
171
172 ### ONAP vDNS Guard
173 ```
174 guard:
175   version: 2.0.0
176
177 guards:
178   - id: unique_guard_ONAP_vDNS_1
179     name: SO Spinup
180     description: We only spin up 1 instance over a 10 minute window
181     actor: SO
182     recipe: VF Module Create
183     limit_constraints:
184       - num: 1
185         duration: PT10M
186 ```
187