Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.provider.lisp.mapping.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.lisp.ctl.LispController; |
| 24 | import org.onosproject.lisp.ctl.LispMessageListener; |
| 25 | import org.onosproject.lisp.ctl.LispRouterId; |
| 26 | import org.onosproject.lisp.ctl.LispRouterListener; |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 29 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 30 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 31 | import org.onosproject.mapping.MappingEntry; |
| 32 | import org.onosproject.mapping.MappingProvider; |
| 33 | import org.onosproject.mapping.MappingProviderRegistry; |
| 34 | import org.onosproject.mapping.MappingProviderService; |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 35 | import org.onosproject.mapping.MappingStore; |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 36 | import org.onosproject.net.DeviceId; |
Jian Li | e5aa5df | 2017-04-02 22:40:56 +0900 | [diff] [blame^] | 37 | import org.onosproject.net.device.DeviceService; |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 38 | import org.onosproject.net.provider.AbstractProvider; |
| 39 | import org.onosproject.net.provider.ProviderId; |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 40 | import org.onosproject.provider.lisp.mapping.util.MappingEntryBuilder; |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 44 | import java.util.List; |
| 45 | |
Jian Li | c7badf9 | 2017-02-02 19:43:46 +0900 | [diff] [blame] | 46 | import static org.onosproject.mapping.MappingStore.Type.MAP_CACHE; |
| 47 | import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE; |
| 48 | |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 49 | /** |
| 50 | * Provider which uses a LISP controller to manage EID-RLOC mapping. |
| 51 | */ |
| 52 | @Component(immediate = true) |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 53 | public class LispMappingProvider extends AbstractProvider implements MappingProvider { |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 54 | |
| 55 | private static final Logger log = LoggerFactory.getLogger(LispMappingProvider.class); |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected LispController controller; |
| 59 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected MappingProviderRegistry providerRegistry; |
| 62 | |
Jian Li | e5aa5df | 2017-04-02 22:40:56 +0900 | [diff] [blame^] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected DeviceService deviceService; |
| 65 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 66 | protected MappingProviderService providerService; |
| 67 | |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 68 | private static final String SCHEME_NAME = "lisp"; |
| 69 | private static final String MAPPING_PROVIDER_PACKAGE = |
| 70 | "org.onosproject.lisp.provider.mapping"; |
| 71 | |
| 72 | private final InternalMappingProvider listener = new InternalMappingProvider(); |
| 73 | |
| 74 | /** |
| 75 | * Creates a LISP mapping provider with the supplier identifier. |
| 76 | */ |
| 77 | public LispMappingProvider() { |
| 78 | super(new ProviderId(SCHEME_NAME, MAPPING_PROVIDER_PACKAGE)); |
| 79 | } |
| 80 | |
| 81 | @Activate |
| 82 | public void activate() { |
| 83 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 84 | providerService = providerRegistry.register(this); |
| 85 | |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 86 | // listens all LISP router related events |
| 87 | controller.addRouterListener(listener); |
| 88 | |
| 89 | // listens all LISP control message |
| 90 | controller.addMessageListener(listener); |
| 91 | |
| 92 | log.info("Started"); |
| 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | public void deactivate() { |
| 97 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 98 | providerRegistry.unregister(this); |
| 99 | |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 100 | // stops listening all LISP router related events |
| 101 | controller.removeRouterListener(listener); |
| 102 | |
| 103 | // stops listening all LISP control messages |
| 104 | controller.removeMessageListener(listener); |
| 105 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 106 | providerService = null; |
| 107 | |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 108 | log.info("Stopped"); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * A listener for LISP router events and control messages. |
| 113 | */ |
| 114 | private class InternalMappingProvider implements LispRouterListener, |
| 115 | LispMessageListener { |
| 116 | |
| 117 | @Override |
| 118 | public void routerAdded(LispRouterId routerId) { |
| 119 | |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void routerRemoved(LispRouterId routerId) { |
| 124 | |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void routerChanged(LispRouterId routerId) { |
| 129 | |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void handleIncomingMessage(LispRouterId routerId, LispMessage msg) { |
| 134 | |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void handleOutgoingMessage(LispRouterId routerId, LispMessage msg) { |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 139 | if (providerService == null) { |
| 140 | // We are shutting down, nothing to be done |
| 141 | return; |
| 142 | } |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 143 | |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 144 | DeviceId deviceId = DeviceId.deviceId(routerId.toString()); |
| 145 | switch (msg.getType()) { |
| 146 | |
| 147 | case LISP_MAP_REPLY: |
| 148 | LispMapReply reply = (LispMapReply) msg; |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 149 | processMappings(deviceId, reply.getMapRecords(), MAP_CACHE); |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | case LISP_MAP_NOTIFY: |
| 153 | LispMapNotify notify = (LispMapNotify) msg; |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 154 | processMappings(deviceId, notify.getMapRecords(), MAP_DATABASE); |
Jian Li | 77d6e75 | 2017-01-29 23:24:18 +0900 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | default: |
| 158 | log.warn("Unhandled message type: {}", msg.getType()); |
| 159 | } |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 160 | } |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Converts map records into mapping, notifies to provider. |
| 164 | * |
| 165 | * @param deviceId device identifier |
| 166 | * @param records a collection of map records |
| 167 | * @param type MappingStore type |
| 168 | */ |
| 169 | private void processMappings(DeviceId deviceId, |
| 170 | List<LispMapRecord> records, |
| 171 | MappingStore.Type type) { |
| 172 | records.forEach(r -> { |
Jian Li | e5aa5df | 2017-04-02 22:40:56 +0900 | [diff] [blame^] | 173 | MappingEntry me = |
| 174 | new MappingEntryBuilder(deviceId, r, deviceService).build(); |
Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 175 | providerService.mappingAdded(me, type); |
| 176 | }); |
| 177 | } |
Jian Li | 7d36157 | 2017-01-13 11:39:37 +0900 | [diff] [blame] | 178 | } |
| 179 | } |