Brian Stanke
Committed by Gerrit Code Review

ONOS-3658 - Adding CLI commands to display and manage Device Keys.

Change-Id: I98d7d3b7bc9a61f106a6b79b38d7ddaebbfb3288
...@@ -19,13 +19,13 @@ import org.onosproject.cluster.ControllerNode; ...@@ -19,13 +19,13 @@ import org.onosproject.cluster.ControllerNode;
19 import org.onosproject.core.Application; 19 import org.onosproject.core.Application;
20 import org.onosproject.core.ApplicationId; 20 import org.onosproject.core.ApplicationId;
21 import org.onosproject.incubator.net.intf.Interface; 21 import org.onosproject.incubator.net.intf.Interface;
22 +import org.onosproject.incubator.net.key.DeviceKey;
22 import org.onosproject.net.ConnectPoint; 23 import org.onosproject.net.ConnectPoint;
23 import org.onosproject.net.Element; 24 import org.onosproject.net.Element;
24 import org.onosproject.net.ElementId; 25 import org.onosproject.net.ElementId;
25 import org.onosproject.net.Port; 26 import org.onosproject.net.Port;
26 import org.onosproject.net.flow.FlowRule; 27 import org.onosproject.net.flow.FlowRule;
27 import org.onosproject.net.group.Group; 28 import org.onosproject.net.group.Group;
28 -
29 import org.onosproject.net.statistic.TypedFlowEntryWithLoad; 29 import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
30 import org.onosproject.net.topology.TopologyCluster; 30 import org.onosproject.net.topology.TopologyCluster;
31 31
...@@ -118,8 +118,8 @@ public final class Comparators { ...@@ -118,8 +118,8 @@ public final class Comparators {
118 public int compare(ConnectPoint o1, ConnectPoint o2) { 118 public int compare(ConnectPoint o1, ConnectPoint o2) {
119 int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId()); 119 int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId());
120 return (compareId != 0) ? 120 return (compareId != 0) ?
121 - compareId : 121 + compareId :
122 - Long.signum(o1.port().toLong() - o2.port().toLong()); 122 + Long.signum(o1.port().toLong() - o2.port().toLong());
123 } 123 }
124 }; 124 };
125 125
...@@ -130,8 +130,15 @@ public final class Comparators { ...@@ -130,8 +130,15 @@ public final class Comparators {
130 new Comparator<TypedFlowEntryWithLoad>() { 130 new Comparator<TypedFlowEntryWithLoad>() {
131 @Override 131 @Override
132 public int compare(TypedFlowEntryWithLoad fe1, TypedFlowEntryWithLoad fe2) { 132 public int compare(TypedFlowEntryWithLoad fe1, TypedFlowEntryWithLoad fe2) {
133 - long delta = fe1.load().rate() - fe2.load().rate(); 133 + long delta = fe1.load().rate() - fe2.load().rate();
134 return delta == 0 ? 0 : (delta > 0 ? -1 : +1); 134 return delta == 0 ? 0 : (delta > 0 ? -1 : +1);
135 } 135 }
136 }; 136 };
137 +
138 + public static final Comparator<DeviceKey> DEVICE_KEY_COMPARATOR = new Comparator<DeviceKey>() {
139 + @Override
140 + public int compare(DeviceKey deviceKey1, DeviceKey deviceKey2) {
141 + return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString());
142 + }
143 + };
137 } 144 }
......
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.cli.net;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.apache.karaf.shell.commands.Option;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.incubator.net.key.DeviceKey;
24 +import org.onosproject.incubator.net.key.DeviceKeyAdminService;
25 +import org.onosproject.incubator.net.key.DeviceKeyId;
26 +
27 +/**
28 + * Adds a device key.
29 + */
30 +@Command(scope = "onos", name = "device-key-add",
31 + description = "Adds a device key. Adding a new device key with " +
32 + "the same id will replace the existing device key.")
33 +
34 +public class DeviceKeyAddCommand extends AbstractShellCommand {
35 +
36 + private static final String COMMUNITY_NAME = "CommunityName";
37 + private static final String USERNAME = "UsernamePassword";
38 +
39 + @Argument(index = 0, name = "id", description = "Device Key ID",
40 + required = true, multiValued = false)
41 + String id = null;
42 +
43 + @Argument(index = 1, name = "type", description = "Device Key Type, " +
44 + "it includes CommunityName, UsernamePassword.",
45 + required = true, multiValued = false)
46 + String type = null;
47 +
48 + @Option(name = "-c", aliases = "--communityName", description = "Device Key Community Name",
49 + required = false, multiValued = false)
50 + String communityName = null;
51 +
52 + @Option(name = "-l", aliases = "--label", description = "Device Key Label",
53 + required = false, multiValued = false)
54 + String label = null;
55 +
56 + @Option(name = "-u", aliases = "--username", description = "Device Key Username",
57 + required = false, multiValued = false)
58 + String username = null;
59 +
60 + @Option(name = "-p", aliases = "--password", description = "Device Key Password",
61 + required = false, multiValued = false)
62 + String password = null;
63 +
64 + @Override
65 + protected void execute() {
66 + DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
67 + DeviceKey deviceKey = null;
68 + if (type.equalsIgnoreCase(COMMUNITY_NAME)) {
69 + deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(DeviceKeyId.deviceKeyId(id),
70 + label, communityName);
71 + } else if (type.equalsIgnoreCase(USERNAME)) {
72 + deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(DeviceKeyId.deviceKeyId(id),
73 + label, username, password);
74 + } else {
75 + print("Invalid Device key type: ", type);
76 + return;
77 + }
78 + service.addKey(deviceKey);
79 + print("Device Key successfully added.");
80 + }
81 +}
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.cli.net;
18 +
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.cli.Comparators;
22 +import org.onosproject.incubator.net.key.DeviceKey;
23 +import org.onosproject.incubator.net.key.DeviceKeyService;
24 +
25 +import java.util.Collections;
26 +import java.util.List;
27 +
28 +import static com.google.common.collect.Lists.newArrayList;
29 +
30 +/**
31 + * Lists all device keys.
32 + */
33 +@Command(scope = "onos", name = "device-keys",
34 + description = "Lists all device keys")
35 +
36 +public class DeviceKeyListCommand extends AbstractShellCommand {
37 + private static final String FMT_COMMUNITY_NAME =
38 + "identifier=%s, type=%s, community name=%s";
39 + private static final String FMT_USERNAME_PASSWORD =
40 + "identifier=%s, type=%s, username=%s, password=%s";
41 +
42 + @Override
43 + protected void execute() {
44 + DeviceKeyService service = get(DeviceKeyService.class);
45 + for (DeviceKey deviceKey : getSortedDeviceKeys(service)) {
46 + printDeviceKey(deviceKey);
47 + }
48 + }
49 +
50 + /**
51 + * Returns the list of devices keys sorted using the device key identifier.
52 + *
53 + * @param service device key service
54 + * @return sorted device key list
55 + */
56 + protected List<DeviceKey> getSortedDeviceKeys(DeviceKeyService service) {
57 + List<DeviceKey> deviceKeys = newArrayList(service.getDeviceKeys());
58 + Collections.sort(deviceKeys, Comparators.DEVICE_KEY_COMPARATOR);
59 + return deviceKeys;
60 + }
61 +
62 + /**
63 + * Prints out each device key.
64 + *
65 + * @param deviceKey the device key to be printed
66 + */
67 + private void printDeviceKey(DeviceKey deviceKey) {
68 + if (DeviceKey.Type.COMMUNITY_NAME.equals(deviceKey.type())) {
69 + print(FMT_COMMUNITY_NAME, deviceKey.deviceKeyId().id(), deviceKey.type(),
70 + deviceKey.asCommunityName().name());
71 + } else if (DeviceKey.Type.USERNAME_PASSWORD.equals(deviceKey.type())) {
72 + print(FMT_USERNAME_PASSWORD, deviceKey.deviceKeyId().id(), deviceKey.type(),
73 + deviceKey.asUsernamePassword().username(), deviceKey.asUsernamePassword().password());
74 + } else {
75 + log.error("Unsupported device key type: " + deviceKey.type());
76 + }
77 + }
78 +
79 +}
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.cli.net;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.key.DeviceKeyAdminService;
23 +import org.onosproject.incubator.net.key.DeviceKeyId;
24 +
25 +/**
26 + * Removes a device key.
27 + */
28 +@Command(scope = "onos", name = "device-key-remove",
29 + description = "Removes a device key")
30 +
31 +public class DeviceKeyRemoveCommand extends AbstractShellCommand {
32 +
33 + @Argument(index = 0, name = "id", description = "Device Key ID",
34 + required = true, multiValued = false)
35 + String id = null;
36 +
37 + @Override
38 + protected void execute() {
39 + DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
40 + service.removeKey(DeviceKeyId.deviceKeyId(id));
41 + }
42 +}
...@@ -533,6 +533,17 @@ ...@@ -533,6 +533,17 @@
533 <command> 533 <command>
534 <action class="org.onosproject.cli.net.TunnelUpdateCommand"/> 534 <action class="org.onosproject.cli.net.TunnelUpdateCommand"/>
535 </command> 535 </command>
536 + <!-- device key commands -->
537 + <command>
538 + <action class="org.onosproject.cli.net.DeviceKeyListCommand"/>
539 + </command>
540 + <command>
541 + <action class="org.onosproject.cli.net.DeviceKeyAddCommand"/>
542 + </command>
543 + <command>
544 + <action class="org.onosproject.cli.net.DeviceKeyRemoveCommand"/>
545 + </command>
546 +
536 </command-bundle> 547 </command-bundle>
537 548
538 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/> 549 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/>
......
...@@ -40,7 +40,7 @@ public class DeviceKey extends AbstractAnnotated { ...@@ -40,7 +40,7 @@ public class DeviceKey extends AbstractAnnotated {
40 /** 40 /**
41 * type of the device key. 41 * type of the device key.
42 */ 42 */
43 - enum Type { 43 + public enum Type {
44 COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY 44 COMMUNITY_NAME, USERNAME_PASSWORD, SSL_KEY
45 } 45 }
46 46
......
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
17 package org.onosproject.incubator.net.key.impl; 17 package org.onosproject.incubator.net.key.impl;
18 18
19 import org.apache.felix.scr.annotations.Activate; 19 import org.apache.felix.scr.annotations.Activate;
20 +import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
21 import org.apache.felix.scr.annotations.Reference; 22 import org.apache.felix.scr.annotations.Reference;
22 import org.apache.felix.scr.annotations.ReferenceCardinality; 23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.apache.felix.scr.annotations.Service;
23 import org.onosproject.event.AbstractListenerManager; 25 import org.onosproject.event.AbstractListenerManager;
24 import org.onosproject.incubator.net.key.DeviceKey; 26 import org.onosproject.incubator.net.key.DeviceKey;
25 import org.onosproject.incubator.net.key.DeviceKeyAdminService; 27 import org.onosproject.incubator.net.key.DeviceKeyAdminService;
...@@ -42,14 +44,13 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -42,14 +44,13 @@ import static org.slf4j.LoggerFactory.getLogger;
42 /** 44 /**
43 * Implementation of device key services. 45 * Implementation of device key services.
44 */ 46 */
47 +@Component(immediate = true, enabled = true)
48 +@Service
45 public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener> 49 public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener>
46 implements DeviceKeyService, DeviceKeyAdminService { 50 implements DeviceKeyService, DeviceKeyAdminService {
47 51
48 private final Logger log = getLogger(getClass()); 52 private final Logger log = getLogger(getClass());
49 53
50 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 - protected DeviceKeyService deviceKeyService;
52 -
53 private DeviceKeyStoreDelegate delegate = this::post; 54 private DeviceKeyStoreDelegate delegate = this::post;
54 55
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......
...@@ -36,6 +36,7 @@ import org.onosproject.store.service.Serializer; ...@@ -36,6 +36,7 @@ import org.onosproject.store.service.Serializer;
36 import org.onosproject.store.service.StorageService; 36 import org.onosproject.store.service.StorageService;
37 import org.slf4j.Logger; 37 import org.slf4j.Logger;
38 38
39 +import java.util.Arrays;
39 import java.util.Collection; 40 import java.util.Collection;
40 import java.util.Map; 41 import java.util.Map;
41 42
...@@ -68,7 +69,10 @@ public class DistributedDeviceKeyStore ...@@ -68,7 +69,10 @@ public class DistributedDeviceKeyStore
68 @Activate 69 @Activate
69 public void activate() { 70 public void activate() {
70 deviceKeys = storageService.<DeviceKeyId, DeviceKey>consistentMapBuilder() 71 deviceKeys = storageService.<DeviceKeyId, DeviceKey>consistentMapBuilder()
71 - .withSerializer(Serializer.using(KryoNamespaces.API)) 72 + .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
73 + DeviceKey.class,
74 + DeviceKeyId.class,
75 + DeviceKey.Type.class))
72 .withName("onos-device-keys") 76 .withName("onos-device-keys")
73 .withRelaxedReadConsistency() 77 .withRelaxedReadConsistency()
74 .build(); 78 .build();
......