Xin Jin | 313708b | 2015-07-09 13:43:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.net.flowobjective.impl.composition; |
| 17 | |
| 18 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 19 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| 29 | * Provides a table to store Forward. |
| 30 | */ |
| 31 | public class ForwardTable { |
| 32 | |
| 33 | protected Map<Integer, ForwardingObjective> forwardMap; |
| 34 | protected Map<Integer, List<ForwardingObjective>> generatedParentForwardingObjectiveMap; |
| 35 | |
| 36 | public ForwardTable() { |
| 37 | this.forwardMap = new HashMap<>(); |
| 38 | this.generatedParentForwardingObjectiveMap = new HashMap<>(); |
| 39 | } |
| 40 | |
| 41 | public ForwardUpdateTable updateForward(ForwardingObjective forwardingObjective) { |
| 42 | ForwardUpdateTable updates = new ForwardUpdateTable(); |
| 43 | switch (forwardingObjective.op()) { |
| 44 | case ADD: |
| 45 | this.forwardMap.put(forwardingObjectiveHash(forwardingObjective), forwardingObjective); |
| 46 | this.generatedParentForwardingObjectiveMap |
| 47 | .put(forwardingObjectiveHash(forwardingObjective), new ArrayList<>()); |
| 48 | updates.addObjectives.add(forwardingObjective); |
| 49 | break; |
| 50 | case REMOVE: |
| 51 | if (this.forwardMap.remove(forwardingObjectiveHash(forwardingObjective)) != null) { |
| 52 | updates.removeObjectives.add(forwardingObjective); |
| 53 | } |
| 54 | break; |
| 55 | default: |
| 56 | break; |
| 57 | } |
| 58 | return updates; |
| 59 | } |
| 60 | |
| 61 | public ForwardUpdateTable updateForward(List<ForwardingObjective> forwardingObjectives) { |
| 62 | ForwardUpdateTable updates = new ForwardUpdateTable(); |
| 63 | for (ForwardingObjective forwardingObjective : forwardingObjectives) { |
| 64 | updates.addUpdateTable(this.updateForward(forwardingObjective)); |
| 65 | } |
| 66 | return updates; |
| 67 | } |
| 68 | |
| 69 | public void addGeneratedParentForwardingObjective(ForwardingObjective child, ForwardingObjective parent) { |
| 70 | this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child)).add(parent); |
| 71 | } |
| 72 | |
| 73 | public void deleteGeneratedParentForwardingObjective(List<ForwardingObjective> children) { |
| 74 | for (ForwardingObjective fo : children) { |
| 75 | this.generatedParentForwardingObjectiveMap.remove(forwardingObjectiveHash(fo)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private List<ForwardingObjective> getGeneratedParentForwardingObjective(ForwardingObjective child) { |
| 80 | return this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child)); |
| 81 | } |
| 82 | |
| 83 | public List<ForwardingObjective> getGeneratedParentForwardingObjectiveForRemove(ForwardingObjective child) { |
| 84 | List<ForwardingObjective> fos = this.generatedParentForwardingObjectiveMap.get(forwardingObjectiveHash(child)); |
| 85 | List<ForwardingObjective> removeFos = new ArrayList<>(); |
| 86 | for (ForwardingObjective fo : fos) { |
| 87 | removeFos.add(DefaultForwardingObjective.builder() |
| 88 | .fromApp(fo.appId()) |
| 89 | .makePermanent() |
| 90 | .withFlag(fo.flag()) |
| 91 | .withPriority(fo.priority()) |
| 92 | .withSelector(fo.selector()) |
| 93 | .withTreatment(fo.treatment()) |
| 94 | .remove()); |
| 95 | } |
| 96 | return removeFos; |
| 97 | } |
| 98 | |
| 99 | public Collection<ForwardingObjective> getForwardingObjectives() { |
| 100 | return this.forwardMap.values(); |
| 101 | } |
| 102 | |
| 103 | public static int forwardingObjectiveHash(ForwardingObjective forwardingObjective) { |
| 104 | return Objects.hash(forwardingObjective.selector(), forwardingObjective.flag(), |
| 105 | forwardingObjective.permanent(), forwardingObjective.timeout(), |
| 106 | forwardingObjective.appId(), forwardingObjective.priority(), |
| 107 | forwardingObjective.nextId(), forwardingObjective.treatment()); |
| 108 | } |
| 109 | } |