aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/m2/guard/src/main/java/org/onap/policy/guard/GuardContext.java
blob: 75163e24ec854f3b57fe1a9143a60eceb28eef8d (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*-
 * ============LICENSE_START=======================================================
 * guard
 * ================================================================================
 * Copyright (C) 2020 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.onap.policy.guard;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.Properties;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.drools.core.WorkingMemory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.onap.policy.drools.core.PolicyContainer;
import org.onap.policy.drools.core.PolicySession;
import org.onap.policy.drools.system.PolicyControllerConstants;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.util.DroolsSessionCommonSerializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Each instance of this class is initialized from a 'Properties' object,
 * which is most likely a '*-controller.properties' file. The expectation is
 * that it will be initialized within a '.drl' file, and be referenced by
 * the associated 'Context' object.
 */
public class GuardContext implements Serializable {
    private static final long serialVersionUID = 1L;

    private static Logger logger = LoggerFactory.getLogger(GuardContext.class);

    // object that should be serialized
    private Object namedSerializable;

    /*==================================*/
    /* fields extracted from properties */
    /*==================================*/
    // contains the four database properties, 'javax.persistence.jdbc.*',
    private Properties dbProperties = null;

    // initialized from 'guard.disabled', but may also be set to 'true' if
    // there is an initialization error
    private boolean disabled = false;

    // errors that forced 'disabled' to be set to 'true'
    private String errorMessage = null;

    /*======================================================*/
    /* fields that shouldn't be included in serialized data */
    /*======================================================*/

    // derived from DB properties
    private transient EntityManagerFactory emf = null;

    /**
     * Constructor - initialize the 'GuardContext' instance using the
     * controller's properties file. The properties file is located using a
     * 'PolicySession' instance, but the way this mapping is done isn't
     * perfect -- it may use the wrong properties file if there is another
     * 'PolicyContainer' instance using the same 'artifactId' and 'groupId'.
     *
     * @param session the 'PolicySession' instance used to locate the associated
     *     'Properties' instance
     */
    public GuardContext(PolicySession session) {
        this(session, null);
    }

    /**
     * Constructor - initialize the 'GuardContext' instance using the
     * controller's properties file. The properties file is located using a
     * 'PolicySession' instance, but the way this mapping is done isn't
     * perfect -- it may use the wrong properties file if there is another
     * 'PolicyContainer' instance using the same 'artifactId' and 'groupId'.
     *
     * @param session the 'PolicySession' instance used to locate the associated
     *     'Properties' instance
     * @param serializableName a String name unique within the Drools session
     *     that can be used to locate the corresponding 'GuardContext' object
     *     on the remote host
     */
    public GuardContext(PolicySession session, String serializableName) {
        namedSerializable =
            (serializableName == null ? this :
             new DroolsSessionCommonSerializable(serializableName, this));

        // At present, there is no simple way to get the properties based
        // upon a 'PolicyContainer'. Instead, we search through all of the
        // 'PolicyController' instances looking for one with a matching
        // 'artifactId' and 'groupId'. Note that this may not work correctly
        // if there is more than one controller using the same or different
        // version of the same artifact.

        PolicyContainer container = session.getPolicyContainer();
        String artifactId = container.getArtifactId();
        String groupId = container.getGroupId();

        Properties properties =
            PolicyControllerConstants.getFactory().get(groupId, artifactId).getProperties();
        init(properties);
    }

    /**
     * Constructor - initialize the 'GuardContext' instance using the
     * specified properties.
     *
     * @param properties configuration data used to initialize the 'GuardContext' instance
     */
    public GuardContext(Properties properties) {
        init(properties);
    }

    /**
     * Common initialization routine code used by both constructors.
     *
     * @param properties configuration data used to initialize the 'GuardContext' instance
     */
    private void init(Properties properties) {
        // used to store error messages
        StringBuilder sb = new StringBuilder();

        // fetch these parameters, if they exist
        String disabledString =
            PolicyEngineConstants.getManager().getEnvironmentProperty(Util.PROP_GUARD_DISABLED);

        if (disabledString != null) {
            // decode optional 'guard.disabled' parameter
            disabled = Boolean.valueOf(disabledString);
            if (disabled) {
                // skip everything else
                return;
            }
        }

        // extract 'guard.java.persistence.jdbc.*' parameters,
        // which are all mandatory
        dbProperties = new Properties();
        setProperty(dbProperties, Util.ONAP_KEY_URL, PersistenceUnitProperties.JDBC_URL, sb);
        setProperty(dbProperties, Util.ONAP_KEY_USER, PersistenceUnitProperties.JDBC_USER, sb);
        setProperty(dbProperties, Util.ONAP_KEY_PASS, PersistenceUnitProperties.JDBC_PASSWORD, sb);
        String driver = properties.getProperty("guard." + PersistenceUnitProperties.JDBC_DRIVER);
        if (driver != null) {
            dbProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, driver);
        }
        dbProperties.setProperty(Util.PROP_GUARD_PERSISTENCE_UNIT,
                        properties.getProperty(Util.PROP_GUARD_PERSISTENCE_UNIT, Util.PU_KEY));

        // if there are any errors, update 'errorMessage' & disable guard queries
        if (sb.length() != 0) {
            // remove the terminating ", ", and extract resulting error message
            sb.setLength(sb.length() - 2);
            errorMessage = sb.toString();
            disabled = true;
            logger.error("Initialization failure: {}", errorMessage);
        }
    }

    /**
     * Fetch a property from the PolicyEngine environment, and store it in
     * a corresponding property in 'properties'.
     *
     * @param properties the location to store the properties
     * @param srcName source environment property name
     * @param destName destination property name
     * @param log a 'StringBuilder' used to construct an error message, if needed
     */
    private void setProperty(Properties properties, String srcName, String destName, StringBuilder log) {
        String value =
            PolicyEngineConstants.getManager().getEnvironmentProperty(srcName);
        if (value == null) {
            log.append("'").append(srcName).append("' is not defined, ");
        } else {
            properties.setProperty(destName, value);
        }
    }

    /**
     * Do an asynchronous (non-blocking) HTTP REST query to see if this
     * operation is permitted by 'guard'. The response is returned by
     * inserting a 'PolicyGuardResponse' instance into Drools memory.
     *
     * @param workingMemory the Drools response is inserted here
     * @param actor the processor being acted upon (e.g. "APPC")
     * @param recipe otherwise known as "operation" (e.g. "Restart")
     * @param target a further qualifier on 'actor'? (e.g. "VM")
     * @param requestId the UUID string identifying the overall request
     */
    public void asyncQuery(
        WorkingMemory workingMemory,
        String actor, String recipe, String target,
        String requestId) {

        asyncQuery(workingMemory, actor, recipe, target, requestId, null);
    }

    /**
     * Do an asynchronous (non-blocking) HTTP REST query to see if this
     * operation is permitted by 'guard'. The response is returned by
     * inserting a 'PolicyGuardResponse' instance into Drools memory.
     *
     * @param workingMemory the Drools response is inserted here
     * @param actor the processor being acted upon (e.g. "APPC")
     * @param recipe otherwise known as "operation" (e.g. "Restart")
     * @param target a further qualifier on 'actor'? (e.g. "VM")
     * @param requestId the UUID string identifying the overall request
     * @param controlLoopName the 'controlLoopName' value or 'null'
     *     (if 'null', it is ommitted from the query to 'guard')
     */
    public void asyncQuery(
        final WorkingMemory workingMemory,
        final String actor, final String recipe, final String target,
        final String requestId, final String controlLoopName) {

        if (disabled) {
            logger.error("query skipped: {}", errorMessage);
            workingMemory.insert(
                new PolicyGuardResponse("Deny", UUID.fromString(requestId), recipe));
            return;
        }

        CallGuardTask cgt = new CallGuardTask(workingMemory, controlLoopName,
            actor, recipe, target, requestId, () -> null);

        PolicyEngineConstants.getManager().getExecutorService().execute(cgt);
    }

    /**
     * Create an 'EntityManagerFactory', if needed, and then create a new
     * 'EntityManager' instance.
     *
     * @return a new 'EntityManager' instance
     */
    private EntityManager createEntityManager() {
        if (emf == null) {
            // 'EntityManagerFactory' does not exist yet -- create one

            // copy database properties to a 'HashMap'
            HashMap<Object, Object> propertiesMap = new HashMap<>(dbProperties);

            // use 'ClassLoader' from Drools session
            propertiesMap.put("eclipselink.classloader",
                              GuardContext.class.getClassLoader());

            // create DB tables, if needed
            propertiesMap.put("eclipselink.ddl-generation", "create-tables");

            // create entity manager factory
            String persistenceUnit = dbProperties.getProperty(Util.PROP_GUARD_PERSISTENCE_UNIT);
            emf = Persistence.createEntityManagerFactory(persistenceUnit, propertiesMap);
        }

        // create and return the 'EntityManager'
        return emf.createEntityManager();
    }

    /**
     * This is a synchronous (blocking) method, which creates a database entity
     * for an in-progress request.
     *
     * @param starttime this is used as the 'starttime' timestamp in the record
     * @param endtime this is used as the 'endtime' timestamp in the record
     * @param closedLoopControlName uniquely identifies the Drools rules
     * @param actor the processor being acted upon (e.g. "APPC")
     * @param recipe otherwise known as "operation" (e.g. "Restart")
     * @param target a further qualifier on 'actor'? (e.g. "VM")
     * @param requestId the UUID string identifying the overall request
     * @param subRequestId further qualifier on 'requestId'
     * @param message indicates success status, or reason for failure
     * @param outcome 'PolicyResult' enumeration string
     * @return 'true' if the operation was successful, and 'false' if not
     */
    public boolean createDbEntry(
        Instant starttime, Instant endtime, String closedLoopControlName,
        String actor, String recipe, String target,
        String requestId, String subRequestId, String message, String outcome) {

        if (disabled) {
            if (errorMessage != null) {
                logger.error("Database update skipped: {}", errorMessage);
            }
            return false;
        }

        EntityManager em = null;
        boolean rval = false;

        try {
            em = createEntityManager();

            // create the new DB table entry
            OperationsHistory newEntry = new OperationsHistory();

            // populate the new DB table entry
            newEntry.setClosedLoopName(closedLoopControlName);
            newEntry.setRequestId(requestId);
            newEntry.setActor(actor);
            newEntry.setOperation(recipe);
            newEntry.setTarget(target);
            newEntry.setStarttime(new Timestamp(starttime.toEpochMilli()));
            newEntry.setSubrequestId(subRequestId);

            newEntry.setEndtime(new Timestamp(endtime.toEpochMilli()));
            newEntry.setMessage(message);
            newEntry.setOutcome(outcome);

            // store the new entry in the DB
            em.getTransaction().begin();
            em.persist(newEntry);
            em.getTransaction().commit();

            rval = true;
        } finally {
            // free EntityManager
            if (em != null) {
                em.close();
            }
        }
        return rval;
    }

    /**
     * This is an asynchronous (non-blocking) method, which creates a database
     * entity for an in-progress request.
     *
     * @param starttime this is used as the 'starttime' timestamp in the record
     * @param endtime this is used as the 'endtime' timestamp in the record
     * @param closedLoopControlName uniquely identifies the Drools rules
     * @param actor the processor being acted upon (e.g. "APPC")
     * @param recipe otherwise known as "operation" (e.g. "Restart")
     * @param target a further qualifier on 'actor'? (e.g. "VM")
     * @param requestId the UUID string identifying the overall request
     * @param subRequestId further qualifier on 'requestId'
     * @param message indicates success status, or reason for failure
     * @param outcome 'PolicyResult' enumeration string
     */
    public void asyncCreateDbEntry(
        final Instant starttime, final Instant endtime,
        final String closedLoopControlName,
        final String actor, final String recipe, final String target,
        final String requestId, final String subRequestId,
        final String message, final String outcome) {
        if (disabled) {
            if (errorMessage != null) {
                logger.error("Database update skipped: {}", errorMessage);
            }
            return;
        }

        PolicyEngineConstants.getManager().getExecutorService().execute(() -> {
            try {
                // using a separate thread, call the synchronous 'createDbEntry'
                // method
                createDbEntry(starttime, endtime, closedLoopControlName,
                              actor, recipe, target, requestId, subRequestId,
                              message, outcome);
            } catch (Exception e) {
                logger.error("GuardContext.asyncCreateDbEntry", e);
            }
        });
    }

    /**
     * This method is used as part of serialization -- 'namedSerializable'
     * is serialized instead of 'this'.
     *
     * @return the object to be serialized
     */
    private Object writeReplace() throws ObjectStreamException {
        return namedSerializable;
    }
}