blob: d5366ae175865e99a02f61e1ab7fed9f30cf7166 [file] [log] [blame]
Jian Li4aa17642019-01-30 00:01:11 +09001/*
2 * Copyright 2019-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.k8snetworking.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.slf4j.Logger;
26
27import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST;
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * Provides common methods to help populating flow rules for SONA applications.
32 */
33public final class RulePopulatorUtil {
34
35 private static final Logger log = getLogger(RulePopulatorUtil.class);
36
37 private static final String TUNNEL_DST = "tunnelDst";
38
39 private RulePopulatorUtil() {
40 }
41
42 /**
43 * Returns tunnel destination extension treatment object.
44 *
45 * @param deviceService driver service
46 * @param deviceId device id to apply this treatment
47 * @param remoteIp tunnel destination ip address
48 * @return extension treatment
49 */
50 public static ExtensionTreatment buildExtension(DeviceService deviceService,
51 DeviceId deviceId,
52 Ip4Address remoteIp) {
53 Device device = deviceService.getDevice(deviceId);
54 if (device != null && !device.is(ExtensionTreatmentResolver.class)) {
55 log.error("The extension treatment is not supported");
56 return null;
57 }
58
59 if (device == null) {
60 return null;
61 }
62
63 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
64 ExtensionTreatment treatment =
65 resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
66 try {
67 treatment.setPropertyValue(TUNNEL_DST, remoteIp);
68 return treatment;
69 } catch (ExtensionPropertyException e) {
70 log.warn("Failed to get tunnelDst extension treatment for {} " +
71 "because of {}", deviceId, e);
72 return null;
73 }
74 }
75}