Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 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 { |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 30 | private final ConnectPoint cp; |
| 31 | private final TypedStoredFlowEntry tfe; |
| 32 | private final Load load; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Creates a new typed flow entry with load. |
| 36 | * |
| 37 | * @param cp connect point |
| 38 | * @param tfe typed flow entry |
| 39 | * @param load load |
| 40 | */ |
| 41 | public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe, Load load) { |
| 42 | this.cp = cp; |
| 43 | this.tfe = tfe; |
| 44 | this.load = load; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates a new typed flow entry with load. |
| 49 | * |
| 50 | * @param cp connect point |
| 51 | * @param tfe typed flow entry |
| 52 | */ |
| 53 | public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe) { |
| 54 | this.cp = cp; |
| 55 | this.tfe = tfe; |
| 56 | this.load = new DefaultLoad(tfe.bytes(), 0, typedPollInterval(tfe)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a new typed flow entry with load. |
| 61 | * |
| 62 | * @param cp connect point |
| 63 | * @param fe flow entry |
| 64 | */ |
| 65 | public TypedFlowEntryWithLoad(ConnectPoint cp, FlowEntry fe) { |
| 66 | this.cp = cp; |
| 67 | this.tfe = newTypedStoredFlowEntry(fe); |
| 68 | this.load = new DefaultLoad(fe.bytes(), 0, typedPollInterval(this.tfe)); |
| 69 | } |
| 70 | |
| 71 | public ConnectPoint connectPoint() { |
| 72 | return cp; |
| 73 | } |
| 74 | public TypedStoredFlowEntry typedStoredFlowEntry() { |
| 75 | return tfe; |
| 76 | } |
| 77 | public Load load() { |
| 78 | return load; |
| 79 | } |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Returns current typed flow entry's polling interval. |
| 83 | * |
| 84 | * @param tfe typed flow entry |
| 85 | * @return typed poll interval |
| 86 | */ |
| 87 | public static long typedPollInterval(TypedStoredFlowEntry tfe) { |
| 88 | checkNotNull(tfe, "TypedStoredFlowEntry cannot be null"); |
| 89 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 90 | PollInterval pollIntervalInstance = PollInterval.getInstance(); |
| 91 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 92 | switch (tfe.flowLiveType()) { |
| 93 | case LONG_FLOW: |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 94 | return pollIntervalInstance.getLongPollInterval(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 95 | case MID_FLOW: |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 96 | return pollIntervalInstance.getMidPollInterval(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 97 | case SHORT_FLOW: |
| 98 | case IMMEDIATE_FLOW: |
| 99 | default: |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 100 | return pollIntervalInstance.getPollInterval(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates a new typed flow entry with the given flow entry fe. |
| 106 | * |
| 107 | * @param fe flow entry |
| 108 | * @return new typed flow entry |
| 109 | */ |
| 110 | public static TypedStoredFlowEntry newTypedStoredFlowEntry(FlowEntry fe) { |
| 111 | if (fe == null) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | long life = fe.life(); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 116 | PollInterval pollIntervalInstance = PollInterval.getInstance(); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 117 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 118 | if (life < 0) { |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 119 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 120 | } else if (life < pollIntervalInstance.getPollInterval()) { |
| 121 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW); |
| 122 | } else if (life < pollIntervalInstance.getMidPollInterval()) { |
| 123 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW); |
| 124 | } else if (life < pollIntervalInstance.getLongPollInterval()) { |
| 125 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.MID_FLOW); |
| 126 | } else { // >= longPollInterval |
| 127 | return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.LONG_FLOW); |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |