blob: 04d78ae192297033c7652324a5cda20eedece7a5 [file] [log] [blame]
Madan Jampanic27b6b22016-02-05 11:36:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampanic27b6b22016-02-05 11:36:31 -08003 *
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
17package org.onosproject.net.statistic;
18
19import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.flow.FlowEntry;
21import org.onosproject.net.flow.TypedStoredFlowEntry;
22import org.onosproject.net.flow.DefaultTypedFlowEntry;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Load of flow entry of flow live type.
28 */
29public class TypedFlowEntryWithLoad {
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090030 private final ConnectPoint cp;
31 private final TypedStoredFlowEntry tfe;
32 private final Load load;
Madan Jampanic27b6b22016-02-05 11:36:31 -080033
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 Jampanic27b6b22016-02-05 11:36:31 -080080
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 Yoonb1b823f2016-05-16 18:55:39 +090090 PollInterval pollIntervalInstance = PollInterval.getInstance();
91
Madan Jampanic27b6b22016-02-05 11:36:31 -080092 switch (tfe.flowLiveType()) {
93 case LONG_FLOW:
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090094 return pollIntervalInstance.getLongPollInterval();
Madan Jampanic27b6b22016-02-05 11:36:31 -080095 case MID_FLOW:
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090096 return pollIntervalInstance.getMidPollInterval();
Madan Jampanic27b6b22016-02-05 11:36:31 -080097 case SHORT_FLOW:
98 case IMMEDIATE_FLOW:
99 default:
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900100 return pollIntervalInstance.getPollInterval();
Madan Jampanic27b6b22016-02-05 11:36:31 -0800101 }
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 Yoonb1b823f2016-05-16 18:55:39 +0900116 PollInterval pollIntervalInstance = PollInterval.getInstance();
Madan Jampanic27b6b22016-02-05 11:36:31 -0800117
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900118 if (life < 0) {
Madan Jampanic27b6b22016-02-05 11:36:31 -0800119 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW);
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900120 } 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 Jampanic27b6b22016-02-05 11:36:31 -0800128 }
129 }
130}