blob: 31b6a182387af4709f6927821d396674dc7b43b1 [file] [log] [blame]
arjunek1992f6353d42018-11-20 08:56:29 -05001/*
2 * Copyright 2016-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.audit.impl;
18
19import org.onlab.rest.AuditFilter;
20
21import org.onosproject.cfg.ComponentConfigService;
22import org.onosproject.cli.AbstractShellCommand;
23import org.osgi.service.component.ComponentContext;
24import org.osgi.service.component.annotations.Component;
25import org.osgi.service.component.annotations.Modified;
26import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Reference;
28import org.osgi.service.component.annotations.ReferenceCardinality;
29
30import java.util.Dictionary;
31
32import static org.onlab.util.Tools.get;
33import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DESC;
34import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DEFAULT;
35import static org.onosproject.net.OsgiPropertyConstants.AUDIT_STATUS_DESC;
36import static org.onosproject.net.OsgiPropertyConstants.AUDIT_STATUS_DEFAULT;
37
38
39/**
40 * Component to manage REST API Audit.
41 */
42@Component(
43 immediate = true,
44 property = {
45 AUDIT_FILE_TYPE_DESC + "=" + AUDIT_FILE_TYPE_DEFAULT,
46 AUDIT_STATUS_DESC + ":Boolean=" + AUDIT_STATUS_DEFAULT
47 })
48public class AuditManager {
49
50 public String auditFile = AUDIT_FILE_TYPE_DEFAULT;
51 public boolean auditEnabled = AUDIT_STATUS_DEFAULT;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY)
54 protected ComponentConfigService cfgService;
55
56 @Activate
57 public void activate(ComponentContext context) {
58 cfgService.registerProperties(getClass());
59 setAuditStatus(auditFile, auditEnabled);
60 }
61
62 @Modified
63 protected void modifyFileType(ComponentContext context) {
64 Dictionary<?, ?> properties = context.getProperties();
65 if (properties == null) {
66 return;
67 }
68 auditFile = get(properties, AUDIT_FILE_TYPE_DESC);
69 String enableAuditStr = get(properties, AUDIT_STATUS_DESC);
70
71 auditEnabled = Boolean.parseBoolean(enableAuditStr);
72 setAuditStatus(auditFile, auditEnabled);
73 }
74
75 /**
76 * To enable Audit and set file type for REST API and CLI as per the changes in configuration properties.
77 *
78 * @param auditFile file which audit logs are saved.
79 * @param auditEnabled status of REST API Audit and CLI Audit.
80 */
81 public void setAuditStatus(String auditFile, boolean auditEnabled) {
82 if (auditEnabled) {
83 AuditFilter.enableAudit();
84 AbstractShellCommand.enableAudit();
85 } else {
86 AuditFilter.disableAudit();
87 AbstractShellCommand.disableAudit();
88 }
89 AuditFilter.setAuditFile(auditFile);
90 AbstractShellCommand.setAuditFile(auditFile);
91 }
92}