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