Jian Li | 0b93b00 | 2018-07-31 13:41:08 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacktroubleshoot.util; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 24 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Provides common methods to help populating flow rules for troubleshoot app. |
| 34 | */ |
| 35 | public final class OpenstackTroubleshootUtil { |
| 36 | |
| 37 | private static final Logger log = getLogger(OpenstackTroubleshootUtil.class); |
| 38 | |
| 39 | private static final String TUNNEL_DST = "tunnelDst"; |
| 40 | |
| 41 | private OpenstackTroubleshootUtil() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns tunnel destination extension treatment object. |
| 46 | * |
| 47 | * @param deviceService driver service |
| 48 | * @param deviceId device id to apply this treatment |
| 49 | * @param remoteIp tunnel destination ip address |
| 50 | * @return extension treatment |
| 51 | */ |
| 52 | public static ExtensionTreatment buildExtension(DeviceService deviceService, |
| 53 | DeviceId deviceId, |
| 54 | Ip4Address remoteIp) { |
| 55 | Device device = deviceService.getDevice(deviceId); |
| 56 | if (device != null && !device.is(ExtensionTreatmentResolver.class)) { |
| 57 | log.error("The extension treatment is not supported"); |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | if (device == null) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class); |
| 66 | ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
| 67 | try { |
| 68 | treatment.setPropertyValue(TUNNEL_DST, remoteIp); |
| 69 | return treatment; |
| 70 | } catch (ExtensionPropertyException e) { |
| 71 | log.warn("Failed to get tunnelDst extension treatment for {}", deviceId); |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns segment ID of the given instance port where a VM is attached. |
| 78 | * |
| 79 | * @param service openstack network service |
| 80 | * @param port instance port |
| 81 | * @return segment ID |
| 82 | */ |
| 83 | public static long getSegId(OpenstackNetworkService service, InstancePort port) { |
| 84 | return Long.parseLong(service.segmentId(port.networkId())); |
| 85 | } |
| 86 | } |