blob: 885b8517a2c0267af347c70aa36c5cd4146b58cd [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
Brian O'Connor41718fc2014-10-30 16:57:21 -070022import java.util.Arrays;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070023import java.util.Collection;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
Brian O'Connorb876bf12014-10-02 14:59:37 -070028/**
29 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070030 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070031 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070032 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070034public abstract class Intent implements BatchOperationTarget {
35
36 private final IntentId id;
37 private final ApplicationId appId;
38 private final Collection<NetworkResource> resources;
39
40 /**
41 * Constructor for serializer.
42 */
43 protected Intent() {
44 this.id = null;
45 this.appId = null;
46 this.resources = null;
47 }
48
49 /**
50 * Creates a new intent.
51 *
52 * @param id intent identifier
53 * @param appId application identifier
54 * @param resources required network resources (optional)
55 */
56 protected Intent(IntentId id, ApplicationId appId,
57 Collection<NetworkResource> resources) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070058 this.id = checkNotNull(id, "Intent ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070059 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070060 this.resources = resources;
61 }
62
Brian O'Connorb876bf12014-10-02 14:59:37 -070063 /**
64 * Returns the intent identifier.
65 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070066 * @return intent fingerprint
67 */
68 public IntentId id() {
69 return id;
70 }
71
72 /**
73 * Returns the identifier of the application that requested the intent.
74 *
75 * @return application identifier
76 */
77 public ApplicationId appId() {
78 return appId;
79 }
80
81 /**
82 * Returns the collection of resources required for this intent.
83 *
84 * @return collection of resources; may be null
85 */
86 public Collection<NetworkResource> resources() {
87 return resources;
88 }
89
90 /**
91 * Produces an intent identifier backed by hash-like fingerprint for the
92 * specified class of intent and its constituent fields.
93 *
94 * @param fields intent fields
Brian O'Connorb876bf12014-10-02 14:59:37 -070095 * @return intent identifier
96 */
Brian O'Connor41718fc2014-10-30 16:57:21 -070097 protected static IntentId id(Class<?> intentClass, Object... fields) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070098 // FIXME: spread the bits across the full long spectrum
Brian O'Connor41718fc2014-10-30 16:57:21 -070099 return IntentId.valueOf(Objects.hash(intentClass.getName(),
100 Arrays.hashCode(fields)));
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700101 }
102
103 /**
104 * Indicates whether or not the intent is installable.
105 *
106 * @return true if installable
107 */
108 public boolean isInstallable() {
109 return false;
110 }
111
112 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700113 public final int hashCode() {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700114 return Objects.hash(id);
115 }
116
117 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700118 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700119 if (this == obj) {
120 return true;
121 }
122 if (obj == null || getClass() != obj.getClass()) {
123 return false;
124 }
125 final Intent other = (Intent) obj;
126 return Objects.equals(this.id, other.id);
127 }
128
Brian O'Connorb876bf12014-10-02 14:59:37 -0700129}