Pier Luigi | bdcd967 | 2017-10-13 13:54:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.driver.trafficcontrol; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.trafficcontrol.Policer; |
| 21 | import org.onosproject.net.behaviour.trafficcontrol.PolicerConfigurable; |
| 22 | import org.onosproject.net.behaviour.trafficcontrol.PolicerId; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.driver.DriverHandler; |
| 25 | import org.onosproject.net.meter.MeterId; |
| 26 | import org.onosproject.net.meter.MeterService; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.net.URI; |
| 30 | import java.util.Collection; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of policer config which allows to add, delete and get policers. |
| 36 | */ |
| 37 | public class OpenFlowPolicerConfigurable extends AbstractHandlerBehaviour implements PolicerConfigurable { |
| 38 | |
| 39 | // log |
| 40 | private final Logger log = getLogger(OpenFlowPolicerConfigurable.class); |
| 41 | // OpenFlow scheme |
| 42 | private static final String OF_SCHEME = "of"; |
| 43 | // Hex |
| 44 | private static final int HEX = 16; |
| 45 | |
| 46 | // Create a policer id from a meter id |
| 47 | private PolicerId getPolicerIdFromMeterId(MeterId meterId) { |
| 48 | // Create URI representing the meter id |
| 49 | URI uri = URI.create(OF_SCHEME + ":" + Long.toHexString(meterId.id())); |
| 50 | // Return the new policer id |
| 51 | return PolicerId.policerId(uri); |
| 52 | } |
| 53 | |
| 54 | // Create a meter id from a policer id |
| 55 | private MeterId getMeterIdFromPolicerId(PolicerId policerId) { |
| 56 | // Get scheme specific part |
| 57 | Long id = Long.parseLong(policerId.uri().getSchemeSpecificPart(), HEX); |
| 58 | // Return the meter id |
| 59 | return MeterId.meterId(id); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public PolicerId allocatePolicerId() { |
| 64 | // Init step |
| 65 | DriverHandler handler = handler(); |
| 66 | // First step is to get MeterService |
| 67 | MeterService meterService = handler.get(MeterService.class); |
| 68 | // There was a problem, return none |
| 69 | if (meterService == null) { |
| 70 | log.warn("MeterService is null"); |
| 71 | return PolicerId.NONE; |
| 72 | } |
| 73 | // Let's get the device id |
| 74 | DeviceId deviceId = handler.data().deviceId(); |
| 75 | // Double check correspondence between schemas |
| 76 | if (!deviceId.uri().getScheme().equals(OF_SCHEME)) { |
| 77 | log.warn("The device {} does not seem to be managed by OpenFlow", deviceId); |
| 78 | return PolicerId.NONE; |
| 79 | } |
| 80 | // Get a new meter id |
| 81 | MeterId meterId = meterService.allocateMeterId(deviceId); |
| 82 | // There was a problem |
| 83 | if (meterId == null) { |
| 84 | log.warn("MeterService does not provide valid ids"); |
| 85 | return PolicerId.NONE; |
| 86 | } |
| 87 | // Create a policer id from the meter id |
| 88 | return getPolicerIdFromMeterId(meterId); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void freePolicerId(PolicerId id) { |
| 93 | // Init step |
| 94 | DriverHandler handler = handler(); |
| 95 | // First step is to get MeterService |
| 96 | MeterService meterService = handler.get(MeterService.class); |
| 97 | // There was a problem, return none |
| 98 | if (meterService == null) { |
| 99 | log.warn("MeterService is null"); |
| 100 | return; |
| 101 | } |
| 102 | // Let's get the device id |
| 103 | DeviceId deviceId = handler.data().deviceId(); |
| 104 | // Double check correspondence with device schema |
| 105 | if (!deviceId.uri().getScheme().equals(OF_SCHEME)) { |
| 106 | log.warn("The device {} does not seem to be managed by OpenFlow", deviceId); |
| 107 | return; |
| 108 | } |
| 109 | // Double check correspondence with pid schema |
| 110 | if (!id.uri().getScheme().equals(OF_SCHEME)) { |
| 111 | log.warn("The id {} does not seem to be OpenFlow", id); |
| 112 | return; |
| 113 | } |
| 114 | // Get the meter id |
| 115 | MeterId meterId = getMeterIdFromPolicerId(id); |
| 116 | // Free the meter id |
| 117 | meterService.freeMeterId(deviceId, meterId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void addPolicer(Policer policer) { |
| 122 | throw new UnsupportedOperationException("addPolicer not yet implemented"); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void deletePolicer(PolicerId id) { |
| 127 | throw new UnsupportedOperationException("deletePolicer not yet implemented"); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Policer getPolicer(PolicerId policerId) { |
| 132 | throw new UnsupportedOperationException("getPolicer not yet implemented"); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Collection<Policer> getPolicers() { |
| 137 | throw new UnsupportedOperationException("getPolicers not yet implemented"); |
| 138 | } |
| 139 | |
| 140 | } |