Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | */ |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 49 | WITHDRAWN, |
| 50 | |
| 51 | /** |
| 52 | * Signifies that an intent has been purged from the system. |
| 53 | */ |
| 54 | PURGED |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 58 | * Creates an event of a given type and for the specified intent and the |
| 59 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 60 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 61 | * @param type event type |
| 62 | * @param intent subject intent |
| 63 | * @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] | 64 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 65 | public IntentEvent(Type type, Intent intent, long time) { |
| 66 | super(type, intent, time); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 70 | * Creates an event of a given type and for the specified intent and the |
| 71 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 72 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 73 | * @param type event type |
| 74 | * @param intent subject intent |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 75 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 76 | public IntentEvent(Type type, Intent intent) { |
| 77 | super(type, intent); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 80 | /** |
| 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'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 88 | public static IntentEvent getEvent(IntentData data) { |
| 89 | return getEvent(data.state(), data.intent()); |
| 90 | } |
| 91 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 92 | /** |
| 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 | */ |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 100 | public static IntentEvent getEvent(IntentState state, Intent intent) { |
| 101 | Type type; |
| 102 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 103 | case INSTALL_REQ: |
| 104 | type = Type.INSTALL_REQ; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 105 | break; |
| 106 | case INSTALLED: |
| 107 | type = Type.INSTALLED; |
| 108 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 109 | case WITHDRAW_REQ: |
| 110 | type = Type.WITHDRAW_REQ; |
| 111 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 112 | case WITHDRAWN: |
| 113 | type = Type.WITHDRAWN; |
| 114 | break; |
| 115 | case FAILED: |
| 116 | type = Type.FAILED; |
| 117 | break; |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 118 | case PURGE_REQ: |
| 119 | type = Type.PURGED; |
| 120 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 121 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 122 | // fallthrough to default from here |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 123 | case COMPILING: |
| 124 | case INSTALLING: |
| 125 | case RECOMPILING: |
| 126 | case WITHDRAWING: |
| 127 | default: |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 128 | return null; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 129 | } |
| 130 | return new IntentEvent(type, intent); |
| 131 | } |
| 132 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 133 | } |