Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [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 | |
| 17 | package org.onosproject.net.statistic; |
| 18 | |
| 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.flow.FlowEntry; |
| 21 | import org.onosproject.net.flow.TypedStoredFlowEntry; |
| 22 | import org.onosproject.net.flow.DefaultTypedFlowEntry; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
| 26 | /** |
| 27 | * Load of flow entry of flow live type. |
| 28 | */ |
| 29 | public class TypedFlowEntryWithLoad { |
| 30 | private ConnectPoint cp; |
| 31 | private TypedStoredFlowEntry tfe; |
| 32 | private Load load; |
| 33 | |
| 34 | //TODO: make this variables class, and share with NewAdaptivceFlowStatsCollector class |
| 35 | private static final int CAL_AND_POLL_INTERVAL = 5; // means SHORT_POLL_INTERVAL |
| 36 | private static final int MID_POLL_INTERVAL = 10; |
| 37 | private static final int LONG_POLL_INTERVAL = 15; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new typed flow entry with load. |
| 41 | * |
| 42 | * @param cp connect point |
| 43 | * @param tfe typed flow entry |
| 44 | * @param load load |
| 45 | */ |
| 46 | public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe, Load load) { |
| 47 | this.cp = cp; |
| 48 | this.tfe = tfe; |
| 49 | this.load = load; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates a new typed flow entry with load. |
| 54 | * |
| 55 | * @param cp connect point |
| 56 | * @param tfe typed flow entry |
| 57 | */ |
| 58 | public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe) { |
| 59 | this.cp = cp; |
| 60 | this.tfe = tfe; |
| 61 | this.load = new DefaultLoad(tfe.bytes(), 0, typedPollInterval(tfe)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new typed flow entry with load. |
| 66 | * |
| 67 | * @param cp connect point |
| 68 | * @param fe flow entry |
| 69 | */ |
| 70 | public TypedFlowEntryWithLoad(ConnectPoint cp, FlowEntry fe) { |
| 71 | this.cp = cp; |
| 72 | this.tfe = newTypedStoredFlowEntry(fe); |
| 73 | this.load = new DefaultLoad(fe.bytes(), 0, typedPollInterval(this.tfe)); |
| 74 | } |
| 75 | |
| 76 | public ConnectPoint connectPoint() { |
| 77 | return cp; |
| 78 | } |
| 79 | public TypedStoredFlowEntry typedStoredFlowEntry() { |
| 80 | return tfe; |
| 81 | } |
| 82 | public Load load() { |
| 83 | return load; |
| 84 | } |
| 85 | public void setLoad(Load load) { |
| 86 | this.load = load; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns short polling interval. |
| 91 | * |
| 92 | * @return short poll interval |
| 93 | */ |
| 94 | public static int shortPollInterval() { |
| 95 | return CAL_AND_POLL_INTERVAL; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns mid polling interval. |
| 100 | * |
| 101 | * @return mid poll interval |
| 102 | */ |
| 103 | public static int midPollInterval() { |
| 104 | return MID_POLL_INTERVAL; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns long polling interval. |
| 109 | * |
| 110 | * @return long poll interval |
| 111 | */ |
| 112 | public static int longPollInterval() { |
| 113 | return LONG_POLL_INTERVAL; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns average polling interval. |
| 118 | * |
| 119 | * @return average poll interval |
| 120 | */ |
| 121 | public static int avgPollInterval() { |
| 122 | return (CAL_AND_POLL_INTERVAL + MID_POLL_INTERVAL + LONG_POLL_INTERVAL) / 3; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns current typed flow entry's polling interval. |
| 127 | * |
| 128 | * @param tfe typed flow entry |
| 129 | * @return typed poll interval |
| 130 | */ |
| 131 | public static long typedPollInterval(TypedStoredFlowEntry tfe) { |
| 132 | checkNotNull(tfe, "TypedStoredFlowEntry cannot be null"); |
| 133 | |
| 134 | switch (tfe.flowLiveType()) { |
| 135 | case LONG_FLOW: |
| 136 | return LONG_POLL_INTERVAL; |
| 137 | case MID_FLOW: |
| 138 | return MID_POLL_INTERVAL; |
| 139 | case SHORT_FLOW: |
| 140 | case IMMEDIATE_FLOW: |
| 141 | default: |
| 142 | return CAL_AND_POLL_INTERVAL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a new typed flow entry with the given flow entry fe. |
| 148 | * |
| 149 | * @param fe flow entry |
| 150 | * @return new typed flow entry |
| 151 | */ |
| 152 | public static TypedStoredFlowEntry newTypedStoredFlowEntry(FlowEntry fe) { |
| 153 | if (fe == null) { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | long life = fe.life(); |
| 158 | |
| 159 | if (life >= LONG_POLL_INTERVAL) { |
| 160 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.LONG_FLOW); |
| 161 | } else if (life >= MID_POLL_INTERVAL) { |
| 162 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.MID_FLOW); |
| 163 | } else if (life >= CAL_AND_POLL_INTERVAL) { |
| 164 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW); |
| 165 | } else if (life >= 0) { |
| 166 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW); |
| 167 | } else { // life < 0 |
| 168 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW); |
| 169 | } |
| 170 | } |
| 171 | } |