Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [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 | */ |
Sho SHIMIZU | 739873b | 2016-02-23 17:02:12 -0800 | [diff] [blame] | 16 | package org.onosproject.store.intent.impl; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 17 | |
Sho SHIMIZU | cfe707a | 2015-10-12 17:45:42 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.intent.IntentId; |
Sho SHIMIZU | 739873b | 2016-02-23 17:02:12 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentSetMultimap; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.ConsistentMap; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.StorageService; |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Versioned; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Set; |
| 37 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 38 | import static org.slf4j.LoggerFactory.getLogger; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Sho SHIMIZU | 7d20af1 | 2015-10-01 16:03:51 -0700 | [diff] [blame] | 41 | * A collection that maps Intent IDs as keys to values as Intent IDs, |
| 42 | * where each key may associated with multiple values without duplication. |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 43 | */ |
Sho SHIMIZU | 5c396e3 | 2016-08-12 15:19:12 -0700 | [diff] [blame] | 44 | @Component(immediate = true) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 45 | @Service |
Sho SHIMIZU | cfe707a | 2015-10-12 17:45:42 -0700 | [diff] [blame] | 46 | @Beta |
Sho SHIMIZU | 7d20af1 | 2015-10-01 16:03:51 -0700 | [diff] [blame] | 47 | public class ConsistentIntentSetMultimap implements IntentSetMultimap { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
Jon Hall | dbe4c53 | 2016-10-04 11:45:52 -0700 | [diff] [blame] | 50 | private static final String INTENT_MAPPING = "onos-intent-mapping"; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 51 | |
Sho SHIMIZU | 200a739 | 2015-07-23 16:28:35 -0700 | [diff] [blame] | 52 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 53 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 54 | private ConsistentMap<IntentId, Set<IntentId>> intentMapping; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected StorageService storageService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected DeviceService deviceService; |
| 61 | |
| 62 | @Activate |
| 63 | public void activate() { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 64 | intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder() |
| 65 | .withName(INTENT_MAPPING) |
| 66 | .withSerializer(SERIALIZER) |
| 67 | .build(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 68 | log.info("Started"); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | log.info("Stopped"); |
| 74 | } |
| 75 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public Set<IntentId> getMapping(IntentId intentId) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 78 | Versioned<Set<IntentId>> result = intentMapping.get(intentId); |
| 79 | |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 80 | if (result != null) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 81 | return result.value(); |
| 82 | } |
| 83 | |
| 84 | return null; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 88 | public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) { |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 89 | Versioned<Set<IntentId>> versionedIntents = intentMapping.get(keyIntentId); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 90 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 91 | if (versionedIntents == null) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 92 | Set<IntentId> newSet = new HashSet<>(); |
| 93 | newSet.add(valIntentId); |
| 94 | intentMapping.put(keyIntentId, newSet); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 95 | } else { |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 96 | versionedIntents.value().add(valIntentId); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
| 102 | @Override |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 103 | public void releaseMapping(IntentId intentId) { |
| 104 | for (IntentId intent : intentMapping.keySet()) { |
| 105 | // TODO: optimize by checking for identical src & dst |
| 106 | Set<IntentId> mapping = intentMapping.get(intent).value(); |
| 107 | if (mapping.remove(intentId)) { |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 113 | } |