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 | |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 209 | activateExistingApplications(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 210 | log.info("Started"); |
| 211 | } |
| 212 | |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 213 | /** |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 214 | * 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] | 215 | */ |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 216 | private InternalApplicationHolder convertApplication(InternalApplicationHolder appHolder, Version version) { |
| 217 | // Load the application description from disk. If the version doesn't match the persisted |
| 218 | // version, update the stored application with the new version. |
| 219 | ApplicationDescription appDesc = getApplicationDescription(appHolder.app.id().name()); |
| 220 | if (!appDesc.version().equals(appHolder.app().version())) { |
Jordan Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 221 | Application newApplication = DefaultApplication.builder(appDesc) |
| 222 | .withAppId(appHolder.app.id()) |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 223 | .build(); |
| 224 | return new InternalApplicationHolder( |
| 225 | newApplication, appHolder.state, appHolder.permissions); |
| 226 | } |
| 227 | return appHolder; |
| 228 | } |
| 229 | |
| 230 | /** |
Jordan Halterman | 400bbe5 | 2018-04-05 23:07:47 -0700 | [diff] [blame] | 231 | * Converts the versions of stored applications propagated from the prior version to the local application versions. |
| 232 | */ |
| 233 | private Application convertApplication(Application app, Version version) { |
| 234 | // Load the application description from disk. If the version doesn't match the persisted |
| 235 | // version, update the stored application with the new version. |
| 236 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 237 | if (!appDesc.version().equals(app.version())) { |
| 238 | return DefaultApplication.builder(appDesc) |
| 239 | .withAppId(app.id()) |
| 240 | .build(); |
| 241 | } |
| 242 | return app; |
| 243 | } |
| 244 | |
| 245 | /** |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 246 | * Activates applications that should be activated according to the distributed store. |
| 247 | */ |
| 248 | private void activateExistingApplications() { |
Jon Hall | 8069fdd | 2018-02-02 10:48:20 -0800 | [diff] [blame] | 249 | getApplicationNames().forEach(appName -> { |
| 250 | // Only update the application version if the application has already been installed. |
| 251 | ApplicationId appId = getId(appName); |
| 252 | if (appId != null) { |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 253 | ApplicationDescription appDesc = getApplicationDescription(appName); |
| 254 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
Jordan Halterman | fedad5b | 2017-10-09 23:16:27 -0700 | [diff] [blame] | 255 | |
Jordan Halterman | a84936d | 2018-04-04 15:45:47 -0700 | [diff] [blame] | 256 | // If the application has already been activated, set the local state to active. |
| 257 | if (appHolder != null |
| 258 | && appDesc.version().equals(appHolder.app().version()) |
| 259 | && appHolder.state == ACTIVATED) { |
| 260 | setActive(appName); |
| 261 | updateTime(appName); |
Jordan Halterman | fedad5b | 2017-10-09 23:16:27 -0700 | [diff] [blame] | 262 | } |
Jon Hall | 8069fdd | 2018-02-02 10:48:20 -0800 | [diff] [blame] | 263 | } |
| 264 | }); |
Jordan Halterman | fedad5b | 2017-10-09 23:16:27 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jon Hall | 8069fdd | 2018-02-02 10:48:20 -0800 | [diff] [blame] | 267 | |
Jordan Halterman | fedad5b | 2017-10-09 23:16:27 -0700 | [diff] [blame] | 268 | /** |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 269 | * Processes existing applications from the distributed map. This is done to |
| 270 | * account for events that this instance may be have missed due to a staggered start. |
| 271 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 272 | private void bootstrapExistingApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 273 | apps.asJavaMap().forEach((appId, holder) -> setupApplicationAndNotify(appId, holder.app(), holder.state())); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 277 | * Loads the application inventory from the disk and activates apps if |
| 278 | * they are marked to be active. |
| 279 | */ |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 280 | private void loadFromDisk() { |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 281 | getApplicationNames().forEach(appName -> { |
| 282 | Application app = loadFromDisk(appName); |
| 283 | if (app != null && isActive(app.id().name())) { |
Thomas Vachuska | ad2965b | 2018-04-02 15:23:31 -0700 | [diff] [blame] | 284 | // For now, apps loaded from disk will be marked as having been |
| 285 | // activated explicitly, which means they won't deactivate |
| 286 | // implicitly when all dependent apps have been deactivated. |
| 287 | requiredBy.put(app.id(), coreAppId); |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 288 | activate(app.id(), false); |
| 289 | // TODO Load app permissions |
| 290 | } |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | private Application loadFromDisk(String appName) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 295 | pendingApps.add(appName); |
| 296 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 297 | for (int i = 0; i < MAX_LOAD_RETRIES; i++) { |
| 298 | try { |
| 299 | // Directly return if app already exists |
| 300 | ApplicationId appId = getId(appName); |
| 301 | if (appId != null) { |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 302 | Application application = getApplication(appId); |
| 303 | if (application != null) { |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 304 | pendingApps.remove(appName); |
Ray Milkey | 64591a6 | 2016-01-12 09:50:45 -0800 | [diff] [blame] | 305 | return application; |
| 306 | } |
Thomas Vachuska | 62f04a4 | 2015-04-22 14:38:34 -0700 | [diff] [blame] | 307 | } |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 308 | |
| 309 | ApplicationDescription appDesc = getApplicationDescription(appName); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 310 | |
| 311 | Optional<String> loop = appDesc.requiredApps().stream() |
| 312 | .filter(app -> pendingApps.contains(app)).findAny(); |
| 313 | if (loop.isPresent()) { |
| 314 | log.error("Circular app dependency detected: {} -> {}", pendingApps, loop.get()); |
| 315 | pendingApps.remove(appName); |
| 316 | return null; |
| 317 | } |
| 318 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 319 | boolean success = appDesc.requiredApps().stream() |
| 320 | .noneMatch(requiredApp -> loadFromDisk(requiredApp) == null); |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 321 | pendingApps.remove(appName); |
| 322 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 323 | return success ? create(appDesc, false) : null; |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 324 | |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 325 | } catch (Exception e) { |
Yuta HIGUCHI | 1d6fef3 | 2018-05-07 18:24:20 -0700 | [diff] [blame] | 326 | log.warn("Unable to load application {} from disk: {}; retrying", |
| 327 | appName, |
| 328 | Throwables.getRootCause(e).getMessage()); |
| 329 | log.debug("Full error details:", e); |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 330 | 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] | 331 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 332 | } |
Andrea Campanella | c8eca24 | 2016-04-06 19:34:32 -0700 | [diff] [blame] | 333 | pendingApps.remove(appName); |
Charles Chan | e889e2d | 2015-11-17 12:01:02 -0800 | [diff] [blame] | 334 | return null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Deactivate |
| 338 | public void deactivate() { |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 339 | clusterCommunicator.removeSubscriber(APP_BITS_REQUEST); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 340 | apps.removeStatusChangeListener(statusChangeListener); |
| 341 | apps.removeListener(appsListener); |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 342 | appActivationTopic.unsubscribe(appActivator); |
Madan Jampani | 2af244a | 2015-02-22 13:12:01 -0800 | [diff] [blame] | 343 | messageHandlingExecutor.shutdown(); |
Thomas Vachuska | 62ba45f | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 344 | activationExecutor.shutdown(); |
Madan Jampani | 6b5b717 | 2015-02-23 13:02:26 -0800 | [diff] [blame] | 345 | executor.shutdown(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 346 | log.info("Stopped"); |
| 347 | } |
| 348 | |
| 349 | @Override |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 350 | public void setDelegate(ApplicationStoreDelegate delegate) { |
| 351 | super.setDelegate(delegate); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 352 | executor.execute(this::bootstrapExistingApplications); |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 353 | executor.schedule((Runnable) this::loadFromDisk, APP_LOAD_DELAY_MS, TimeUnit.MILLISECONDS); |
Thomas Vachuska | cf96011 | 2015-03-06 22:36:51 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | @Override |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 357 | public Set<Application> getApplications() { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 358 | return ImmutableSet.copyOf(apps.values() |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 359 | .stream() |
| 360 | .map(Versioned::value) |
| 361 | .map(InternalApplicationHolder::app) |
| 362 | .collect(Collectors.toSet())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public ApplicationId getId(String name) { |
| 367 | return idStore.getAppId(name); |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public Application getApplication(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 372 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 373 | return appHolder != null ? appHolder.app() : null; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public ApplicationState getState(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 378 | InternalApplicationHolder appHolder = Versioned.valueOrNull(apps.get(appId)); |
| 379 | InternalState state = appHolder != null ? appHolder.state() : null; |
| 380 | return state == null ? null : state == ACTIVATED ? ApplicationState.ACTIVE : ApplicationState.INSTALLED; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public Application create(InputStream appDescStream) { |
| 385 | ApplicationDescription appDesc = saveApplication(appDescStream); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 386 | if (hasPrerequisites(appDesc)) { |
| 387 | return create(appDesc, true); |
| 388 | } |
Jonathan Hart | 14651b5 | 2016-06-07 17:59:53 -0700 | [diff] [blame] | 389 | // Purge bits off disk if we don't have prerequisites to allow app to be |
| 390 | // reinstalled later |
| 391 | purgeApplication(appDesc.name()); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 392 | throw new ApplicationException("Missing dependencies for app " + appDesc.name()); |
| 393 | } |
| 394 | |
| 395 | private boolean hasPrerequisites(ApplicationDescription app) { |
Yuta HIGUCHI | 56a7a6b | 2017-02-22 15:17:42 -0800 | [diff] [blame] | 396 | for (String required : app.requiredApps()) { |
| 397 | ApplicationId id = getId(required); |
| 398 | if (id == null || getApplication(id) == null) { |
| 399 | log.error("{} required for {} not available", required, app.name()); |
| 400 | return false; |
| 401 | } |
| 402 | } |
| 403 | return true; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 406 | private Application create(ApplicationDescription appDesc, boolean updateTime) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 407 | Application app = registerApp(appDesc); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 408 | if (updateTime) { |
| 409 | updateTime(app.id().name()); |
| 410 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 411 | InternalApplicationHolder previousApp = |
| 412 | Versioned.valueOrNull(apps.putIfAbsent(app.id(), new InternalApplicationHolder(app, INSTALLED, null))); |
| 413 | return previousApp != null ? previousApp.app() : app; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public void remove(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 418 | uninstallDependentApps(appId); |
| 419 | apps.remove(appId); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 422 | // Uninstalls all apps that depend on the given app. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 423 | private void uninstallDependentApps(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 424 | getApplications().stream() |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 425 | .filter(a -> a.requiredApps().contains(appId.name())) |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 426 | .forEach(a -> remove(a.id())); |
| 427 | } |
| 428 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 429 | @Override |
| 430 | public void activate(ApplicationId appId) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 431 | activate(appId, coreAppId); |
| 432 | } |
| 433 | |
| 434 | private void activate(ApplicationId appId, ApplicationId forAppId) { |
| 435 | requiredBy.put(appId, forAppId); |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 436 | activate(appId, true); |
| 437 | } |
| 438 | |
| 439 | private void activate(ApplicationId appId, boolean updateTime) { |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 440 | Versioned<InternalApplicationHolder> vAppHolder = apps.get(appId); |
| 441 | if (vAppHolder != null) { |
Thomas Vachuska | 161baf5 | 2015-03-27 16:15:39 -0700 | [diff] [blame] | 442 | if (updateTime) { |
| 443 | updateTime(appId.name()); |
| 444 | } |
Jonathan Hart | bd85f78 | 2016-06-20 16:13:37 -0700 | [diff] [blame] | 445 | activateRequiredApps(vAppHolder.value().app()); |
| 446 | |
| 447 | apps.computeIf(appId, v -> v != null && v.state() != ACTIVATED, |
| 448 | (k, v) -> new InternalApplicationHolder( |
| 449 | v.app(), ACTIVATED, v.permissions())); |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 450 | appActivationTopic.publish(vAppHolder.value().app()); |
Thomas Vachuska | 484ae54 | 2017-08-29 15:21:57 -0700 | [diff] [blame] | 451 | appActivationTopic.publish(null); // FIXME: Once ONOS-6977 is fixed |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 455 | // Activates all apps required by this application. |
| 456 | private void activateRequiredApps(Application app) { |
| 457 | app.requiredApps().stream().map(this::getId).forEach(id -> activate(id, app.id())); |
| 458 | } |
| 459 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 460 | @Override |
| 461 | public void deactivate(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 462 | deactivateDependentApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 463 | deactivate(appId, coreAppId); |
| 464 | } |
| 465 | |
| 466 | private void deactivate(ApplicationId appId, ApplicationId forAppId) { |
| 467 | requiredBy.remove(appId, forAppId); |
| 468 | if (requiredBy.get(appId).isEmpty()) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 469 | AtomicBoolean stateChanged = new AtomicBoolean(false); |
| 470 | apps.computeIf(appId, |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 471 | v -> v != null && v.state() != DEACTIVATED, |
| 472 | (k, v) -> { |
| 473 | stateChanged.set(true); |
| 474 | return new InternalApplicationHolder(v.app(), DEACTIVATED, v.permissions()); |
| 475 | }); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 476 | if (stateChanged.get()) { |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 477 | updateTime(appId.name()); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 478 | deactivateRequiredApps(appId); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 479 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 483 | // Deactivates all apps that require this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 484 | private void deactivateDependentApps(ApplicationId appId) { |
| 485 | apps.values() |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 486 | .stream() |
| 487 | .map(Versioned::value) |
| 488 | .filter(a -> a.state() == ACTIVATED) |
| 489 | .filter(a -> a.app().requiredApps().contains(appId.name())) |
| 490 | .forEach(a -> deactivate(a.app().id())); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Deactivates all apps required by this application. |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 494 | private void deactivateRequiredApps(ApplicationId appId) { |
| 495 | getApplication(appId).requiredApps() |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 496 | .stream() |
| 497 | .map(this::getId) |
| 498 | .map(apps::get) |
| 499 | .map(Versioned::value) |
| 500 | .filter(a -> a.state() == ACTIVATED) |
| 501 | .forEach(a -> deactivate(a.app().id(), appId)); |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 504 | @Override |
| 505 | public Set<Permission> getPermissions(ApplicationId appId) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 506 | InternalApplicationHolder app = Versioned.valueOrNull(apps.get(appId)); |
| 507 | return app != null ? ImmutableSet.copyOf(app.permissions()) : ImmutableSet.of(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void setPermissions(ApplicationId appId, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 512 | AtomicBoolean permissionsChanged = new AtomicBoolean(false); |
| 513 | Versioned<InternalApplicationHolder> appHolder = apps.computeIf(appId, |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 514 | v -> v != null && !Sets.symmetricDifference(v.permissions(), permissions).isEmpty(), |
| 515 | (k, v) -> { |
| 516 | permissionsChanged.set(true); |
| 517 | return new InternalApplicationHolder(v.app(), v.state(), ImmutableSet.copyOf(permissions)); |
| 518 | }); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 519 | if (permissionsChanged.get()) { |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 520 | notifyDelegate(new ApplicationEvent(APP_PERMISSIONS_CHANGED, appHolder.value().app())); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 524 | @Override |
| 525 | public InputStream getApplicationArchive(ApplicationId appId) { |
| 526 | return getApplicationInputStream(appId.name()); |
| 527 | } |
| 528 | |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 529 | private class AppActivator implements Consumer<Application> { |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 530 | @Override |
Madan Jampani | c5a1754 | 2016-08-22 10:26:11 -0700 | [diff] [blame] | 531 | public void accept(Application app) { |
Thomas Vachuska | 484ae54 | 2017-08-29 15:21:57 -0700 | [diff] [blame] | 532 | if (app != null) { // FIXME: Once ONOS-6977 is fixed |
| 533 | String appName = app.id().name(); |
| 534 | installAppIfNeeded(app); |
| 535 | setActive(appName); |
| 536 | notifyDelegate(new ApplicationEvent(APP_ACTIVATED, app)); |
| 537 | } |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 541 | /** |
| 542 | * Listener to application state distributed map changes. |
| 543 | */ |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 544 | private final class InternalAppsListener |
| 545 | implements MapEventListener<ApplicationId, InternalApplicationHolder> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 546 | @Override |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 547 | public void event(MapEvent<ApplicationId, InternalApplicationHolder> event) { |
Thomas Vachuska | 4fcdae7 | 2015-03-24 10:22:54 -0700 | [diff] [blame] | 548 | if (delegate == null) { |
| 549 | return; |
| 550 | } |
| 551 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 552 | ApplicationId appId = event.key(); |
| 553 | InternalApplicationHolder newApp = event.newValue() == null ? null : event.newValue().value(); |
| 554 | InternalApplicationHolder oldApp = event.oldValue() == null ? null : event.oldValue().value(); |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 555 | if (event.type() == MapEvent.Type.UPDATE && (newApp == null || oldApp == null || |
| 556 | newApp.state() == oldApp.state())) { |
| 557 | log.warn("Can't update the application {}", event.key()); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 558 | return; |
| 559 | } |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 560 | 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] | 561 | setupApplicationAndNotify(appId, newApp.app(), newApp.state()); |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 562 | } else if (event.type() == MapEvent.Type.REMOVE && oldApp != null) { |
Deepa Vadireddy | 9719eb0 | 2017-11-23 10:12:30 +0000 | [diff] [blame] | 563 | notifyDelegate(new ApplicationEvent(APP_UNINSTALLED, oldApp.app())); |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 564 | purgeApplication(appId.name()); |
| 565 | } else { |
| 566 | log.warn("Can't perform {} on application {}", event.type(), event.key()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 571 | private void setupApplicationAndNotify(ApplicationId appId, Application app, InternalState state) { |
Madan Jampani | ae538ff | 2016-08-19 12:42:43 -0700 | [diff] [blame] | 572 | // ACTIVATED state is handled separately in NextAppToActivateValueListener |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 573 | if (state == INSTALLED) { |
| 574 | fetchBitsIfNeeded(app); |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 575 | notifyDelegate(new ApplicationEvent(APP_INSTALLED, app)); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 576 | } else if (state == DEACTIVATED) { |
| 577 | clearActive(appId.name()); |
Ray Milkey | 2f6f907 | 2016-08-23 13:18:50 -0700 | [diff] [blame] | 578 | notifyDelegate(new ApplicationEvent(APP_DEACTIVATED, app)); |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 582 | /** |
| 583 | * Determines if the application bits are available locally. |
| 584 | */ |
| 585 | private boolean appBitsAvailable(Application app) { |
| 586 | try { |
| 587 | ApplicationDescription appDesc = getApplicationDescription(app.id().name()); |
| 588 | return appDesc.version().equals(app.version()); |
| 589 | } catch (ApplicationException e) { |
| 590 | return false; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Fetches the bits from the cluster peers if necessary. |
| 596 | */ |
| 597 | private void fetchBitsIfNeeded(Application app) { |
| 598 | if (!appBitsAvailable(app)) { |
Thomas Vachuska | 62ba45f | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 599 | fetchBits(app, false); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Installs the application if necessary from the application peers. |
| 605 | */ |
| 606 | private void installAppIfNeeded(Application app) { |
| 607 | if (!appBitsAvailable(app)) { |
Thomas Vachuska | 62ba45f | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 608 | fetchBits(app, true); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Fetches the bits from the cluster peers. |
| 614 | */ |
Thomas Vachuska | 62ba45f | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 615 | private void fetchBits(Application app, boolean delegateInstallation) { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 616 | ControllerNode localNode = clusterService.getLocalNode(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 617 | CountDownLatch latch = new CountDownLatch(1); |
| 618 | |
| 619 | // FIXME: send message with name & version to make sure we don't get served old bits |
| 620 | |
| 621 | log.info("Downloading bits for application {}", app.id().name()); |
| 622 | for (ControllerNode node : clusterService.getNodes()) { |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 623 | if (latch.getCount() == 0) { |
| 624 | break; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 625 | } |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 626 | if (node.equals(localNode)) { |
| 627 | continue; |
| 628 | } |
| 629 | clusterCommunicator.sendAndReceive(app.id().name(), |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 630 | APP_BITS_REQUEST, |
| 631 | s -> s.getBytes(Charsets.UTF_8), |
| 632 | Function.identity(), |
| 633 | node.id()) |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 634 | .whenCompleteAsync((bits, error) -> { |
| 635 | if (error == null && latch.getCount() > 0) { |
| 636 | saveApplication(new ByteArrayInputStream(bits)); |
| 637 | log.info("Downloaded bits for application {} from node {}", |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 638 | app.id().name(), node.id()); |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 639 | latch.countDown(); |
Thomas Vachuska | 62ba45f | 2017-10-02 13:17:28 -0700 | [diff] [blame] | 640 | if (delegateInstallation) { |
| 641 | notifyDelegate(new ApplicationEvent(APP_INSTALLED, app)); |
| 642 | } |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 643 | } else if (error != null) { |
| 644 | log.warn("Unable to fetch bits for application {} from node {}", |
Andrea Campanella | aaa9f11 | 2018-01-26 12:45:24 +0100 | [diff] [blame] | 645 | app.id().name(), node.id()); |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 646 | } |
Jordan Halterman | 85f560d | 2017-09-20 11:57:58 -0700 | [diff] [blame] | 647 | }, messageHandlingExecutor); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | try { |
| 651 | if (!latch.await(FETCH_TIMEOUT_MS, MILLISECONDS)) { |
| 652 | log.warn("Unable to fetch bits for application {}", app.id().name()); |
| 653 | } |
| 654 | } catch (InterruptedException e) { |
| 655 | log.warn("Interrupted while fetching bits for application {}", app.id().name()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 656 | Thread.currentThread().interrupt(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 661 | * Produces a registered application from the supplied description. |
| 662 | */ |
| 663 | private Application registerApp(ApplicationDescription appDesc) { |
| 664 | ApplicationId appId = idStore.registerApplication(appDesc.name()); |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 665 | return DefaultApplication |
| 666 | .builder(appDesc) |
| 667 | .withAppId(appId) |
| 668 | .build(); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 669 | } |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 670 | |
| 671 | /** |
| 672 | * Internal class for holding app information. |
| 673 | */ |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 674 | private static final class InternalApplicationHolder { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 675 | private final Application app; |
| 676 | private final InternalState state; |
| 677 | private final Set<Permission> permissions; |
| 678 | |
| 679 | @SuppressWarnings("unused") |
| 680 | private InternalApplicationHolder() { |
| 681 | app = null; |
| 682 | state = null; |
| 683 | permissions = null; |
| 684 | } |
| 685 | |
Thomas Vachuska | bddbb25 | 2016-08-08 14:25:05 -0700 | [diff] [blame] | 686 | private InternalApplicationHolder(Application app, InternalState state, Set<Permission> permissions) { |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 687 | this.app = Preconditions.checkNotNull(app); |
| 688 | this.state = state; |
| 689 | this.permissions = permissions == null ? null : ImmutableSet.copyOf(permissions); |
| 690 | } |
| 691 | |
| 692 | public Application app() { |
| 693 | return app; |
| 694 | } |
| 695 | |
| 696 | public InternalState state() { |
| 697 | return state; |
| 698 | } |
| 699 | |
| 700 | public Set<Permission> permissions() { |
| 701 | return permissions; |
| 702 | } |
| 703 | |
| 704 | @Override |
| 705 | public String toString() { |
| 706 | return MoreObjects.toStringHelper(getClass()) |
| 707 | .add("app", app.id()) |
| 708 | .add("state", state) |
| 709 | .toString(); |
| 710 | } |
| 711 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 712 | } |