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