2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.monitoring;
23 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
30 * This class is used to monitor context creates, deletes, gets, sets, locks and unlocks on context items in Apex
33 * @author Liam Fallon (liam.fallon@ericsson.com)
35 public class ContextMonitor {
36 // Logger for this class
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextMonitor.class);
40 * Monitor an initiation on a context item.
42 * @param albumKey The item album
43 * @param schemaKey The item schema
44 * @param name The name of the item
45 * @param value The value of the item
47 public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
49 String monitorInitString = monitor("INIT", null, albumKey, schemaKey, name, value);
50 LOGGER.trace(monitorInitString);
54 * Monitor an initiation on a context item.
56 * @param albumKey The item album
57 * @param schemaKey The item schema
58 * @param name The name of the item
59 * @param value The value of the item
60 * @param userArtifactStack the keys of the artifacts using the context map at the moment
62 public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
63 final Object value, final AxConcept[] userArtifactStack) {
64 String monitorInitString = monitor("INIT", userArtifactStack, albumKey, schemaKey, name, value);
65 LOGGER.trace(monitorInitString);
69 * Monitor a deletion on a context item.
71 * @param albumKey The item album
72 * @param schemaKey The item schema
73 * @param name The name of the item
74 * @param value The value of the item
75 * @param userArtifactStack the keys of the artifacts using the context map at the moment
77 public void monitorDelete(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
78 final Object value, final AxConcept[] userArtifactStack) {
79 String monitorDeleteString = monitor("DEL", userArtifactStack, albumKey, schemaKey, name, value);
80 LOGGER.trace(monitorDeleteString);
84 * Monitor get on a context item.
86 * @param albumKey The item album
87 * @param schemaKey The item schema
88 * @param name The name of the item
89 * @param value The value of the item
90 * @param userArtifactStack the keys of the artifacts using the context map at the moment
92 public void monitorGet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
93 final Object value, final AxConcept[] userArtifactStack) {
94 String monitorGetString = monitor("GET", userArtifactStack, albumKey, schemaKey, name, value);
95 LOGGER.trace(monitorGetString);
99 * Monitor set on a context item.
101 * @param albumKey The item album
102 * @param schemaKey The item schema
103 * @param name The name of the item
104 * @param value The value of the item
105 * @param userArtifactStack the keys of the artifacts using the context map at the moment
107 public void monitorSet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
108 final Object value, final AxConcept[] userArtifactStack) {
109 String monitorSetString = monitor("SET", userArtifactStack, albumKey, schemaKey, name, value);
110 LOGGER.trace(monitorSetString);
114 * Monitor a read lock on a key.
116 * @param albumKey The item album
117 * @param schemaKey The item schema
118 * @param name The name of the item
119 * @param userArtifactStack the keys of the artifacts using the context map at the moment
121 public void monitorReadLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
122 final AxConcept[] userArtifactStack) {
123 String monitorReadLockString = monitor("READLOCK", userArtifactStack, albumKey, schemaKey, name, null);
124 LOGGER.trace(monitorReadLockString);
128 * Monitor a write lock on a key.
130 * @param albumKey The item album
131 * @param schemaKey The item schema
132 * @param name The name of the item
133 * @param userArtifactStack the keys of the artifacts using the context map at the moment
135 public void monitorWriteLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
136 final AxConcept[] userArtifactStack) {
137 String writeLockMonitorString = monitor("WRITELOCK", userArtifactStack, albumKey, schemaKey, name, null);
138 LOGGER.trace(writeLockMonitorString);
142 * Monitor a read unlock on a key.
144 * @param albumKey The item album
145 * @param schemaKey The item schema
146 * @param name The name of the item
147 * @param userArtifactStack the keys of the artifacts using the context map at the moment
149 public void monitorReadUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
150 final AxConcept[] userArtifactStack) {
151 String monitorReadUnlockString = monitor("READUNLOCK", userArtifactStack, albumKey, schemaKey, name, null);
152 LOGGER.trace(monitorReadUnlockString);
156 * Monitor a write unlock on a key.
158 * @param albumKey The item album
159 * @param schemaKey The item schema
160 * @param name The name of the item
161 * @param userArtifactStack the keys of the artifacts using the context map at the moment
163 public void monitorWriteUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
164 final AxConcept[] userArtifactStack) {
165 String monitorWriteUnlockString = monitor("WRITEUNLOCK", userArtifactStack, albumKey, schemaKey, name, null);
166 LOGGER.trace(monitorWriteUnlockString);
170 * Monitor the user artifact stack.
172 * @param preamble the preamble
173 * @param userArtifactStack The user stack to print
174 * @param albumKey the album key
175 * @param schemaKey the schema key
176 * @param name the name
177 * @param value the value
180 private String monitor(final String preamble, final AxConcept[] userArtifactStack, final AxArtifactKey albumKey,
181 final AxArtifactKey schemaKey, final String name, final Object value) {
182 final StringBuilder builder = new StringBuilder();
184 builder.append(preamble);
185 builder.append(",[");
187 if (userArtifactStack != null) {
188 boolean first = true;
189 for (final AxConcept stackKey : userArtifactStack) {
195 if (stackKey instanceof AxArtifactKey) {
196 builder.append(((AxArtifactKey) stackKey).getId());
197 } else if (stackKey instanceof AxReferenceKey) {
198 builder.append(((AxReferenceKey) stackKey).getId());
200 builder.append(stackKey.toString());
204 builder.append("],");
206 builder.append(albumKey.getId());
208 builder.append(schemaKey.getId());
210 builder.append(name);
214 builder.append(value.toString());
217 return builder.toString();