Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.event.AbstractEvent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 19 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 20 | /** |
| 21 | * A class to represent an intent related event. |
| 22 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 23 | public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 24 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 25 | public enum Type { |
| 26 | /** |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 27 | * Signifies that an intent is to be installed or reinstalled. |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 28 | */ |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 29 | INSTALL_REQ, |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Signifies that an intent has been successfully installed. |
| 33 | */ |
| 34 | INSTALLED, |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 35 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Signifies that an intent has failed compilation or installation. |
| 38 | */ |
| 39 | FAILED, |
| 40 | |
| 41 | /** |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 42 | * Signifies that an intent will be withdrawn. |
| 43 | */ |
| 44 | WITHDRAW_REQ, |
| 45 | |
| 46 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 47 | * Signifies that an intent has been withdrawn from the system. |
| 48 | */ |
| 49 | WITHDRAWN |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 53 | * Creates an event of a given type and for the specified intent and the |
| 54 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 55 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 56 | * @param type event type |
| 57 | * @param intent subject intent |
| 58 | * @param time time the event created in milliseconds since start of epoch |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 59 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 60 | public IntentEvent(Type type, Intent intent, long time) { |
| 61 | super(type, intent, time); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 65 | * Creates an event of a given type and for the specified intent and the |
| 66 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 67 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 68 | * @param type event type |
| 69 | * @param intent subject intent |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 70 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 71 | public IntentEvent(Type type, Intent intent) { |
| 72 | super(type, intent); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Creates an IntentEvent based on the state contained in the given intent |
| 77 | * data. Some states are not sent as external events, and these states will |
| 78 | * return null events. |
| 79 | * |
| 80 | * @param data the intent data to create an event for |
| 81 | * @return new intent event if the state is valid, otherwise null. |
| 82 | */ |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 83 | public static IntentEvent getEvent(IntentData data) { |
| 84 | return getEvent(data.state(), data.intent()); |
| 85 | } |
| 86 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Creates an IntentEvent based on the given state and intent. Some states |
| 89 | * are not sent as external events, and these states will return null events. |
| 90 | * |
| 91 | * @param state new state of the intent |
| 92 | * @param intent intent to put in event |
| 93 | * @return new intent event if the state is valid, otherwise null. |
| 94 | */ |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 95 | public static IntentEvent getEvent(IntentState state, Intent intent) { |
| 96 | Type type; |
| 97 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 98 | case INSTALL_REQ: |
| 99 | type = Type.INSTALL_REQ; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 100 | break; |
| 101 | case INSTALLED: |
| 102 | type = Type.INSTALLED; |
| 103 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 104 | case WITHDRAW_REQ: |
| 105 | type = Type.WITHDRAW_REQ; |
| 106 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 107 | case WITHDRAWN: |
| 108 | type = Type.WITHDRAWN; |
| 109 | break; |
| 110 | case FAILED: |
| 111 | type = Type.FAILED; |
| 112 | break; |
| 113 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 114 | // fallthrough to default from here |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 115 | case COMPILING: |
| 116 | case INSTALLING: |
| 117 | case RECOMPILING: |
| 118 | case WITHDRAWING: |
| 119 | default: |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 120 | return null; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 121 | } |
| 122 | return new IntentEvent(type, intent); |
| 123 | } |
| 124 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 125 | } |