blob: 3e2dbdf89a85e2b689d3d074e4818ebd1d7767ab [file] [log] [blame]
ssyoon90a98825a2015-08-26 00:48:15 +09001/*
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
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 {
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 public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe, Load load) {
41 this.cp = cp;
42 this.tfe = tfe;
43 this.load = load;
44 }
45
46 public TypedFlowEntryWithLoad(ConnectPoint cp, TypedStoredFlowEntry tfe) {
47 this.cp = cp;
48 this.tfe = tfe;
49 this.load = new DefaultLoad(tfe.bytes(), 0, typedPollInterval(tfe));
50 }
51
52 public TypedFlowEntryWithLoad(ConnectPoint cp, FlowEntry fe) {
53 this.cp = cp;
54 this.tfe = newTypedStoredFlowEntry(fe);
55 this.load = new DefaultLoad(fe.bytes(), 0, typedPollInterval(this.tfe));
56 }
57
58 public ConnectPoint connectPoint() {
59 return cp;
60 }
61 public TypedStoredFlowEntry typedStoredFlowEntry() {
62 return tfe;
63 }
64 public Load load() {
65 return load;
66 }
67 public void setLoad(Load load) {
68 this.load = load;
69 }
70
71 /**
72 * Returns short polling interval.
73 */
74 public static int shortPollInterval() {
75 return CAL_AND_POLL_INTERVAL;
76 }
77
78 /**
79 * Returns mid polling interval.
80 */
81 public static int midPollInterval() {
82 return MID_POLL_INTERVAL;
83 }
84
85 /**
86 * Returns long polling interval.
87 */
88 public static int longPollInterval() {
89 return LONG_POLL_INTERVAL;
90 }
91
92 /**
93 * Returns average polling interval.
94 */
95 public static int avgPollInterval() {
96 return (CAL_AND_POLL_INTERVAL + MID_POLL_INTERVAL + LONG_POLL_INTERVAL) / 3;
97 }
98
99 /**
100 * Returns current typed flow entry's polling interval.
101 *
102 * @param tfe typed flow entry
103 */
104 public static long typedPollInterval(TypedStoredFlowEntry tfe) {
105 checkNotNull(tfe, "TypedStoredFlowEntry cannot be null");
106
107 switch (tfe.flowLiveType()) {
108 case LONG_FLOW:
109 return LONG_POLL_INTERVAL;
110 case MID_FLOW:
111 return MID_POLL_INTERVAL;
112 case SHORT_FLOW:
113 case IMMEDIATE_FLOW:
114 default:
115 return CAL_AND_POLL_INTERVAL;
116 }
117 }
118
119 /**
120 * Creates a new typed flow entry with the given flow entry fe.
121 *
122 * @param fe flow entry
123 */
124 public static TypedStoredFlowEntry newTypedStoredFlowEntry(FlowEntry fe) {
125 if (fe == null) {
126 return null;
127 }
128
129 long life = fe.life();
130
131 if (life >= LONG_POLL_INTERVAL) {
132 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.LONG_FLOW);
133 } else if (life >= MID_POLL_INTERVAL) {
134 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.MID_FLOW);
135 } else if (life >= CAL_AND_POLL_INTERVAL) {
136 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.SHORT_FLOW);
137 } else if (life >= 0) {
138 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.IMMEDIATE_FLOW);
139 } else { // life < 0
140 return new DefaultTypedFlowEntry(fe, TypedStoredFlowEntry.FlowLiveType.UNKNOWN_FLOW);
141 }
142 }
143}