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