blob: 34b4e78a7120effa1de8f5d96b0f6e06149eef01 [file] [log] [blame]
Changhoon Yoonb856b812015-08-10 03:47:19 +09001/*
2 * Copyright 2015 Open Networking Laboratory
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.security;
18
19import org.onlab.osgi.DefaultServiceDirectory;
20import org.onlab.osgi.ServiceDirectory;
21import org.onlab.osgi.ServiceNotFoundException;
22import org.onosproject.core.ApplicationId;
23
24/**
25 * Utility class to aid Security-Mode ONOS.
26 */
27public final class SecurityUtil {
28
29 protected static ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
30
31 private SecurityUtil() {
32 }
33
34 public static boolean isSecurityModeEnabled() {
35 if (System.getSecurityManager() != null) {
36 try {
37 SecurityAdminService securityService = serviceDirectory.get(SecurityAdminService.class);
38 if (securityService != null) {
39 return true;
40 }
41 } catch (ServiceNotFoundException e) {
42 return false;
43 }
44 }
45 return false;
46 }
47
48 public static SecurityAdminService getSecurityService() {
49 if (System.getSecurityManager() != null) {
50 try {
51 SecurityAdminService securityService = serviceDirectory.get(SecurityAdminService.class);
52 if (securityService != null) {
53 return securityService;
54 }
55 } catch (ServiceNotFoundException e) {
56 return null;
57 }
58 }
59 return null;
60 }
61
62 public static boolean isAppSecured(ApplicationId appId) {
63 SecurityAdminService service = getSecurityService();
64 if (service != null) {
65 if (!service.isSecured(appId)) {
66 System.out.println("\n*******************************");
67 System.out.println(" SM-ONOS APP WARNING ");
68 System.out.println("*******************************");
69 System.out.println(appId.name() + " has not been secured.");
70 System.out.println("Please review before activating.");
71 return false;
72 }
73 }
74 return true;
75 }
76 public static void register(ApplicationId appId) {
77 SecurityAdminService service = getSecurityService();
78 if (service != null) {
79 service.register(appId);
80 }
81 }
82}