Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.event.AbstractEvent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 20 | |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 21 | import java.util.Optional; |
| 22 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 23 | /** |
| 24 | * A class to represent an intent related event. |
| 25 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 26 | @Beta |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 27 | public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 28 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 29 | public enum Type { |
| 30 | /** |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 31 | * Signifies that an intent is to be installed or reinstalled. |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 32 | */ |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 33 | INSTALL_REQ, |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Signifies that an intent has been successfully installed. |
| 37 | */ |
| 38 | INSTALLED, |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 39 | |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 40 | /** |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 41 | * Signifies that an intent has failed compilation and that it cannot |
| 42 | * be satisfied by the network at this time. |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 43 | */ |
| 44 | FAILED, |
| 45 | |
| 46 | /** |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 47 | * Signifies that an intent will be withdrawn. |
| 48 | */ |
| 49 | WITHDRAW_REQ, |
| 50 | |
| 51 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 52 | * Signifies that an intent has been withdrawn from the system. |
| 53 | */ |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 54 | WITHDRAWN, |
| 55 | |
| 56 | /** |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 57 | * 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 Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 64 | * Signifies that an intent has been purged from the system. |
| 65 | */ |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 66 | PURGED, |
| 67 | |
| 68 | /** |
| 69 | * Signifies that an intent is being reallocated. |
| 70 | */ |
| 71 | REALLOCATING |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 75 | * Creates an event of a given type and for the specified intent and the |
| 76 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 77 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 78 | * @param type event type |
| 79 | * @param intent subject intent |
| 80 | * @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] | 81 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 82 | public IntentEvent(Type type, Intent intent, long time) { |
| 83 | super(type, intent, time); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 87 | * Creates an event of a given type and for the specified intent and the |
| 88 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 89 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 90 | * @param type event type |
| 91 | * @param intent subject intent |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 92 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 93 | public IntentEvent(Type type, Intent intent) { |
| 94 | super(type, intent); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 97 | /** |
| 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 SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 105 | public static Optional<IntentEvent> getEvent(IntentData data) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 106 | return getEvent(data.state(), data.intent()); |
| 107 | } |
| 108 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 109 | /** |
| 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 SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 117 | public static Optional<IntentEvent> getEvent(IntentState state, Intent intent) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 118 | Type type; |
| 119 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 120 | case INSTALL_REQ: |
| 121 | type = Type.INSTALL_REQ; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 122 | break; |
| 123 | case INSTALLED: |
| 124 | type = Type.INSTALLED; |
| 125 | break; |
Antonio Marsico | 4f68ec9 | 2017-03-09 11:16:32 +0100 | [diff] [blame] | 126 | case REALLOCATING: |
| 127 | type = Type.REALLOCATING; |
| 128 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 129 | case WITHDRAW_REQ: |
| 130 | type = Type.WITHDRAW_REQ; |
| 131 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 132 | case WITHDRAWN: |
| 133 | type = Type.WITHDRAWN; |
| 134 | break; |
| 135 | case FAILED: |
| 136 | type = Type.FAILED; |
| 137 | break; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 138 | case CORRUPT: |
| 139 | type = Type.CORRUPT; |
| 140 | break; |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 141 | case PURGE_REQ: |
| 142 | type = Type.PURGED; |
| 143 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 144 | |
Ray Milkey | 7dac7da | 2017-08-01 16:56:05 -0700 | [diff] [blame] | 145 | case COMPILING: // FALLTHROUGH |
| 146 | case INSTALLING: // FALLTHROUGH |
| 147 | case RECOMPILING: // FALLTHROUGH |
| 148 | case WITHDRAWING: // FALLTHROUGH |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 149 | default: |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 150 | return Optional.empty(); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 151 | } |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 152 | return Optional.of(new IntentEvent(type, intent)); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 155 | } |