summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
blob: b27a5074a19acdc487858f877db51f1fc788209a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright 2014-2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.net.intent;

import com.google.common.annotations.Beta;
import org.onosproject.event.AbstractEvent;

/**
 * A class to represent an intent related event.
 */
@Beta
public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {

    public enum Type {
        /**
         * Signifies that an intent is to be installed or reinstalled.
         */
        INSTALL_REQ,

        /**
         * Signifies that an intent has been successfully installed.
         */
        INSTALLED,

        /**
         * Signifies that an intent has failed compilation and that it cannot
         * be satisfied by the network at this time.
         */
        FAILED,

        /**
         * Signifies that an intent will be withdrawn.
         */
        WITHDRAW_REQ,

        /**
         * Signifies that an intent has been withdrawn from the system.
         */
        WITHDRAWN,

        /**
         * Signifies that an intent has failed installation or withdrawal, but
         * still hold some or all of its resources.
         * (e.g. link reservations, flow rules on the data plane, etc.)
         */
        CORRUPT,

        /**
         * Signifies that an intent has been purged from the system.
         */
        PURGED
    }

    /**
     * Creates an event of a given type and for the specified intent and the
     * current time.
     *
     * @param type   event type
     * @param intent subject intent
     * @param time   time the event created in milliseconds since start of epoch
     */
    public IntentEvent(Type type, Intent intent, long time) {
        super(type, intent, time);
    }

    /**
     * Creates an event of a given type and for the specified intent and the
     * current time.
     *
     * @param type   event type
     * @param intent subject intent
     */
    public IntentEvent(Type type, Intent intent) {
        super(type, intent);
    }

    /**
     * Creates an IntentEvent based on the state contained in the given intent
     * data. Some states are not sent as external events, and these states will
     * return null events.
     *
     * @param data the intent data to create an event for
     * @return new intent event if the state is valid, otherwise null.
     */
    public static IntentEvent getEvent(IntentData data) {
        return getEvent(data.state(), data.intent());
    }

    /**
     * Creates an IntentEvent based on the given state and intent. Some states
     * are not sent as external events, and these states will return null events.
     *
     * @param state new state of the intent
     * @param intent intent to put in event
     * @return new intent event if the state is valid, otherwise null.
     */
    public static IntentEvent getEvent(IntentState state, Intent intent) {
        Type type;
        switch (state) {
            case INSTALL_REQ:
                type = Type.INSTALL_REQ;
                break;
            case INSTALLED:
                type = Type.INSTALLED;
                break;
            case WITHDRAW_REQ:
                type = Type.WITHDRAW_REQ;
                break;
            case WITHDRAWN:
                type = Type.WITHDRAWN;
                break;
            case FAILED:
                type = Type.FAILED;
                break;
            case CORRUPT:
                type = Type.CORRUPT;
                break;
            case PURGE_REQ:
                type = Type.PURGED;
                break;

            // fallthrough to default from here
            case COMPILING:
            case INSTALLING:
            case RECOMPILING:
            case WITHDRAWING:
            default:
                return null;
        }
        return new IntentEvent(type, intent);
    }

}