8e34ecb1bc5874d6751f36c78d9cebc3aa303fa4
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.monitoring;
22
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;
28
29 /**
30  * This class is used to monitor context creates, deletes, gets, sets, locks and unlocks on context items in Apex
31  * context albums.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
35 public class ContextMonitor {
36     // Logger for this class
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextMonitor.class);
38
39     /**
40      * Monitor an initiation on a context item.
41      *
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
46      */
47     public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
48             final Object value) {
49         LOGGER.trace(monitor("INIT", null, albumKey, schemaKey, name, value));
50     }
51
52     /**
53      * Monitor an initiation on a context item.
54      *
55      * @param albumKey The item album
56      * @param schemaKey The item schema
57      * @param name The name of the item
58      * @param value The value of the item
59      * @param userArtifactStack the keys of the artifacts using the context map at the moment
60      */
61     public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
62             final Object value, final AxConcept[] userArtifactStack) {
63         LOGGER.trace(monitor("INIT", userArtifactStack, albumKey, schemaKey, name, value));
64     }
65
66     /**
67      * Monitor a deletion on a context item.
68      *
69      * @param albumKey The item album
70      * @param schemaKey The item schema
71      * @param name The name of the item
72      * @param value The value of the item
73      * @param userArtifactStack the keys of the artifacts using the context map at the moment
74      */
75     public void monitorDelete(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
76             final Object value, final AxConcept[] userArtifactStack) {
77         LOGGER.trace(monitor("DEL", userArtifactStack, albumKey, schemaKey, name, value));
78     }
79
80     /**
81      * Monitor get on a context item.
82      *
83      * @param albumKey The item album
84      * @param schemaKey The item schema
85      * @param name The name of the item
86      * @param value The value of the item
87      * @param userArtifactStack the keys of the artifacts using the context map at the moment
88      */
89     public void monitorGet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
90             final Object value, final AxConcept[] userArtifactStack) {
91         LOGGER.trace(monitor("GET", userArtifactStack, albumKey, schemaKey, name, value));
92     }
93
94     /**
95      * Monitor set on a context item.
96      *
97      * @param albumKey The item album
98      * @param schemaKey The item schema
99      * @param name The name of the item
100      * @param value The value of the item
101      * @param userArtifactStack the keys of the artifacts using the context map at the moment
102      */
103     public void monitorSet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
104             final Object value, final AxConcept[] userArtifactStack) {
105         LOGGER.trace(monitor("SET", userArtifactStack, albumKey, schemaKey, name, value));
106     }
107
108     /**
109      * Monitor a read lock on a key.
110      *
111      * @param albumKey The item album
112      * @param schemaKey The item schema
113      * @param name The name of the item
114      * @param userArtifactStack the keys of the artifacts using the context map at the moment
115      */
116     public void monitorReadLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
117             final AxConcept[] userArtifactStack) {
118         LOGGER.trace(monitor("READLOCK", userArtifactStack, albumKey, schemaKey, name, null));
119     }
120
121     /**
122      * Monitor a write lock on a key.
123      *
124      * @param albumKey The item album
125      * @param schemaKey The item schema
126      * @param name The name of the item
127      * @param userArtifactStack the keys of the artifacts using the context map at the moment
128      */
129     public void monitorWriteLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
130             final AxConcept[] userArtifactStack) {
131         LOGGER.trace(monitor("WRITELOCK", userArtifactStack, albumKey, schemaKey, name, null));
132     }
133
134     /**
135      * Monitor a read unlock on a key.
136      *
137      * @param albumKey The item album
138      * @param schemaKey The item schema
139      * @param name The name of the item
140      * @param userArtifactStack the keys of the artifacts using the context map at the moment
141      */
142     public void monitorReadUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
143             final AxConcept[] userArtifactStack) {
144         LOGGER.trace(monitor("READUNLOCK", userArtifactStack, albumKey, schemaKey, name, null));
145     }
146
147     /**
148      * Monitor a write unlock on a key.
149      *
150      * @param albumKey The item album
151      * @param schemaKey The item schema
152      * @param name The name of the item
153      * @param userArtifactStack the keys of the artifacts using the context map at the moment
154      */
155     public void monitorWriteUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name,
156             final AxConcept[] userArtifactStack) {
157         LOGGER.trace(monitor("WRITEUNLOCK", userArtifactStack, albumKey, schemaKey, name, null));
158     }
159
160     /**
161      * Monitor the user artifact stack.
162      *
163      * @param preamble the preamble
164      * @param userArtifactStack The user stack to print
165      * @param albumKey the album key
166      * @param schemaKey the schema key
167      * @param name the name
168      * @param value the value
169      * @return the string
170      */
171     private String monitor(final String preamble, final AxConcept[] userArtifactStack, final AxArtifactKey albumKey,
172             final AxArtifactKey schemaKey, final String name, final Object value) {
173         final StringBuilder builder = new StringBuilder();
174
175         builder.append(preamble);
176         builder.append(",[");
177
178         if (userArtifactStack != null) {
179             boolean first = true;
180             for (final AxConcept stackKey : userArtifactStack) {
181                 if (first) {
182                     first = false;
183                 } else {
184                     builder.append(',');
185                 }
186                 if (stackKey instanceof AxArtifactKey) {
187                     builder.append(((AxArtifactKey) stackKey).getID());
188                 } else if (stackKey instanceof AxReferenceKey) {
189                     builder.append(((AxReferenceKey) stackKey).getID());
190                 } else {
191                     builder.append(stackKey.toString());
192                 }
193             }
194         }
195         builder.append("],");
196
197         builder.append(albumKey.getID());
198         builder.append(',');
199         builder.append(schemaKey.getID());
200         builder.append(',');
201         builder.append(name);
202
203         if (value != null) {
204             builder.append(',');
205             builder.append(value.toString());
206         }
207
208         return builder.toString();
209     }
210 }