aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-ControlloopPolicy/src/main/java/org/openecomp/policy/controlloop/policy/guard/builder/impl/ControlLoopGuardBuilderImpl.java
blob: bd6a3e42fd2b44183f4e48e6984ca12e477d0ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*-
 * ============LICENSE_START=======================================================
 * ECOMP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.policy.controlloop.policy.guard.builder.impl;

import java.util.LinkedList;

import org.openecomp.policy.controlloop.compiler.CompilerException;
import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
import org.openecomp.policy.controlloop.policy.builder.BuilderException;
import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
import org.openecomp.policy.controlloop.policy.builder.Results;
import org.openecomp.policy.controlloop.policy.builder.impl.MessageImpl;
import org.openecomp.policy.controlloop.policy.builder.impl.ResultsImpl;
import org.openecomp.policy.controlloop.policy.guard.Constraint;
import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
import org.openecomp.policy.controlloop.policy.guard.Guard;
import org.openecomp.policy.controlloop.policy.guard.GuardPolicy;
import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.Yaml;

public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {

	private ControlLoopGuard cLGuard;
	
	public ControlLoopGuardBuilderImpl(Guard guard) {
		cLGuard = new ControlLoopGuard();
		cLGuard.setGuard(guard);
	}
	
	@Override
	public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
		if (policies == null) {
			throw new BuilderException("GuardPolicy must not be null");
		}
		for (GuardPolicy policy : policies) {
			if (!policy.isValid()) {
				throw new BuilderException("Invalid guard policy - some required fields are missing");
			}
			if (cLGuard.getGuards() == null) {
				cLGuard.setGuards(new LinkedList<>());
			}
			cLGuard.getGuards().add(policy);
		}
		return this;
	}

	@Override
	public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
		if (policies == null) {
            throw new BuilderException("GuardPolicy must not be null");
        }
        if (cLGuard.getGuards() == null) {
            throw new BuilderException("No existing guard policies to remove");
        }
        for (GuardPolicy policy : policies) {
        	if (!policy.isValid()) {
				throw new BuilderException("Invalid guard policy - some required fields are missing");
			}
            boolean removed = cLGuard.getGuards().remove(policy);
            if (!removed) {
                throw new BuilderException("Unknown guard policy: " + policy.getName());
            }
        }
        return this;
	}

	@Override
	public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
		cLGuard.getGuards().clear();
        return this;
	}

	@Override
	public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
		if (id == null) {
			throw new BuilderException("The id of target guard policy must not be null");
		}
		if (constraints == null) {
			throw new BuilderException("Constraint much not be null");
		}
		if (!addLimitConstraints(id,constraints)) {
			throw new BuilderException("No existing guard policy matching the id: " + id);
		}
		return this;
	}

	private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
		boolean exist = false;
		for (GuardPolicy policy: cLGuard.getGuards()) {
			//
			// We could have only one guard policy matching the id
			//
			if (policy.getId().equals(id)) {
				exist = true;
				for (Constraint cons: constraints) {
					if (!cons.isValid()) {
						throw new BuilderException("Invalid guard constraint - some required fields are missing");
					}
					if (policy.getLimit_constraints() == null) {
						policy.setLimit_constraints(new LinkedList<>());
					}
					policy.getLimit_constraints().add(cons);
				}
				break;
			}
		}
		return exist;
	}

	@Override
	public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
		if (id == null) {
			throw new BuilderException("The id of target guard policy must not be null");
		}
		if (constraints == null) {
			throw new BuilderException("Constraint much not be null");
		}
		if (!removeConstraints(id, constraints)) {
			throw new BuilderException("No existing guard policy matching the id: " + id);
		}
		return this;
	}

	private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
		boolean exist = false;
		for (GuardPolicy policy: cLGuard.getGuards()) {
			//
			// We could have only one guard policy matching the id
			//
			if (policy.getId().equals(id)) {
				exist = true;
				for (Constraint cons: constraints) {
					if (!cons.isValid()) {
						throw new BuilderException("Invalid guard constraint - some required fields are missing");
					}
					boolean removed = policy.getLimit_constraints().remove(cons);
					if (!removed) {
						throw new BuilderException("Unknown guard constraint: " + cons);
					}
				}
				break;
			}
		}
		return exist;
	}

	@Override
	public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
		if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
			throw new BuilderException("No guard policies exist");
		} 
		if (id == null) {
			throw new BuilderException("The id of target guard policy must not be null");
		}
		boolean exist = false;
		for (GuardPolicy policy: cLGuard.getGuards()) {
			if (policy.getId().equals(id)) {
				exist = true;
				policy.getLimit_constraints().clear();
			}
		}
		if (!exist) {
			throw new BuilderException("No existing guard policy matching the id: " + id);
		}
		return this;
	}

	
	private class BuilderCompilerCallback implements ControlLoopCompilerCallback {

		private ResultsImpl results = new ResultsImpl();
		
		@Override
		public boolean onWarning(String message) {
			results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
			return false;
		}

		@Override
		public boolean onError(String message) {
			results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
			return false;
		}
	}
	
	@Override
	public ControlLoopGuard getControlLoopGuard() {
		return new ControlLoopGuard(this.cLGuard);
	}	
	
	
	@Override
	public Results buildSpecification() {
		//
		// Dump the specification
		//
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(FlowStyle.BLOCK);
		options.setPrettyFlow(true);
		Yaml yaml = new Yaml(options);
		String dumpedYaml = yaml.dump(cLGuard);
		//
		// This is our callback class for our compiler
		//
		BuilderCompilerCallback callback = new BuilderCompilerCallback();
		//
		// Compile it
		//
		try {
			ControlLoopGuardCompiler.compile(cLGuard, callback);
		} catch (CompilerException e) {
			callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
		}
		//
		// Save the spec
		//
		callback.results.setSpecification(dumpedYaml);
		return callback.results;
	}

}