blob: 47aa94d1d721ca904b9670acebdd6102fc078c89 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.event.AbstractEvent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070020
Sho SHIMIZU96b39e22016-01-15 17:35:04 -080021import java.util.Optional;
22
Brian O'Connorb876bf12014-10-02 14:59:37 -070023/**
24 * A class to represent an intent related event.
25 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040026@Beta
tom85258ee2014-10-07 00:10:02 -070027public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
Brian O'Connorb876bf12014-10-02 14:59:37 -070028
tom85258ee2014-10-07 00:10:02 -070029 public enum Type {
30 /**
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080031 * Signifies that an intent is to be installed or reinstalled.
tom85258ee2014-10-07 00:10:02 -070032 */
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080033 INSTALL_REQ,
Brian O'Connorb876bf12014-10-02 14:59:37 -070034
tom85258ee2014-10-07 00:10:02 -070035 /**
36 * Signifies that an intent has been successfully installed.
37 */
38 INSTALLED,
Brian O'Connorb876bf12014-10-02 14:59:37 -070039
tom85258ee2014-10-07 00:10:02 -070040 /**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070041 * Signifies that an intent has failed compilation and that it cannot
42 * be satisfied by the network at this time.
tom85258ee2014-10-07 00:10:02 -070043 */
44 FAILED,
45
46 /**
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080047 * Signifies that an intent will be withdrawn.
48 */
49 WITHDRAW_REQ,
50
51 /**
tom85258ee2014-10-07 00:10:02 -070052 * Signifies that an intent has been withdrawn from the system.
53 */
Ray Milkey8c6d00e2015-03-13 14:14:34 -070054 WITHDRAWN,
55
56 /**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070057 * Signifies that an intent has failed installation or withdrawal, but
58 * still hold some or all of its resources.
59 * (e.g. link reservations, flow rules on the data plane, etc.)
60 */
61 CORRUPT,
62
63 /**
Ray Milkey8c6d00e2015-03-13 14:14:34 -070064 * Signifies that an intent has been purged from the system.
65 */
Antonio Marsico4f68ec92017-03-09 11:16:32 +010066 PURGED,
67
68 /**
69 * Signifies that an intent is being reallocated.
70 */
71 REALLOCATING
Brian O'Connorb876bf12014-10-02 14:59:37 -070072 }
73
74 /**
tom85258ee2014-10-07 00:10:02 -070075 * Creates an event of a given type and for the specified intent and the
76 * current time.
Brian O'Connorb876bf12014-10-02 14:59:37 -070077 *
tom85258ee2014-10-07 00:10:02 -070078 * @param type event type
79 * @param intent subject intent
80 * @param time time the event created in milliseconds since start of epoch
Brian O'Connorb876bf12014-10-02 14:59:37 -070081 */
tom85258ee2014-10-07 00:10:02 -070082 public IntentEvent(Type type, Intent intent, long time) {
83 super(type, intent, time);
Brian O'Connorb876bf12014-10-02 14:59:37 -070084 }
85
86 /**
tom85258ee2014-10-07 00:10:02 -070087 * Creates an event of a given type and for the specified intent and the
88 * current time.
Brian O'Connorb876bf12014-10-02 14:59:37 -070089 *
tom85258ee2014-10-07 00:10:02 -070090 * @param type event type
91 * @param intent subject intent
Brian O'Connorb876bf12014-10-02 14:59:37 -070092 */
tom85258ee2014-10-07 00:10:02 -070093 public IntentEvent(Type type, Intent intent) {
94 super(type, intent);
Brian O'Connorb876bf12014-10-02 14:59:37 -070095 }
96
Jonathan Hart92888362015-02-13 13:14:59 -080097 /**
98 * Creates an IntentEvent based on the state contained in the given intent
99 * data. Some states are not sent as external events, and these states will
100 * return null events.
101 *
102 * @param data the intent data to create an event for
103 * @return new intent event if the state is valid, otherwise null.
104 */
Sho SHIMIZU96b39e22016-01-15 17:35:04 -0800105 public static Optional<IntentEvent> getEvent(IntentData data) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800106 return getEvent(data.state(), data.intent());
107 }
108
Jonathan Hart92888362015-02-13 13:14:59 -0800109 /**
110 * Creates an IntentEvent based on the given state and intent. Some states
111 * are not sent as external events, and these states will return null events.
112 *
113 * @param state new state of the intent
114 * @param intent intent to put in event
115 * @return new intent event if the state is valid, otherwise null.
116 */
Sho SHIMIZU96b39e22016-01-15 17:35:04 -0800117 public static Optional<IntentEvent> getEvent(IntentState state, Intent intent) {
alshabiba9819bf2014-11-30 18:15:52 -0800118 Type type;
119 switch (state) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800120 case INSTALL_REQ:
121 type = Type.INSTALL_REQ;
alshabiba9819bf2014-11-30 18:15:52 -0800122 break;
123 case INSTALLED:
124 type = Type.INSTALLED;
125 break;
Antonio Marsico4f68ec92017-03-09 11:16:32 +0100126 case REALLOCATING:
127 type = Type.REALLOCATING;
128 break;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800129 case WITHDRAW_REQ:
130 type = Type.WITHDRAW_REQ;
131 break;
alshabiba9819bf2014-11-30 18:15:52 -0800132 case WITHDRAWN:
133 type = Type.WITHDRAWN;
134 break;
135 case FAILED:
136 type = Type.FAILED;
137 break;
Brian O'Connorf0c5a052015-04-27 00:34:53 -0700138 case CORRUPT:
139 type = Type.CORRUPT;
140 break;
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700141 case PURGE_REQ:
142 type = Type.PURGED;
143 break;
alshabiba9819bf2014-11-30 18:15:52 -0800144
Jonathan Hart92888362015-02-13 13:14:59 -0800145 // fallthrough to default from here
alshabiba9819bf2014-11-30 18:15:52 -0800146 case COMPILING:
147 case INSTALLING:
148 case RECOMPILING:
149 case WITHDRAWING:
150 default:
Sho SHIMIZU96b39e22016-01-15 17:35:04 -0800151 return Optional.empty();
alshabiba9819bf2014-11-30 18:15:52 -0800152 }
Sho SHIMIZU96b39e22016-01-15 17:35:04 -0800153 return Optional.of(new IntentEvent(type, intent));
alshabiba9819bf2014-11-30 18:15:52 -0800154 }
155
Brian O'Connorb876bf12014-10-02 14:59:37 -0700156}