blob: 375de99aca7d3d8390bab3bbbb87f8f6c40be733 [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
Thomas Vachuskab0317c62015-04-08 23:58:58 -070067 @Property(name = "sharedThreadPoolSize", intValue = SharedExecutors.DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070068 label = "Configure shared pool maximum size ")
Thomas Vachuskab0317c62015-04-08 23:58:58 -070069 private int sharedThreadPoolSize = SharedExecutors.DEFAULT_POOL_SIZE;
Murat Parlakisik553db172015-04-08 03:29:04 -070070
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);
Thomas Vachuskab0317c62015-04-08 23:58:58 -070083 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -070084 }
85
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070086 @Override
87 public Version version() {
88 return version;
89 }
90
91 @Override
92 public Set<ApplicationId> getAppIds() {
93 return applicationIdStore.getAppIds();
94 }
95
96 @Override
97 public ApplicationId getAppId(Short id) {
98 return applicationIdStore.getAppId(id);
99 }
100
101 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800102 public ApplicationId getAppId(String name) {
103 return applicationIdStore.getAppId(name);
104 }
105
106
107 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700108 public ApplicationId registerApplication(String name) {
109 checkNotNull(name, "Application ID cannot be null");
110 return applicationIdStore.registerApplication(name);
111 }
112
Brian O'Connor520c0522014-11-23 23:50:47 -0800113 @Override
114 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800115 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
116 return new BlockAllocatorBasedIdGenerator(allocator);
117 }
118
Murat Parlakisik553db172015-04-08 03:29:04 -0700119
120 @Modified
121 public void modified(ComponentContext context) {
122 Dictionary<?, ?> properties = context.getProperties();
123 Integer sharedThreadPoolSizeConfig =
124 getIntegerProperty(properties, "sharedThreadPoolSize");
125 if (sharedThreadPoolSizeConfig == null) {
126 log.info("Shared Pool Size is not configured, default value is {}",
127 sharedThreadPoolSize);
128 } else {
129 if (sharedThreadPoolSizeConfig > 0) {
130 sharedThreadPoolSize = sharedThreadPoolSizeConfig;
131 SharedExecutors.setPoolSize(sharedThreadPoolSize);
132 log.info("Configured. Shared Pool Size is configured to {}",
133 sharedThreadPoolSize);
134 } else {
135 log.warn("Shared Pool Size size must be greater than 0");
136 }
137 }
138 }
139
140
141
142 /**
143 * Get Integer property from the propertyName
144 * Return null if propertyName is not found.
145 *
146 * @param properties properties to be looked up
147 * @param propertyName the name of the property to look up
148 * @return value when the propertyName is defined or return null
149 */
150 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
151 String propertyName) {
152 Integer value = null;
153 try {
154 String s = (String) properties.get(propertyName);
155 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
156 } catch (NumberFormatException | ClassCastException e) {
157 value = null;
158 }
159 return value;
160 }
161
162
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700163}