blob: 792787c51d2d27b19ba57774e7d646bbcd08771a [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.event.AbstractEvent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070019
Brian O'Connorb876bf12014-10-02 14:59:37 -070020/**
21 * A class to represent an intent related event.
22 */
tom85258ee2014-10-07 00:10:02 -070023public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
Brian O'Connorb876bf12014-10-02 14:59:37 -070024
tom85258ee2014-10-07 00:10:02 -070025 public enum Type {
26 /**
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080027 * Signifies that an intent is to be installed or reinstalled.
tom85258ee2014-10-07 00:10:02 -070028 */
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080029 INSTALL_REQ,
Brian O'Connorb876bf12014-10-02 14:59:37 -070030
tom85258ee2014-10-07 00:10:02 -070031 /**
32 * Signifies that an intent has been successfully installed.
33 */
34 INSTALLED,
Brian O'Connorb876bf12014-10-02 14:59:37 -070035
tom85258ee2014-10-07 00:10:02 -070036 /**
37 * Signifies that an intent has failed compilation or installation.
38 */
39 FAILED,
40
41 /**
Brian O'Connor7a71d5d2014-12-02 00:12:27 -080042 * Signifies that an intent will be withdrawn.
43 */
44 WITHDRAW_REQ,
45
46 /**
tom85258ee2014-10-07 00:10:02 -070047 * Signifies that an intent has been withdrawn from the system.
48 */
Ray Milkey8c6d00e2015-03-13 14:14:34 -070049 WITHDRAWN,
50
51 /**
52 * Signifies that an intent has been purged from the system.
53 */
54 PURGED
Brian O'Connorb876bf12014-10-02 14:59:37 -070055 }
56
57 /**
tom85258ee2014-10-07 00:10:02 -070058 * Creates an event of a given type and for the specified intent and the
59 * current time.
Brian O'Connorb876bf12014-10-02 14:59:37 -070060 *
tom85258ee2014-10-07 00:10:02 -070061 * @param type event type
62 * @param intent subject intent
63 * @param time time the event created in milliseconds since start of epoch
Brian O'Connorb876bf12014-10-02 14:59:37 -070064 */
tom85258ee2014-10-07 00:10:02 -070065 public IntentEvent(Type type, Intent intent, long time) {
66 super(type, intent, time);
Brian O'Connorb876bf12014-10-02 14:59:37 -070067 }
68
69 /**
tom85258ee2014-10-07 00:10:02 -070070 * Creates an event of a given type and for the specified intent and the
71 * current time.
Brian O'Connorb876bf12014-10-02 14:59:37 -070072 *
tom85258ee2014-10-07 00:10:02 -070073 * @param type event type
74 * @param intent subject intent
Brian O'Connorb876bf12014-10-02 14:59:37 -070075 */
tom85258ee2014-10-07 00:10:02 -070076 public IntentEvent(Type type, Intent intent) {
77 super(type, intent);
Brian O'Connorb876bf12014-10-02 14:59:37 -070078 }
79
Jonathan Hart92888362015-02-13 13:14:59 -080080 /**
81 * Creates an IntentEvent based on the state contained in the given intent
82 * data. Some states are not sent as external events, and these states will
83 * return null events.
84 *
85 * @param data the intent data to create an event for
86 * @return new intent event if the state is valid, otherwise null.
87 */
Brian O'Connor03406a42015-02-03 17:28:57 -080088 public static IntentEvent getEvent(IntentData data) {
89 return getEvent(data.state(), data.intent());
90 }
91
Jonathan Hart92888362015-02-13 13:14:59 -080092 /**
93 * Creates an IntentEvent based on the given state and intent. Some states
94 * are not sent as external events, and these states will return null events.
95 *
96 * @param state new state of the intent
97 * @param intent intent to put in event
98 * @return new intent event if the state is valid, otherwise null.
99 */
alshabiba9819bf2014-11-30 18:15:52 -0800100 public static IntentEvent getEvent(IntentState state, Intent intent) {
101 Type type;
102 switch (state) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800103 case INSTALL_REQ:
104 type = Type.INSTALL_REQ;
alshabiba9819bf2014-11-30 18:15:52 -0800105 break;
106 case INSTALLED:
107 type = Type.INSTALLED;
108 break;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800109 case WITHDRAW_REQ:
110 type = Type.WITHDRAW_REQ;
111 break;
alshabiba9819bf2014-11-30 18:15:52 -0800112 case WITHDRAWN:
113 type = Type.WITHDRAWN;
114 break;
115 case FAILED:
116 type = Type.FAILED;
117 break;
Ray Milkey8c6d00e2015-03-13 14:14:34 -0700118 case PURGE_REQ:
119 type = Type.PURGED;
120 break;
alshabiba9819bf2014-11-30 18:15:52 -0800121
Jonathan Hart92888362015-02-13 13:14:59 -0800122 // fallthrough to default from here
alshabiba9819bf2014-11-30 18:15:52 -0800123 case COMPILING:
124 case INSTALLING:
125 case RECOMPILING:
126 case WITHDRAWING:
127 default:
Jonathan Hart92888362015-02-13 13:14:59 -0800128 return null;
alshabiba9819bf2014-11-30 18:15:52 -0800129 }
130 return new IntentEvent(type, intent);
131 }
132
Brian O'Connorb876bf12014-10-02 14:59:37 -0700133}