blob: 206552e8e2a8426187b808f253db200f726f78c3 [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;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070019import org.onlab.onos.net.NetworkResource;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070020import org.onlab.onos.net.flow.BatchOperationTarget;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070021
22import java.util.Collection;
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
Brian O'Connorb876bf12014-10-02 14:59:37 -070027/**
28 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070029 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070030 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070031 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070032 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070033public abstract class Intent implements BatchOperationTarget {
34
35 private final IntentId id;
36 private final ApplicationId appId;
37 private final Collection<NetworkResource> resources;
38
39 /**
40 * Constructor for serializer.
41 */
42 protected Intent() {
43 this.id = null;
44 this.appId = null;
45 this.resources = null;
46 }
47
48 /**
49 * Creates a new intent.
50 *
51 * @param id intent identifier
52 * @param appId application identifier
53 * @param resources required network resources (optional)
54 */
55 protected Intent(IntentId id, ApplicationId appId,
56 Collection<NetworkResource> resources) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070057 this.id = checkNotNull(id, "Intent ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070058 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070059 this.resources = resources;
60 }
61
Brian O'Connorb876bf12014-10-02 14:59:37 -070062 /**
63 * Returns the intent identifier.
64 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070065 * @return intent fingerprint
66 */
67 public IntentId id() {
68 return id;
69 }
70
71 /**
72 * Returns the identifier of the application that requested the intent.
73 *
74 * @return application identifier
75 */
76 public ApplicationId appId() {
77 return appId;
78 }
79
80 /**
81 * Returns the collection of resources required for this intent.
82 *
83 * @return collection of resources; may be null
84 */
85 public Collection<NetworkResource> resources() {
86 return resources;
87 }
88
89 /**
90 * Produces an intent identifier backed by hash-like fingerprint for the
91 * specified class of intent and its constituent fields.
92 *
93 * @param fields intent fields
Brian O'Connorb876bf12014-10-02 14:59:37 -070094 * @return intent identifier
95 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070096 protected static IntentId id(Object... fields) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070097 // FIXME: spread the bits across the full long spectrum
Thomas Vachuskac96058a2014-10-20 23:00:16 -070098 return IntentId.valueOf(Objects.hash(fields));
99 }
100
101 /**
102 * Indicates whether or not the intent is installable.
103 *
104 * @return true if installable
105 */
106 public boolean isInstallable() {
107 return false;
108 }
109
110 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700111 public final int hashCode() {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700112 return Objects.hash(id);
113 }
114
115 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700116 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700117 if (this == obj) {
118 return true;
119 }
120 if (obj == null || getClass() != obj.getClass()) {
121 return false;
122 }
123 final Intent other = (Intent) obj;
124 return Objects.equals(this.id, other.id);
125 }
126
Brian O'Connorb876bf12014-10-02 14:59:37 -0700127}