Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 1 | /* |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 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 | package org.onosproject.store.app; |
| 17 | |
Yuta HIGUCHI | 8029205 | 2015-02-10 23:11:59 -0800 | [diff] [blame] | 18 | import com.google.common.base.Charsets; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Preconditions; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Multimap; |
| 25 | import com.google.common.collect.Sets; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 26 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 33 | import org.onosproject.app.ApplicationDescription; |
| 34 | import org.onosproject.app.ApplicationEvent; |
| 35 | import org.onosproject.app.ApplicationException; |
| 36 | import org.onosproject.app.ApplicationState; |
| 37 | import org.onosproject.app.ApplicationStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 38 | import org.onosproject.app.ApplicationStoreDelegate; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 39 | import org.onosproject.cluster.ClusterService; |
| 40 | import org.onosproject.cluster.ControllerNode; |
| 41 | import org.onosproject.common.app.ApplicationArchive; |
| 42 | import org.onosproject.core.Application; |
| 43 | import org.onosproject.core.ApplicationId; |
| 44 | import org.onosproject.core.ApplicationIdStore; |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 45 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 46 | import org.onosproject.core.DefaultApplication; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 47 | import org.onosproject.security.Permission; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 48 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 50 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 51 | import org.onosproject.store.service.AtomicValue; |
| 52 | import org.onosproject.store.service.AtomicValueEvent; |
| 53 | import org.onosproject.store.service.AtomicValueEventListener; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.ConsistentMap; |
| 55 | import org.onosproject.store.service.MapEvent; |
| 56 | import org.onosproject.store.service.MapEventListener; |
| 57 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 58 | import org.onosproject.store.service.StorageException; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 59 | import org.onosproject.store.service.StorageService; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 60 | import org.onosproject.store.service.Versioned; |
| 61 | import org.onosproject.store.service.DistributedPrimitive.Status; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
| 64 | import java.io.ByteArrayInputStream; |
Madan Jampani | 01e05fb | 2015-08-13 13:29:36 -0700 | [diff] [blame] | 65 | import java.io.IOException; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 66 | import java.io.InputStream; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 67 | import java.util.List; |
| 68 | import java.util.Optional; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 69 | import java.util.Set; |
| 70 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 71 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 72 | import java.util.concurrent.Executors; |
| 73 | import java.util.concurrent.ScheduledExecutorService; |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 74 | import java.util.concurrent.TimeUnit; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 75 | import java.util.concurrent.atomic.AtomicBoolean; |
| 76 | import java.util.function.Consumer; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 77 | import java.util.function.Function; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 78 | import java.util.stream.Collectors; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 79 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 80 | import static com.google.common.collect.Multimaps.newSetMultimap; |
| 81 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 82 | import static com.google.common.io.ByteStreams.toByteArray; |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 83 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 84 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 85 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 86 | import static org.onlab.util.Tools.randomDelay; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 87 | import static org.onosproject.app.ApplicationEvent.Type.*; |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 88 | import static org.onosproject.store.app.DistributedApplicationStore.InternalState.*; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 89 | import static org.slf4j.LoggerFactory.getLogger; |
| 90 | |
| 91 | /** |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 92 | * Manages inventory of applications in a distributed data store providing |
| 93 | * stronger consistency guarantees. |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 94 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 95 | @Component(immediate = true) |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 96 | @Service |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 97 | public class DistributedApplicationStore extends ApplicationArchive |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 98 | implements ApplicationStore { |
| 99 | |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 100 | // FIXME: eliminate the need for this |
| 101 | private static final int FIXME_ACTIVATION_DELAY = 500; |
| 102 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 103 | private final Logger log = getLogger(getClass()); |
| 104 | |
| 105 | private static final MessageSubject APP_BITS_REQUEST = new MessageSubject("app-bits-request"); |
| 106 | |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 107 | private static final int MAX_LOAD_RETRIES = 5; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 108 | private static final int RETRY_DELAY_MS = 2_000; |
| 109 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 110 | private static final int FETCH_TIMEOUT_MS = 10_000; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 111 | |
Jonathan Hart | d6fb053 | 2016-01-21 17:28:20 -0800 | [diff] [blame] | 112 | private static final int APP_LOAD_DELAY_MS = 500; |
| 113 | |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 114 | private static List<String> pendingApps = Lists.newArrayList(); |
| 115 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 116 | public enum InternalState { |
| 117 | INSTALLED, ACTIVATED, DEACTIVATED |
| 118 | } |
| 119 | |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 120 | private ScheduledExecutorService executor; |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 121 | private ExecutorService messageHandlingExecutor; |
| 122 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 123 | private ConsistentMap<ApplicationId, InternalApplicationHolder> apps; |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 124 | private AtomicValue<Application> nextAppToActivate; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected ClusterCommunicationService clusterCommunicator; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected ClusterService clusterService; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 133 | protected StorageService storageService; |
| 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 136 | protected ApplicationIdStore idStore; |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 137 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 138 | private final InternalAppsListener appsListener = new InternalAppsListener(); |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 139 | private final NextAppToActivateValueListener nextAppToActivateListener = new NextAppToActivateValueListener(); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 140 | |
| 141 | private Consumer<Status> statusChangeListener; |
| 142 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 143 | // Multimap to track which apps are required by others apps |
| 144 | // app -> { required-by, ... } |
| 145 | // Apps explicitly activated will be required by the CORE app |
| 146 | private final Multimap<ApplicationId, ApplicationId> requiredBy = |
| 147 | synchronizedSetMultimap(newSetMultimap(Maps.newHashMap(), Sets::newHashSet)); |
| 148 | |
| 149 | private ApplicationId coreAppId; |
| 150 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 151 | @Activate |
| 152 | public void activate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 153 | messageHandlingExecutor = Executors.newSingleThreadExecutor( |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 154 | groupedThreads("onos/store/app", "message-handler", log)); |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 155 | clusterCommunicator.addSubscriber(APP_BITS_REQUEST, |
| 156 | bytes -> new String(bytes, Charsets.UTF_8), |
| 157 | name -> { |
| 158 | try { |
| 159 | return toByteArray(getApplicationInputStream(name)); |
| 160 | } catch (IOException e) { |
| 161 | throw new StorageException(e); |
| 162 | } |
| 163 | }, |
| 164 | Function.identity(), |
| 165 | messageHandlingExecutor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 166 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 167 | apps = storageService.<ApplicationId, InternalApplicationHolder>consistentMapBuilder() |
| 168 | .withName("onos-apps") |
| 169 | .withRelaxedReadConsistency() |
| 170 | .withSerializer(Serializer.using(KryoNamespaces.API, |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 171 | InternalApplicationHolder.class, |
| 172 | InternalState.class)) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 173 | .build(); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 174 | |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 175 | nextAppToActivate = storageService.<Application>atomicValueBuilder() |
| 176 | .withName("onos-apps-activation-order-value") |
| 177 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 178 | .build() |
| 179 | .asAtomicValue(); |
| 180 | |
| 181 | nextAppToActivate.addListener(nextAppToActivateListener); |
| 182 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 183 | executor = newSingleThreadScheduledExecutor(groupedThreads("onos/app", "store", log)); |
| 184 | statusChangeListener = status -> { |
| 185 | if (status == Status.ACTIVE) { |
| 186 | executor.execute(this::bootstrapExistingApplications); |
| 187 | } |
| 188 | }; |
| 189 | apps.addListener(appsListener, messageHandlingExecutor); |
| 190 | apps.addStatusChangeListener(statusChangeListener); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 191 | coreAppId = getId(CoreService.CORE_APP_NAME); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 192 | log.info("Started"); |
| 193 | } |
| 194 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 195 | /** |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 196 | * Processes existing applications from the distributed map. This is done to |
| 197 | * account for events that this instance may be have missed due to a staggered start. |
| 198 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 199 | private void bootstrapExistingApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 200 | apps.asJavaMap().forEach((appId, holder) -> setupApplicationAndNotify(appId, holder.app(), holder.state())); |
| 201 | |
| 202 | } |
| 203 | |
| 204 | /** |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 205 | * Loads the application inventory from the disk and activates apps if |
| 206 | * they are marked to be active. |
| 207 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 208 | private void loadFromDisk() { |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 209 | getApplicationNames().forEach(appName -> { |
| 210 | Application app = loadFromDisk(appName); |
| 211 | if (app != null && isActive(app.id().name())) { |
| 212 | activate(app.id(), false); |
| 213 | // TODO Load app permissions |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | private Application loadFromDisk(String appName) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 219 | pendingApps.add(appName); |
| 220 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 221 | for (int i = 0; i < MAX_LOAD_RETRIES; i++) { |
| 222 | try { |
| 223 | // Directly return if app already exists |
| 224 | ApplicationId appId = getId(appName); |
| 225 | if (appId != null) { |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 226 | Application application = getApplication(appId); |
| 227 | if (application != null) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 228 | pendingApps.remove(appName); |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 229 | return application; |
| 230 | } |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 231 | } |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 232 | |
| 233 | ApplicationDescription appDesc = getApplicationDescription(appName); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 234 | |
| 235 | Optional<String> loop = appDesc.requiredApps().stream() |
| 236 | .filter(app -> pendingApps.contains(app)).findAny(); |
| 237 | if (loop.isPresent()) { |
| 238 | log.error("Circular app dependency detected: {} -> {}", pendingApps, loop.get()); |
| 239 | pendingApps.remove(appName); |
| 240 | return null; |
| 241 | } |
| 242 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 243 | boolean success = appDesc.requiredApps().stream() |
| 244 | .noneMatch(requiredApp -> loadFromDisk(requiredApp) == null); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 245 | pendingApps.remove(appName); |
| 246 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 247 | return success ? create(appDesc, false) : null; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 248 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 249 | } catch (Exception e) { |
| 250 | log.warn("Unable to load application {} from disk; retrying", appName); |
| 251 | randomDelay(RETRY_DELAY_MS); //FIXME: This is a deliberate hack; fix in Falcon |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 252 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 253 | } |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 254 | pendingApps.remove(appName); |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 255 | return null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Deactivate |
| 259 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 260 | clusterCommunicator.removeSubscriber(APP_BITS_REQUEST); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 261 | apps.removeStatusChangeListener(statusChangeListener); |
| 262 | apps.removeListener(appsListener); |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 263 | nextAppToActivate.removeListener(nextAppToActivateListener); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 264 | messageHandlingExecutor.shutdown(); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 265 | executor.shutdown(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 266 | log.info("Stopped"); |
| 267 | } |
| 268 | |
| 269 | @Override |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 270 | public void setDelegate(ApplicationStoreDelegate delegate) { |
| 271 | super.setDelegate(delegate); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 272 | executor.execute(this::bootstrapExistingApplications); |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 273 | executor.schedule((Runnable) this::loadFromDisk, APP_LOAD_DELAY_MS, TimeUnit.MILLISECONDS); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 277 | public Set<Application> getApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 278 | return ImmutableSet.copyOf(apps.values() |
| 279 | .stream() |
| 280 | .map(Versioned::value) |
| 281 | .map(InternalApplicationHolder::app) |
| 282 | .collect(Collectors.toSet())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public ApplicationId getId(String name) { |
| 287 | return idStore.getAppId(name); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public Application getApplication(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 292 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 293 | return appHolder != null ? appHolder.app() : null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public ApplicationState getState(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 298 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 299 | InternalState state = appHolder != null ? appHolder.state() : null; |
| 300 | return state == null ? null : state == ACTIVATED ? ApplicationState.ACTIVE : ApplicationState.INSTALLED; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public Application create(InputStream appDescStream) { |
| 305 | ApplicationDescription appDesc = saveApplication(appDescStream); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 306 | if (hasPrerequisites(appDesc)) { |
| 307 | return create(appDesc, true); |
| 308 | } |
Jonathan Hart | 14651b5 | 2016-06-07 17:59:53 -0700 | [diff] [blame] | 309 | // Purge bits off disk if we don't have prerequisites to allow app to be |
| 310 | // reinstalled later |
| 311 | purgeApplication(appDesc.name()); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 312 | throw new ApplicationException("Missing dependencies for app " + appDesc.name()); |
| 313 | } |
| 314 | |
| 315 | private boolean hasPrerequisites(ApplicationDescription app) { |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 316 | return !app.requiredApps().stream().map(this::getId) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 317 | .anyMatch(id -> id == null || getApplication(id) == null); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 320 | private Application create(ApplicationDescription appDesc, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 321 | Application app = registerApp(appDesc); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 322 | if (updateTime) { |
| 323 | updateTime(app.id().name()); |
| 324 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 325 | InternalApplicationHolder previousApp = |
| 326 | Versioned.valueOrNull(apps.putIfAbsent(app.id(), new InternalApplicationHolder(app, INSTALLED, null))); |
| 327 | return previousApp != null ? previousApp.app() : app; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void remove(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 332 | uninstallDependentApps(appId); |
| 333 | apps.remove(appId); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 336 | // Uninstalls all apps that depend on the given app. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 337 | private void uninstallDependentApps(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 338 | getApplications().stream() |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 339 | .filter(a -> a.requiredApps().contains(appId.name())) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 340 | .forEach(a -> remove(a.id())); |
| 341 | } |
| 342 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 343 | @Override |
| 344 | public void activate(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 345 | activate(appId, coreAppId); |
| 346 | } |
| 347 | |
| 348 | private void activate(ApplicationId appId, ApplicationId forAppId) { |
| 349 | requiredBy.put(appId, forAppId); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 350 | activate(appId, true); |
| 351 | } |
| 352 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 353 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 354 | private void activate(ApplicationId appId, boolean updateTime) { |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 355 | Versioned<InternalApplicationHolder> vAppHolder = apps.get(appId); |
| 356 | if (vAppHolder != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 357 | if (updateTime) { |
| 358 | updateTime(appId.name()); |
| 359 | } |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 360 | activateRequiredApps(vAppHolder.value().app()); |
| 361 | |
| 362 | apps.computeIf(appId, v -> v != null && v.state() != ACTIVATED, |
| 363 | (k, v) -> new InternalApplicationHolder( |
| 364 | v.app(), ACTIVATED, v.permissions())); |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 365 | nextAppToActivate.set(vAppHolder.value().app()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 369 | // Activates all apps required by this application. |
| 370 | private void activateRequiredApps(Application app) { |
| 371 | app.requiredApps().stream().map(this::getId).forEach(id -> activate(id, app.id())); |
| 372 | } |
| 373 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 374 | @Override |
| 375 | public void deactivate(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 376 | deactivateDependentApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 377 | deactivate(appId, coreAppId); |
| 378 | } |
| 379 | |
| 380 | private void deactivate(ApplicationId appId, ApplicationId forAppId) { |
| 381 | requiredBy.remove(appId, forAppId); |
| 382 | if (requiredBy.get(appId).isEmpty()) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 383 | AtomicBoolean stateChanged = new AtomicBoolean(false); |
| 384 | apps.computeIf(appId, |
| 385 | v -> v != null && v.state() != DEACTIVATED, |
| 386 | (k, v) -> { |
| 387 | stateChanged.set(true); |
| 388 | return new InternalApplicationHolder(v.app(), DEACTIVATED, v.permissions()); |
| 389 | }); |
| 390 | if (stateChanged.get()) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 391 | updateTime(appId.name()); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 392 | deactivateRequiredApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 393 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 397 | // Deactivates all apps that require this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 398 | private void deactivateDependentApps(ApplicationId appId) { |
| 399 | apps.values() |
| 400 | .stream() |
| 401 | .map(Versioned::value) |
| 402 | .filter(a -> a.state() == ACTIVATED) |
| 403 | .filter(a -> a.app().requiredApps().contains(appId.name())) |
| 404 | .forEach(a -> deactivate(a.app().id())); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // Deactivates all apps required by this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 408 | private void deactivateRequiredApps(ApplicationId appId) { |
| 409 | getApplication(appId).requiredApps() |
| 410 | .stream() |
| 411 | .map(this::getId) |
| 412 | .map(apps::get) |
| 413 | .map(Versioned::value) |
| 414 | .filter(a -> a.state() == ACTIVATED) |
| 415 | .forEach(a -> deactivate(a.app().id(), appId)); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 418 | @Override |
| 419 | public Set<Permission> getPermissions(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 420 | InternalApplicationHolder app = Versioned.valueOrNull(apps.get(appId)); |
| 421 | return app != null ? ImmutableSet.copyOf(app.permissions()) : ImmutableSet.of(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 426 | AtomicBoolean permissionsChanged = new AtomicBoolean(false); |
| 427 | Versioned<InternalApplicationHolder> appHolder = apps.computeIf(appId, |
| 428 | v -> v != null && !Sets.symmetricDifference(v.permissions(), permissions).isEmpty(), |
| 429 | (k, v) -> { |
| 430 | permissionsChanged.set(true); |
| 431 | return new InternalApplicationHolder(v.app(), v.state(), ImmutableSet.copyOf(permissions)); |
| 432 | }); |
| 433 | if (permissionsChanged.get()) { |
| 434 | delegate.notify(new ApplicationEvent(APP_PERMISSIONS_CHANGED, appHolder.value().app())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 438 | private class NextAppToActivateValueListener implements AtomicValueEventListener<Application> { |
| 439 | |
| 440 | @Override |
| 441 | public void event(AtomicValueEvent<Application> event) { |
| 442 | messageHandlingExecutor.execute(() -> { |
| 443 | Application app = event.newValue(); |
| 444 | String appName = app.id().name(); |
| 445 | installAppIfNeeded(app); |
| 446 | setActive(appName); |
| 447 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 448 | }); |
| 449 | } |
| 450 | } |
| 451 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 452 | /** |
| 453 | * Listener to application state distributed map changes. |
| 454 | */ |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 455 | private final class InternalAppsListener |
| 456 | implements MapEventListener<ApplicationId, InternalApplicationHolder> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 457 | @Override |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 458 | public void event(MapEvent<ApplicationId, InternalApplicationHolder> event) { |
Thomas Vachuska | 4fcdae7 | 2015-03-24 10:22:54 -0700 | [diff] [blame] | 459 | if (delegate == null) { |
| 460 | return; |
| 461 | } |
| 462 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 463 | ApplicationId appId = event.key(); |
| 464 | InternalApplicationHolder newApp = event.newValue() == null ? null : event.newValue().value(); |
| 465 | InternalApplicationHolder oldApp = event.oldValue() == null ? null : event.oldValue().value(); |
| 466 | if (event.type() == MapEvent.Type.INSERT || event.type() == MapEvent.Type.UPDATE) { |
| 467 | if (event.type() == MapEvent.Type.UPDATE && newApp.state() == oldApp.state()) { |
| 468 | return; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 469 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 470 | setupApplicationAndNotify(appId, newApp.app(), newApp.state()); |
| 471 | } else if (event.type() == MapEvent.Type.REMOVE) { |
| 472 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, oldApp.app())); |
| 473 | purgeApplication(appId.name()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 478 | private void setupApplicationAndNotify(ApplicationId appId, Application app, InternalState state) { |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame^] | 479 | // ACTIVATED state is handled separately in NextAppToActivateValueListener |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 480 | if (state == INSTALLED) { |
| 481 | fetchBitsIfNeeded(app); |
| 482 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 483 | } else if (state == DEACTIVATED) { |
| 484 | clearActive(appId.name()); |
| 485 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 486 | } |
| 487 | } |
| 488 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 489 | /** |
| 490 | * Determines if the application bits are available locally. |
| 491 | */ |
| 492 | private boolean appBitsAvailable(Application app) { |
| 493 | try { |
| 494 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 495 | return appDesc.version().equals(app.version()); |
| 496 | } catch (ApplicationException e) { |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Fetches the bits from the cluster peers if necessary. |
| 503 | */ |
| 504 | private void fetchBitsIfNeeded(Application app) { |
| 505 | if (!appBitsAvailable(app)) { |
| 506 | fetchBits(app); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Installs the application if necessary from the application peers. |
| 512 | */ |
| 513 | private void installAppIfNeeded(Application app) { |
| 514 | if (!appBitsAvailable(app)) { |
| 515 | fetchBits(app); |
| 516 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Fetches the bits from the cluster peers. |
| 522 | */ |
| 523 | private void fetchBits(Application app) { |
| 524 | ControllerNode localNode = clusterService.getLocalNode(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 525 | CountDownLatch latch = new CountDownLatch(1); |
| 526 | |
| 527 | // FIXME: send message with name & version to make sure we don't get served old bits |
| 528 | |
| 529 | log.info("Downloading bits for application {}", app.id().name()); |
| 530 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 531 | if (latch.getCount() == 0) { |
| 532 | break; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 533 | } |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 534 | if (node.equals(localNode)) { |
| 535 | continue; |
| 536 | } |
| 537 | clusterCommunicator.sendAndReceive(app.id().name(), |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 538 | APP_BITS_REQUEST, |
| 539 | s -> s.getBytes(Charsets.UTF_8), |
| 540 | Function.identity(), |
| 541 | node.id()) |
| 542 | .whenCompleteAsync((bits, error) -> { |
| 543 | if (error == null && latch.getCount() > 0) { |
| 544 | saveApplication(new ByteArrayInputStream(bits)); |
| 545 | log.info("Downloaded bits for application {} from node {}", |
| 546 | app.id().name(), node.id()); |
| 547 | latch.countDown(); |
| 548 | } else if (error != null) { |
| 549 | log.warn("Unable to fetch bits for application {} from node {}", |
| 550 | app.id().name(), node.id()); |
| 551 | } |
| 552 | }, executor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | try { |
| 556 | if (!latch.await(FETCH_TIMEOUT_MS, MILLISECONDS)) { |
| 557 | log.warn("Unable to fetch bits for application {}", app.id().name()); |
| 558 | } |
| 559 | } catch (InterruptedException e) { |
| 560 | log.warn("Interrupted while fetching bits for application {}", app.id().name()); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /** |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 565 | * Produces a registered application from the supplied description. |
| 566 | */ |
| 567 | private Application registerApp(ApplicationDescription appDesc) { |
| 568 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 569 | return new DefaultApplication(appId, |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 570 | appDesc.version(), |
| 571 | appDesc.title(), |
| 572 | appDesc.description(), |
| 573 | appDesc.origin(), |
| 574 | appDesc.category(), |
| 575 | appDesc.url(), |
| 576 | appDesc.readme(), |
| 577 | appDesc.icon(), |
| 578 | appDesc.role(), |
| 579 | appDesc.permissions(), |
| 580 | appDesc.featuresRepo(), |
| 581 | appDesc.features(), |
| 582 | appDesc.requiredApps()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 583 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * Internal class for holding app information. |
| 587 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 588 | private static final class InternalApplicationHolder { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 589 | private final Application app; |
| 590 | private final InternalState state; |
| 591 | private final Set<Permission> permissions; |
| 592 | |
| 593 | @SuppressWarnings("unused") |
| 594 | private InternalApplicationHolder() { |
| 595 | app = null; |
| 596 | state = null; |
| 597 | permissions = null; |
| 598 | } |
| 599 | |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 600 | private InternalApplicationHolder(Application app, InternalState state, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 601 | this.app = Preconditions.checkNotNull(app); |
| 602 | this.state = state; |
| 603 | this.permissions = permissions == null ? null : ImmutableSet.copyOf(permissions); |
| 604 | } |
| 605 | |
| 606 | public Application app() { |
| 607 | return app; |
| 608 | } |
| 609 | |
| 610 | public InternalState state() { |
| 611 | return state; |
| 612 | } |
| 613 | |
| 614 | public Set<Permission> permissions() { |
| 615 | return permissions; |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public String toString() { |
| 620 | return MoreObjects.toStringHelper(getClass()) |
| 621 | .add("app", app.id()) |
| 622 | .add("state", state) |
| 623 | .toString(); |
| 624 | } |
| 625 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 626 | } |