arjunek1992 | f6353d4 | 2018-11-20 08:56:29 -0500 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.audit.impl; |
| 18 | |
| 19 | import org.onlab.rest.AuditFilter; |
| 20 | |
| 21 | import org.onosproject.cfg.ComponentConfigService; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.osgi.service.component.ComponentContext; |
| 24 | import org.osgi.service.component.annotations.Component; |
| 25 | import org.osgi.service.component.annotations.Modified; |
| 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Reference; |
| 28 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 29 | |
| 30 | import java.util.Dictionary; |
| 31 | |
| 32 | import static org.onlab.util.Tools.get; |
| 33 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DESC; |
| 34 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DEFAULT; |
| 35 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_STATUS_DESC; |
| 36 | import 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 | }) |
| 48 | public 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 | } |