blob: b57e375d7ee92ca31df9d1e9b77b88b9caccac2b [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070016package org.onlab.onos.net.intent;
17
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070018import org.onlab.onos.core.ApplicationId;
Brian O'Connor520c0522014-11-23 23:50:47 -080019import org.onlab.onos.core.IdGenerator;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070020import org.onlab.onos.net.NetworkResource;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070021import org.onlab.onos.net.flow.BatchOperationTarget;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070022
23import java.util.Collection;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connor520c0522014-11-23 23:50:47 -080027import static com.google.common.base.Preconditions.checkState;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070028
Brian O'Connorb876bf12014-10-02 14:59:37 -070029/**
30 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070031 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070032 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070033 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070034 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070035public abstract class Intent implements BatchOperationTarget {
36
37 private final IntentId id;
38 private final ApplicationId appId;
39 private final Collection<NetworkResource> resources;
40
Brian O'Connor520c0522014-11-23 23:50:47 -080041 private static IdGenerator idGenerator;
42
Thomas Vachuskac96058a2014-10-20 23:00:16 -070043 /**
44 * Constructor for serializer.
45 */
46 protected Intent() {
47 this.id = null;
48 this.appId = null;
49 this.resources = null;
50 }
51
52 /**
53 * Creates a new intent.
54 *
Brian O'Connor520c0522014-11-23 23:50:47 -080055 * @param appId application identifier
56 * @param resources required network resources (optional)
Thomas Vachuskac96058a2014-10-20 23:00:16 -070057 */
Brian O'Connor520c0522014-11-23 23:50:47 -080058 protected Intent(ApplicationId appId,
Thomas Vachuskac96058a2014-10-20 23:00:16 -070059 Collection<NetworkResource> resources) {
Brian O'Connor520c0522014-11-23 23:50:47 -080060 checkState(idGenerator != null, "Id generator is not bound.");
61 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070062 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070063 this.resources = resources;
64 }
65
Brian O'Connorb876bf12014-10-02 14:59:37 -070066 /**
67 * Returns the intent identifier.
68 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070069 * @return intent fingerprint
70 */
71 public IntentId id() {
72 return id;
73 }
74
75 /**
76 * Returns the identifier of the application that requested the intent.
77 *
78 * @return application identifier
79 */
80 public ApplicationId appId() {
81 return appId;
82 }
83
84 /**
85 * Returns the collection of resources required for this intent.
86 *
87 * @return collection of resources; may be null
88 */
89 public Collection<NetworkResource> resources() {
90 return resources;
91 }
92
93 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070094 * Indicates whether or not the intent is installable.
95 *
96 * @return true if installable
97 */
98 public boolean isInstallable() {
99 return false;
100 }
101
102 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700103 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800104 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700105 }
106
107 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700108 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700109 if (this == obj) {
110 return true;
111 }
112 if (obj == null || getClass() != obj.getClass()) {
113 return false;
114 }
115 final Intent other = (Intent) obj;
Brian O'Connor520c0522014-11-23 23:50:47 -0800116 return this.id().equals(((Intent) obj).id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700117 }
118
Brian O'Connor520c0522014-11-23 23:50:47 -0800119 /**
120 * Binds an id generator for unique intent id generation.
121 *
122 * Note: A generator cannot be bound if there is already a generator bound.
123 * @param newIdGenerator id generator
124 */
125 public static final void bindIdGenerator(IdGenerator newIdGenerator) {
126 checkState(idGenerator == null, "Id generator is already bound.");
127 idGenerator = checkNotNull(newIdGenerator);
128 }
129
130 /**
131 * Unbinds an id generator.
132 *
133 * Note: The caller must provide the old id generator to succeed.
134 * @param oldIdGenerator the current id generator
135 */
136 public static final void unbindIdGenerator(IdGenerator oldIdGenerator) {
137 if (Objects.equals(idGenerator, oldIdGenerator)) {
138 idGenerator = null;
139 }
140 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700141}