Brian Stanke
Committed by Ray Milkey

ONOS-3655 - Initial submission for DeviceKey subsystem.

Change-Id: I14c834c65b68e49b80894efe12b1fa921fc430cc
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +package org.onosproject.incubator.net.key;
18 +
19 +/**
20 + * Representation of an SNMP community name authentication token.
21 + */
22 +public final class CommunityName {
23 + private final String name;
24 +
25 + /**
26 + * Private constructor for a community name.
27 + *
28 + * @param name community name
29 + */
30 + private CommunityName(String name) {
31 + this.name = name;
32 + }
33 +
34 + /**
35 + * Static method to construct a community name.
36 + *
37 + * @param name community name
38 + * @return community name
39 + */
40 + static CommunityName communityName(String name) {
41 + return new CommunityName(name);
42 + }
43 +
44 + /**
45 + * Returns the community name.
46 + *
47 + * @return name
48 + */
49 + public String name() {
50 + return name;
51 + }
52 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +package org.onosproject.incubator.net.key;
18 +
19 +import com.google.common.annotations.Beta;
20 +import org.onosproject.net.AbstractAnnotated;
21 +import org.onosproject.net.AnnotationKeys;
22 +import org.onosproject.net.Annotations;
23 +import org.onosproject.net.DefaultAnnotations;
24 +
25 +import static com.google.common.base.Preconditions.checkNotNull;
26 +import static com.google.common.base.Preconditions.checkState;
27 +import static org.onosproject.net.DefaultAnnotations.builder;
28 +
29 +/**
30 + * Abstraction of a device key.
31 + */
32 +@Beta
33 +public class DeviceKey extends AbstractAnnotated {
34 +
35 + // device key identifier
36 + private final DeviceKeyId deviceKeyId;
37 + // label of the device key
38 + private String label;
39 +
40 + /**
41 + * type of the device key.
42 + */
43 + enum Type {
44 + COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY
45 + }
46 +
47 + private Type type;
48 +
49 + /**
50 + * Constructor for serialization.
51 + */
52 + DeviceKey() {
53 + this.deviceKeyId = null;
54 + this.label = null;
55 + this.type = null;
56 + }
57 +
58 + /**
59 + * Private constructor for a device key.
60 + *
61 + * @param id device key identifier
62 + * @param label optional label for this device key
63 + * @param type to be assigned to this device key
64 + * @param annotations name/value pairs for this device key
65 + */
66 + private DeviceKey(DeviceKeyId id, String label, Type type, Annotations... annotations) {
67 + super(annotations);
68 + checkNotNull(id, "The DeviceKeyId cannot be null.");
69 + this.deviceKeyId = id;
70 + this.label = label;
71 + this.type = type;
72 + }
73 +
74 + /**
75 + * Returns the device key identifier of the device key.
76 + *
77 + * @return device key identifier
78 + */
79 + public DeviceKeyId deviceKeyId() {
80 + return deviceKeyId;
81 + }
82 +
83 + /**
84 + * Returns the label of device key.
85 + *
86 + * @return label
87 + */
88 + public String label() {
89 + return label;
90 + }
91 +
92 + /**
93 + * Returns the type of the device key.
94 + *
95 + * @return type
96 + */
97 + public Type type() {
98 + return type;
99 + }
100 +
101 + /**
102 + * Method to create a device key of type CommunityName.
103 + *
104 + * @param id device key identifier
105 + * @param label optional label for this device key
106 + * @param name community name for this device key
107 + * @return device key
108 + */
109 + public static DeviceKey createDeviceKeyUsingCommunityName(DeviceKeyId id, String label, String name) {
110 + DefaultAnnotations annotations = builder().set(AnnotationKeys.NAME, name).build();
111 +
112 + return new DeviceKey(id, label, Type.COMMUNITY_NAME, annotations);
113 + }
114 +
115 + /**
116 + * Returns a community name object from the device key.
117 + *
118 + * @return community name
119 + */
120 + public CommunityName asCommunityName() {
121 +
122 + // Validate that the device key is of type COMMUNITY_NAME.
123 + checkState(this.type == Type.COMMUNITY_NAME, "This device key is not a COMMUNITY_NAME type.");
124 +
125 + String name = annotations().value(AnnotationKeys.NAME);
126 + return CommunityName.communityName(name);
127 + }
128 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +package org.onosproject.incubator.net.key;
18 +
19 +import com.google.common.annotations.Beta;
20 +
21 +/**
22 + * Service for managing device keys.
23 + */
24 +@Beta
25 +public interface DeviceKeyAdminService extends DeviceKeyService {
26 +
27 + /**
28 + * Adds a new device key to the store.
29 + *
30 + * @param deviceKey device key to be stored
31 + */
32 + void addKey(DeviceKey deviceKey);
33 +
34 + /**
35 + * Removes a device key from the store using the device
36 + * key identifier.
37 + *
38 + * @param id device key identifier used to identify the device key
39 + */
40 + void removeKey(DeviceKeyId id);
41 +}
42 +
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +package org.onosproject.incubator.net.key;
18 +
19 +import java.util.Objects;
20 +
21 +import static com.google.common.base.Preconditions.checkNotNull;
22 +
23 +/**
24 + * Device key Id definition.
25 + */
26 +public final class DeviceKeyId {
27 + private final String identifier;
28 +
29 + /**
30 + * Constructor for serialization.
31 + */
32 + private DeviceKeyId() {
33 + this.identifier = null;
34 + }
35 +
36 + /**
37 + * Constructs the ID corresponding to a given string value.
38 + *
39 + * @param value the underlying value of this ID
40 + */
41 + private DeviceKeyId(String value) {
42 + this.identifier = checkNotNull(value, "Device Key Id cannot be null.");
43 + }
44 +
45 + /**
46 + * Static method to construct a device key identifier.
47 + *
48 + * @param id for the device key identifier
49 + * @return device key identifier
50 + */
51 + static final DeviceKeyId deviceKeyId(String id) {
52 + return new DeviceKeyId(id);
53 + }
54 +
55 + /**
56 + * Returns the identifier of the device key identifier.
57 + *
58 + * @return identifier
59 + */
60 + public String id() {
61 + return identifier;
62 + }
63 +
64 + /**
65 + * Returns the hashcode of the identifier.
66 + *
67 + * @return hashcode
68 + */
69 + @Override
70 + public int hashCode() {
71 + return identifier.hashCode();
72 + }
73 +
74 + /**
75 + * Compares two device key identifiers for equality.
76 + *
77 + * @param obj to compare against
78 + * @return true if the objects are equal, false otherwise.
79 + */
80 + @Override
81 + public boolean equals(Object obj) {
82 + if (this == obj) {
83 + return true;
84 + }
85 + if (obj instanceof DeviceKeyId) {
86 + final DeviceKeyId that = (DeviceKeyId) obj;
87 + return this.getClass() == that.getClass() &&
88 + Objects.equals(this.identifier, that.identifier);
89 + }
90 + return false;
91 + }
92 +
93 + /**
94 + * Returns a string representation of a DeviceKeyId.
95 + *
96 + * @return string
97 + */
98 + public String toString() {
99 + return identifier;
100 + }
101 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +package org.onosproject.incubator.net.key;
18 +
19 +import com.google.common.annotations.Beta;
20 +
21 +/**
22 + * Service for querying device keys.
23 + */
24 +@Beta
25 +public interface DeviceKeyService {
26 +
27 + /**
28 + * Returns all device keys.
29 + *
30 + * @return collection of device keys
31 + */
32 + Iterable<DeviceKey> getDeviceKeys();
33 +
34 + /**
35 + * Returns the device key(s) using a device key identifier.
36 + *
37 + * @param id device key identifier
38 + * @return collection of device keys
39 + */
40 + Iterable<DeviceKey> getDeviceKey(DeviceKeyId id);
41 +}
42 +
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +
17 +/**
18 + * Device key data model and services.
19 + * This subsystem and its interfaces will change in the upcoming release.
20 + */
21 +package org.onosproject.incubator.net.key;
...\ No newline at end of file ...\ No newline at end of file