blob: c78b160f45b10f0f5714a76c1dc7c62f0536a05e [file] [log] [blame]
Jian Li0b93b002018-07-31 13:41:08 +09001/*
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 */
16package org.onosproject.openstacktroubleshoot.util;
17
18import org.onlab.packet.Ip4Address;
19import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
22import org.onosproject.net.device.DeviceService;
23import org.onosproject.net.flow.instructions.ExtensionPropertyException;
24import org.onosproject.net.flow.instructions.ExtensionTreatment;
25import org.onosproject.openstacknetworking.api.InstancePort;
26import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
27import org.slf4j.Logger;
28
29import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST;
30import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Provides common methods to help populating flow rules for troubleshoot app.
34 */
35public 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}