blob: b5fefb2872f848661efe103a6c75cf0681147e34 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070019package org.onlab.onos.net.intent;
20
Thomas Vachuskac96058a2014-10-20 23:00:16 -070021import org.onlab.onos.ApplicationId;
22import org.onlab.onos.net.NetworkResource;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070023import org.onlab.onos.net.flow.BatchOperationTarget;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070024
25import java.util.Collection;
26import java.util.Objects;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
Brian O'Connorb876bf12014-10-02 14:59:37 -070030/**
31 * Abstraction of an application level intent.
toma1d16b62014-10-02 23:45:11 -070032 * <p/>
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 * Make sure that an Intent should be immutable when a new type is defined.
34 */
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
41 /**
42 * Constructor for serializer.
43 */
44 protected Intent() {
45 this.id = null;
46 this.appId = null;
47 this.resources = null;
48 }
49
50 /**
51 * Creates a new intent.
52 *
53 * @param id intent identifier
54 * @param appId application identifier
55 * @param resources required network resources (optional)
56 */
57 protected Intent(IntentId id, ApplicationId appId,
58 Collection<NetworkResource> resources) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070059 this.id = checkNotNull(id, "Intent ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070060 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070061 this.resources = resources;
62 }
63
Brian O'Connorb876bf12014-10-02 14:59:37 -070064 /**
65 * Returns the intent identifier.
66 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070067 * @return intent fingerprint
68 */
69 public IntentId id() {
70 return id;
71 }
72
73 /**
74 * Returns the identifier of the application that requested the intent.
75 *
76 * @return application identifier
77 */
78 public ApplicationId appId() {
79 return appId;
80 }
81
82 /**
83 * Returns the collection of resources required for this intent.
84 *
85 * @return collection of resources; may be null
86 */
87 public Collection<NetworkResource> resources() {
88 return resources;
89 }
90
91 /**
92 * Produces an intent identifier backed by hash-like fingerprint for the
93 * specified class of intent and its constituent fields.
94 *
95 * @param fields intent fields
Brian O'Connorb876bf12014-10-02 14:59:37 -070096 * @return intent identifier
97 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070098 protected static IntentId id(Object... fields) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070099 // FIXME: spread the bits across the full long spectrum
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700100 return IntentId.valueOf(Objects.hash(fields));
101 }
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}