Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | 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 | */ |
| 66 | PURGED |
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 |
| 75 | * @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] | 76 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 77 | public IntentEvent(Type type, Intent intent, long time) { |
| 78 | super(type, intent, time); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 82 | * Creates an event of a given type and for the specified intent and the |
| 83 | * current time. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 84 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 85 | * @param type event type |
| 86 | * @param intent subject intent |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 87 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 88 | public IntentEvent(Type type, Intent intent) { |
| 89 | super(type, intent); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Creates an IntentEvent based on the state contained in the given intent |
| 94 | * data. Some states are not sent as external events, and these states will |
| 95 | * return null events. |
| 96 | * |
| 97 | * @param data the intent data to create an event for |
| 98 | * @return new intent event if the state is valid, otherwise null. |
| 99 | */ |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 100 | public static Optional<IntentEvent> getEvent(IntentData data) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 101 | return getEvent(data.state(), data.intent()); |
| 102 | } |
| 103 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Creates an IntentEvent based on the given state and intent. Some states |
| 106 | * are not sent as external events, and these states will return null events. |
| 107 | * |
| 108 | * @param state new state of the intent |
| 109 | * @param intent intent to put in event |
| 110 | * @return new intent event if the state is valid, otherwise null. |
| 111 | */ |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 112 | public static Optional<IntentEvent> getEvent(IntentState state, Intent intent) { |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 113 | Type type; |
| 114 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 115 | case INSTALL_REQ: |
| 116 | type = Type.INSTALL_REQ; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 117 | break; |
| 118 | case INSTALLED: |
| 119 | type = Type.INSTALLED; |
| 120 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 121 | case WITHDRAW_REQ: |
| 122 | type = Type.WITHDRAW_REQ; |
| 123 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 124 | case WITHDRAWN: |
| 125 | type = Type.WITHDRAWN; |
| 126 | break; |
| 127 | case FAILED: |
| 128 | type = Type.FAILED; |
| 129 | break; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 130 | case CORRUPT: |
| 131 | type = Type.CORRUPT; |
| 132 | break; |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 133 | case PURGE_REQ: |
| 134 | type = Type.PURGED; |
| 135 | break; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 136 | |
Jonathan Hart | 9288836 | 2015-02-13 13:14:59 -0800 | [diff] [blame] | 137 | // fallthrough to default from here |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 138 | case COMPILING: |
| 139 | case INSTALLING: |
| 140 | case RECOMPILING: |
| 141 | case WITHDRAWING: |
| 142 | default: |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 143 | return Optional.empty(); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 144 | } |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 145 | return Optional.of(new IntentEvent(type, intent)); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 148 | } |