blob: d860b9c0887f82cab9e63689c9d631146a0cc580 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.core.impl;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Reference;
21import org.apache.felix.scr.annotations.ReferenceCardinality;
22import org.apache.felix.scr.annotations.Service;
Murat Parlakisik553db172015-04-08 03:29:04 -070023import org.apache.felix.scr.annotations.Property;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Modified;
26import org.onlab.util.SharedExecutors;
Simon Hunt9fec43f2015-03-25 11:36:30 -070027import org.onlab.util.Tools;
Murat Parlakisik553db172015-04-08 03:29:04 -070028import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.ApplicationIdStore;
31import org.onosproject.core.CoreService;
32import org.onosproject.core.IdBlockStore;
33import org.onosproject.core.IdGenerator;
34import org.onosproject.core.Version;
Murat Parlakisik553db172015-04-08 03:29:04 -070035import org.osgi.service.component.ComponentContext;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070038import java.io.File;
Murat Parlakisik553db172015-04-08 03:29:04 -070039import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070040import java.util.List;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070044import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070045
46/**
47 * Core service implementation.
48 */
Brian O'Connor520c0522014-11-23 23:50:47 -080049@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070050@Service
51public class CoreManager implements CoreService {
52
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070055 private static final File VERSION_FILE = new File("../VERSION");
Simon Hunt9fec43f2015-03-25 11:36:30 -070056 private static Version version = Version.version("1.2.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ApplicationIdStore applicationIdStore;
60
Brian O'Connor520c0522014-11-23 23:50:47 -080061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected IdBlockStore idBlockStore;
63
Murat Parlakisik553db172015-04-08 03:29:04 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ComponentConfigService cfgService;
66
67 @Property(name = "sharedThreadPoolSize", intValue = SharedExecutors.DEFAULT_THREAD_SIZE,
68 label = "Configure shared pool maximum size ")
69 private int sharedThreadPoolSize = SharedExecutors.DEFAULT_THREAD_SIZE;
70
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070071 @Activate
72 public void activate() {
Murat Parlakisik553db172015-04-08 03:29:04 -070073 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070074 List<String> versionLines = Tools.slurp(VERSION_FILE);
75 if (versionLines != null && !versionLines.isEmpty()) {
76 version = Version.version(versionLines.get(0));
77 }
78 }
79
Murat Parlakisik553db172015-04-08 03:29:04 -070080 @Deactivate
81 public void deactivate() {
82 cfgService.unregisterProperties(getClass(), false);
83 }
84
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070085 @Override
86 public Version version() {
87 return version;
88 }
89
90 @Override
91 public Set<ApplicationId> getAppIds() {
92 return applicationIdStore.getAppIds();
93 }
94
95 @Override
96 public ApplicationId getAppId(Short id) {
97 return applicationIdStore.getAppId(id);
98 }
99
100 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800101 public ApplicationId getAppId(String name) {
102 return applicationIdStore.getAppId(name);
103 }
104
105
106 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700107 public ApplicationId registerApplication(String name) {
108 checkNotNull(name, "Application ID cannot be null");
109 return applicationIdStore.registerApplication(name);
110 }
111
Brian O'Connor520c0522014-11-23 23:50:47 -0800112 @Override
113 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800114 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
115 return new BlockAllocatorBasedIdGenerator(allocator);
116 }
117
Murat Parlakisik553db172015-04-08 03:29:04 -0700118
119 @Modified
120 public void modified(ComponentContext context) {
121 Dictionary<?, ?> properties = context.getProperties();
122 Integer sharedThreadPoolSizeConfig =
123 getIntegerProperty(properties, "sharedThreadPoolSize");
124 if (sharedThreadPoolSizeConfig == null) {
125 log.info("Shared Pool Size is not configured, default value is {}",
126 sharedThreadPoolSize);
127 } else {
128 if (sharedThreadPoolSizeConfig > 0) {
129 sharedThreadPoolSize = sharedThreadPoolSizeConfig;
130 SharedExecutors.setPoolSize(sharedThreadPoolSize);
131 log.info("Configured. Shared Pool Size is configured to {}",
132 sharedThreadPoolSize);
133 } else {
134 log.warn("Shared Pool Size size must be greater than 0");
135 }
136 }
137 }
138
139
140
141 /**
142 * Get Integer property from the propertyName
143 * Return null if propertyName is not found.
144 *
145 * @param properties properties to be looked up
146 * @param propertyName the name of the property to look up
147 * @return value when the propertyName is defined or return null
148 */
149 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
150 String propertyName) {
151 Integer value = null;
152 try {
153 String s = (String) properties.get(propertyName);
154 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
155 } catch (NumberFormatException | ClassCastException e) {
156 value = null;
157 }
158 return value;
159 }
160
161
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700162}