c37323759b42f5e6439b20416ec6df1f8655d79a
[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         String monitorInitString = monitor("INIT", null, albumKey, schemaKey, name, value);
50         LOGGER.trace(monitorInitString);
51     }
52
53     /**
54      * Monitor an initiation on a context item.
55      *
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
61      */
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);
66     }
67
68     /**
69      * Monitor a deletion on a context item.
70      *
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
76      */
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);
81     }
82
83     /**
84      * Monitor get on a context item.
85      *
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
91      */
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);
96     }
97
98     /**
99      * Monitor set on a context item.
100      *
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
106      */
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);
111     }
112
113     /**
114      * Monitor a read lock on a key.
115      *
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
120      */
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);
125     }
126
127     /**
128      * Monitor a write lock on a key.
129      *
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
134      */
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);
139     }
140
141     /**
142      * Monitor a read unlock on a key.
143      *
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
148      */
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);
153     }
154
155     /**
156      * Monitor a write unlock on a key.
157      *
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
162      */
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);
167     }
168
169     /**
170      * Monitor the user artifact stack.
171      *
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
178      * @return the string
179      */
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();
183
184         builder.append(preamble);
185         builder.append(",[");
186
187         if (userArtifactStack != null) {
188             boolean first = true;
189             for (final AxConcept stackKey : userArtifactStack) {
190                 if (first) {
191                     first = false;
192                 } else {
193                     builder.append(',');
194                 }
195                 if (stackKey instanceof AxArtifactKey) {
196                     builder.append(((AxArtifactKey) stackKey).getId());
197                 } else if (stackKey instanceof AxReferenceKey) {
198                     builder.append(((AxReferenceKey) stackKey).getId());
199                 } else {
200                     builder.append(stackKey.toString());
201                 }
202             }
203         }
204         builder.append("],");
205
206         builder.append(albumKey.getId());
207         builder.append(',');
208         builder.append(schemaKey.getId());
209         builder.append(',');
210         builder.append(name);
211
212         if (value != null) {
213             builder.append(',');
214             builder.append(value.toString());
215         }
216
217         return builder.toString();
218     }
219 }