3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.containermanager.internal;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.EnumSet;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Locale;
25 import java.util.Map.Entry;
27 import java.util.concurrent.ConcurrentMap;
28 import java.util.concurrent.CopyOnWriteArrayList;
30 import org.eclipse.osgi.framework.console.CommandInterpreter;
31 import org.eclipse.osgi.framework.console.CommandProvider;
32 import org.opendaylight.controller.appauth.authorization.Authorization;
33 import org.opendaylight.controller.clustering.services.CacheConfigException;
34 import org.opendaylight.controller.clustering.services.CacheExistException;
35 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
36 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
37 import org.opendaylight.controller.clustering.services.IClusterServices;
38 import org.opendaylight.controller.configuration.ConfigurationObject;
39 import org.opendaylight.controller.configuration.IConfigurationAware;
40 import org.opendaylight.controller.configuration.IConfigurationService;
41 import org.opendaylight.controller.containermanager.ContainerChangeEvent;
42 import org.opendaylight.controller.containermanager.ContainerConfig;
43 import org.opendaylight.controller.containermanager.ContainerData;
44 import org.opendaylight.controller.containermanager.ContainerFlowChangeEvent;
45 import org.opendaylight.controller.containermanager.ContainerFlowConfig;
46 import org.opendaylight.controller.containermanager.IContainerAuthorization;
47 import org.opendaylight.controller.containermanager.IContainerManager;
48 import org.opendaylight.controller.containermanager.NodeConnectorsChangeEvent;
49 import org.opendaylight.controller.sal.authorization.AppRoleLevel;
50 import org.opendaylight.controller.sal.authorization.Privilege;
51 import org.opendaylight.controller.sal.authorization.Resource;
52 import org.opendaylight.controller.sal.authorization.ResourceGroup;
53 import org.opendaylight.controller.sal.authorization.UserLevel;
54 import org.opendaylight.controller.sal.core.ContainerFlow;
55 import org.opendaylight.controller.sal.core.IContainerAware;
56 import org.opendaylight.controller.sal.core.IContainerListener;
57 import org.opendaylight.controller.sal.core.IContainerLocalListener;
58 import org.opendaylight.controller.sal.core.Node;
59 import org.opendaylight.controller.sal.core.NodeConnector;
60 import org.opendaylight.controller.sal.core.UpdateType;
61 import org.opendaylight.controller.sal.match.Match;
62 import org.opendaylight.controller.sal.utils.GlobalConstants;
63 import org.opendaylight.controller.sal.utils.IObjectReader;
64 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
65 import org.opendaylight.controller.sal.utils.NodeCreator;
66 import org.opendaylight.controller.sal.utils.ServiceHelper;
67 import org.opendaylight.controller.sal.utils.Status;
68 import org.opendaylight.controller.sal.utils.StatusCode;
69 import org.opendaylight.controller.topologymanager.ITopologyManager;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 public class ContainerManager extends Authorization<String> implements IContainerManager, IObjectReader,
74 CommandProvider, ICacheUpdateAware<String, Object>, IContainerInternal, IContainerAuthorization,
76 private static final Logger logger = LoggerFactory.getLogger(ContainerManager.class);
77 private static String CONTAINERS_FILE_NAME = "containers.conf";
78 private static final String allContainersGroup = "allContainers";
79 private IClusterGlobalServices clusterServices;
80 private IConfigurationService configurationService;
82 * Collection containing the configuration objects. This is configuration
83 * world: container names (also the map key) are maintained as they were
84 * configured by user, same case
86 private ConcurrentMap<String, ContainerConfig> containerConfigs;
87 private ConcurrentMap<String, ContainerData> containerData;
88 private ConcurrentMap<NodeConnector, CopyOnWriteArrayList<String>> nodeConnectorToContainers;
89 private ConcurrentMap<Node, Set<String>> nodeToContainers;
90 private ConcurrentMap<String, Object> containerChangeEvents;
91 private final Set<IContainerAware> iContainerAware = Collections.synchronizedSet(new HashSet<IContainerAware>());
92 private final Set<IContainerListener> iContainerListener = Collections
93 .synchronizedSet(new HashSet<IContainerListener>());
94 private final Set<IContainerLocalListener> iContainerLocalListener = Collections
95 .synchronizedSet(new HashSet<IContainerLocalListener>());
97 void setIContainerListener(IContainerListener s) {
98 if (this.iContainerListener != null) {
99 this.iContainerListener.add(s);
101 * At boot with startup, containers are created before listeners have
102 * joined. Replaying here the first container creation notification for
103 * the joining listener when containers are already present. Also
104 * replaying all the node connectors and container flows additions
105 * to the existing containers.
107 if (!this.containerData.isEmpty()) {
108 s.containerModeUpdated(UpdateType.ADDED);
110 for (ConcurrentMap.Entry<NodeConnector, CopyOnWriteArrayList<String>> entry : nodeConnectorToContainers
112 NodeConnector port = entry.getKey();
113 for (String container : entry.getValue()) {
114 s.nodeConnectorUpdated(container, port, UpdateType.ADDED);
117 for (Map.Entry<String, ContainerData> container : containerData.entrySet()) {
118 for (ContainerFlow cFlow : container.getValue().getContainerFlowSpecs()) {
119 s.containerFlowUpdated(container.getKey(), cFlow, cFlow, UpdateType.ADDED);
125 void unsetIContainerListener(IContainerListener s) {
126 if (this.iContainerListener != null) {
127 this.iContainerListener.remove(s);
131 void setIContainerLocalListener(IContainerLocalListener s) {
132 if (this.iContainerLocalListener != null) {
133 this.iContainerLocalListener.add(s);
137 void unsetIContainerLocalListener(IContainerLocalListener s) {
138 if (this.iContainerLocalListener != null) {
139 this.iContainerLocalListener.remove(s);
143 public void setIContainerAware(IContainerAware iContainerAware) {
144 if (!this.iContainerAware.contains(iContainerAware)) {
145 this.iContainerAware.add(iContainerAware);
146 // Now call the container creation for all the known containers so far
147 for (String container : getContainerNameList()) {
148 iContainerAware.containerCreate(container.toLowerCase(Locale.ENGLISH));
153 public void unsetIContainerAware(IContainerAware iContainerAware) {
154 this.iContainerAware.remove(iContainerAware);
155 // There is no need to do cleanup of the component when
156 // unregister because it will be taken care by the Container
160 public void setClusterServices(IClusterGlobalServices i) {
161 this.clusterServices = i;
162 logger.debug("IClusterServices set");
165 public void unsetClusterServices(IClusterGlobalServices i) {
166 if (this.clusterServices == i) {
167 this.clusterServices = null;
168 logger.debug("IClusterServices Unset");
172 public void setConfigurationService(IConfigurationService service) {
173 logger.trace("Got configuration service set request {}", service);
174 this.configurationService = service;
177 public void unsetConfigurationService(IConfigurationService service) {
178 logger.trace("Got configuration service UNset request");
179 this.configurationService = null;
182 private void allocateCaches() {
183 logger.debug("Container Manager allocating caches");
185 if (clusterServices == null) {
186 logger.warn("un-initialized Cluster Services, can't allocate caches");
190 clusterServices.createCache("containermgr.containerConfigs", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
192 clusterServices.createCache("containermgr.event.containerChange",
193 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
195 clusterServices.createCache("containermgr.containerData", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
197 clusterServices.createCache("containermgr.nodeConnectorToContainers",
198 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
200 clusterServices.createCache("containermgr.nodeToContainers", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
202 clusterServices.createCache("containermgr.containerGroups", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
204 clusterServices.createCache("containermgr.containerAuthorizations",
205 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
207 clusterServices.createCache("containermgr.roles", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
208 } catch (CacheConfigException cce) {
209 logger.error("Cache configuration invalid - check cache mode");
210 } catch (CacheExistException ce) {
211 logger.error("Cache already exits - destroy and recreate if needed");
215 @SuppressWarnings({ "unchecked" })
216 private void retrieveCaches() {
217 logger.debug("Container Manager retrieving caches");
219 if (clusterServices == null) {
220 logger.warn("un-initialized Cluster Services, can't retrieve caches");
224 containerConfigs = (ConcurrentMap<String, ContainerConfig>) clusterServices.getCache("containermgr.containerConfigs");
226 containerChangeEvents = (ConcurrentMap<String, Object>) clusterServices.getCache("containermgr.event.containerChange");
228 containerData = (ConcurrentMap<String, ContainerData>) clusterServices.getCache("containermgr.containerData");
230 nodeConnectorToContainers = (ConcurrentMap<NodeConnector, CopyOnWriteArrayList<String>>) clusterServices
231 .getCache("containermgr.nodeConnectorToContainers");
233 nodeToContainers = (ConcurrentMap<Node, Set<String>>) clusterServices.getCache("containermgr.nodeToContainers");
235 resourceGroups = (ConcurrentMap<String, Set<String>>) clusterServices.getCache("containermgr.containerGroups");
237 groupsAuthorizations = (ConcurrentMap<String, Set<ResourceGroup>>) clusterServices
238 .getCache("containermgr.containerAuthorizations");
240 roles = (ConcurrentMap<String, AppRoleLevel>) clusterServices.getCache("containermgr.roles");
242 if (inContainerMode()) {
243 for (Map.Entry<String, ContainerConfig> entry : containerConfigs.entrySet()) {
244 // Notify global and local listeners about the mode change
245 notifyContainerChangeInternal(entry.getValue(), UpdateType.ADDED, true);
251 public void entryCreated(String containerName, String cacheName, boolean originLocal) {
256 public void entryUpdated(String key, Object value, String cacheName, boolean originLocal) {
258 * This is were container manager replays a configuration event that was
259 * notified by its peer from a cluster node where the configuration
260 * happened. Only the global listeners, the cluster unaware classes,
261 * (mainly the shim classes in the sdn protocol plugins) need to receive
262 * these notifications on this cluster node. The cluster aware classes,
263 * like the functional modules which reacts on these events, must _not_
264 * be notified to avoid parallel computation in the cluster.
267 if (value instanceof NodeConnectorsChangeEvent) {
268 NodeConnectorsChangeEvent event = (NodeConnectorsChangeEvent) value;
269 List<NodeConnector> ncList = event.getNodeConnectors();
270 notifyContainerEntryChangeInternal(key, ncList, event.getUpdateType(), false);
271 } else if (value instanceof ContainerFlowChangeEvent) {
272 ContainerFlowChangeEvent event = (ContainerFlowChangeEvent) value;
273 notifyCFlowChangeInternal(key, event.getConfigList(), event.getUpdateType(), false);
274 } else if (value instanceof ContainerChangeEvent) {
275 ContainerChangeEvent event = (ContainerChangeEvent) value;
276 notifyContainerChangeInternal(event.getConfig(), event.getUpdateType(), false);
282 public void entryDeleted(String containerName, String cacheName, boolean originLocal) {
285 public ContainerManager() {
292 public void start() {
293 // Get caches from cluster manager
297 // Allocates default groups and association to default roles
298 createDefaultAuthorizationGroups();
300 // Read startup configuration and create local database
301 loadContainerConfig();
304 public void destroy() {
305 // Clear local states
306 this.iContainerAware.clear();
307 this.iContainerListener.clear();
308 this.iContainerLocalListener.clear();
312 * Adds/Remove the list of flow specs to/from the specified container. This
313 * function is supposed to be called after all the validation checks have
314 * already been run on the proposed configuration.
316 private Status updateContainerFlow(String containerName, List<ContainerFlowConfig> confList, boolean delete) {
317 ContainerData container = getContainerByName(containerName);
318 if (container == null) {
319 return new Status(StatusCode.GONE, "Container not present");
322 for (ContainerFlowConfig conf : confList) {
323 // Validation was fine. Modify the database now.
324 for (Match match : conf.getMatches()) {
325 ContainerFlow cFlow = new ContainerFlow(match);
327 logger.trace("Removing Flow Spec {} from Container {}", conf.getName(), containerName);
328 container.deleteFlowSpec(cFlow);
330 logger.trace("Adding Flow Spec {} to Container {}", conf.getName(), containerName);
331 container.addFlowSpec(cFlow);
335 putContainerDataByName(containerName, container);
338 return new Status(StatusCode.SUCCESS);
342 * Adds/Remove this container to/from the Container database, no updates are going
343 * to be generated here other that the destroying and creation of the container.
344 * This function is supposed to be called after all the validation checks
345 * have already been run on the configuration object
347 private Status updateContainerDatabase(ContainerConfig containerConf, boolean delete) {
349 * Back-end world here, container names are all stored in lower case
351 String containerName = containerConf.getContainerName();
352 ContainerData container = getContainerByName(containerName);
353 if (delete && container == null) {
354 return new Status(StatusCode.NOTFOUND, "Container is not present");
356 if (!delete && container != null) {
357 // A container with the same (lower case) name already exists
358 return new Status(StatusCode.CONFLICT, "A container with the same name already exists");
361 logger.debug("Removing container {}", containerName);
362 removeNodeToContainersMapping(container);
363 removeNodeConnectorToContainersMapping(container);
364 removeContainerDataByName(containerName);
366 logger.debug("Adding container {}", containerName);
367 container = new ContainerData(containerConf);
368 putContainerDataByName(containerName, container);
370 // If flow specs are specified, add them
371 if (containerConf.hasFlowSpecs()) {
372 updateContainerFlow(containerName, containerConf.getContainerFlowConfigs(), delete);
375 // If ports are specified, add them
376 if (!containerConf.getPortList().isEmpty()) {
377 updateContainerEntryDatabase(containerName, containerConf.getPortList(), delete);
380 return new Status(StatusCode.SUCCESS);
383 private void removeNodeConnectorToContainersMapping(ContainerData container) {
384 Iterator<Entry<NodeConnector, CopyOnWriteArrayList<String>>> it = nodeConnectorToContainers.entrySet().iterator();
385 String containerName = container.getContainerName();
386 for (; it.hasNext();) {
387 Entry<NodeConnector, CopyOnWriteArrayList<String>> entry = it.next();
388 final NodeConnector nc = entry.getKey();
389 final CopyOnWriteArrayList<String> slist = entry.getValue();
390 for (final String sdata : slist) {
391 if (sdata.equalsIgnoreCase(containerName)) {
392 logger.debug("Removing NodeConnector->Containers mapping, nodeConnector: {}", nc);
393 slist.remove(containerName);
394 if (slist.isEmpty()) {
395 nodeConnectorToContainers.remove(nc);
397 nodeConnectorToContainers.put(nc, slist);
405 private void removeNodeToContainersMapping(ContainerData container) {
406 for (Entry<Node, Set<String>> entry : nodeToContainers.entrySet()) {
407 Node node = entry.getKey();
408 for (String sdata : entry.getValue()) {
409 if (sdata.equals(container.getContainerName())) {
410 logger.debug("Removing Node->Containers mapping, node {} container {}", node, sdata);
411 Set<String> value = nodeToContainers.get(node);
413 nodeToContainers.put(node, value);
421 * Adds/Remove container data to/from the container. This function is supposed to be
422 * called after all the validation checks have already been run on the
423 * configuration object
425 private Status updateContainerEntryDatabase(String containerName, List<NodeConnector> nodeConnectors, boolean delete) {
426 ContainerData container = getContainerByName(containerName);
428 if (container == null) {
429 return new Status(StatusCode.NOTFOUND, "Container Not Found");
432 // Check changes in the portlist
433 for (NodeConnector port : nodeConnectors) {
434 Node node = port.getNode();
436 container.removePortFromSwitch(port);
437 putContainerDataByName(containerName, container);
439 /* remove <sp> - container mapping */
440 if (nodeConnectorToContainers.containsKey(port)) {
441 nodeConnectorToContainers.remove(port);
444 * If no more ports in the switch, remove switch from container
445 * Generate switchRemoved Event
447 if (container.portListEmpty(node)) {
448 logger.debug("Port List empty for switch {}", node);
449 putContainerDataByName(containerName, container);
450 // remove node->containers mapping
451 Set<String> slist = nodeToContainers.get(node);
453 logger.debug("Removing container from switch-container list. node{}, container{}", node, containerName);
454 slist.remove(container.getContainerName());
455 nodeToContainers.put(node, slist);
456 if (slist.isEmpty()) {
457 logger.debug("Container list empty for switch {}. removing switch-container mapping", node);
458 nodeToContainers.remove(node);
463 if (container.isSwitchInContainer(node) == false) {
464 Set<String> value = nodeToContainers.get(node);
465 // Add node->containers mapping
467 value = new HashSet<String>();
468 logger.debug("Creating new Container Set for switch {}", node);
470 value.add(container.getContainerName());
471 nodeToContainers.put(node, value);
473 container.addPortToSwitch(port);
474 putContainerDataByName(containerName, container);
476 // added nc->containers mapping
477 CopyOnWriteArrayList<String> slist = nodeConnectorToContainers.get(port);
479 slist = new CopyOnWriteArrayList<String>();
481 slist.add(container.getContainerName());
482 nodeConnectorToContainers.put(port, slist);
485 return new Status(StatusCode.SUCCESS);
488 private Status validateContainerFlowAddRemoval(String containerName, ContainerFlow cFlow, boolean delete) {
490 * It used to be the comment below: ~~~~~~~~~~~~~~~~~~~~ If Link Sharing
491 * at Host facing interfaces, then disallow last ContainerFlow removal
492 * ~~~~~~~~~~~~~~~~~~~~ But the interface being host facing is a
493 * condition that can change at runtime and so the final effect will be
494 * unreliable. So now we will always allow the container flow removal,
495 * if this is a link host facing and is shared by many that will cause
496 * issues but that validation should be done not at the configuration
497 * but in the UI/northbound side.
499 ContainerData container = this.getContainerByName(containerName);
500 if (container == null) {
501 String error = String.format("Cannot validate flow specs for container %s: (Container does not exist)", containerName);
503 return new Status(StatusCode.BADREQUEST, error);
507 Set<NodeConnector> thisContainerPorts = container.getNodeConnectors();
508 // Go through all the installed containers
509 for (Map.Entry<String, ContainerData> entry : containerData.entrySet()) {
510 if (containerName.equalsIgnoreCase(entry.getKey())) {
513 // Derive the common ports
514 Set<NodeConnector> commonPorts = entry.getValue().getNodeConnectors();
515 commonPorts.retainAll(thisContainerPorts);
516 if (commonPorts.isEmpty()) {
520 // Check if this operation would remove the only flow spec
521 // assigned to this container
522 if (container.getFlowSpecCount() == 1) {
523 if (!container.hasStaticVlanAssigned()) {
524 // Ports are shared and static vlan is not present: this
526 // regardless the shared ports are host facing or
528 return new Status(StatusCode.BADREQUEST, "Container shares port with another container: "
529 + "The only one flow spec assigned to this container cannot be removed,"
530 + "because this container is not assigned any static vlan");
533 // Check on host facing port
534 ITopologyManager topologyManager = (ITopologyManager) ServiceHelper.getInstance(
535 ITopologyManager.class, GlobalConstants.DEFAULT.toString(), this);
536 if (topologyManager == null) {
537 return new Status(StatusCode.NOSERVICE,
538 "Cannot validate the request: Required service is not available");
540 for (NodeConnector nc : commonPorts) {
542 * Shared link case : For internal port check if it has
543 * a vlan configured. If vlan is configured, allow the
544 * flowspec to be deleted If the port is host-facing, do
545 * not allow the flowspec to be deleted
547 if (!topologyManager.isInternal(nc)) {
548 return new Status(StatusCode.BADREQUEST, String.format(
549 "Port %s is shared and is host facing port: "
550 + "The only one flow spec assigned to this container cannot be removed", nc));
556 // Adding a new flow spec: need to check if other containers with common
557 // ports do not have same flow spec
558 Set<NodeConnector> thisContainerPorts = container.getNodeConnectors();
559 List<ContainerFlow> proposed = new ArrayList<ContainerFlow>(container.getContainerFlowSpecs());
561 for (Map.Entry<String, ContainerData> entry : containerData.entrySet()) {
562 if (containerName.equalsIgnoreCase(entry.getKey())) {
565 ContainerData otherContainer = entry.getValue();
566 Set<NodeConnector> commonPorts = otherContainer.getNodeConnectors();
567 commonPorts.retainAll(thisContainerPorts);
569 if (!commonPorts.isEmpty()) {
570 Status status = checkCommonContainerFlow(otherContainer.getContainerFlowSpecs(), proposed);
571 if (!status.isSuccess()) {
572 return new Status(StatusCode.BADREQUEST, String.format(
573 "Container %s which shares ports with this container has overlapping flow spec: %s",
574 entry.getKey(), status.getDescription()));
580 return new Status(StatusCode.SUCCESS);
584 * Checks if the passed list of node connectors can be safely applied to the
585 * specified existing container in terms of port sharing with other containers.
587 * @param containerName
588 * the name of the existing container
590 * the list of node connectors to be added to the container
591 * @return the status object representing the result of the check
593 private Status validatePortSharing(String containerName, List<NodeConnector> portList) {
594 ContainerData container = this.getContainerByName(containerName);
595 if (container == null) {
596 String error = String
597 .format("Cannot validate port sharing for container %s: (container does not exist)", containerName);
599 return new Status(StatusCode.BADREQUEST, error);
601 return validatePortSharingInternal(portList, container.getContainerFlowSpecs());
605 * Checks if the proposed container configuration is valid to be applied in
606 * terms of port sharing with other containers.
608 * @param containerConf
609 * the container configuration object containing the list of node
611 * @return the status object representing the result of the check
613 private Status validatePortSharing(ContainerConfig containerConf) {
614 return validatePortSharingInternal(containerConf.getPortList(), containerConf.getContainerFlowSpecs());
618 * If any port is shared with an existing container, need flowSpec to be
619 * configured. If no flowSpec for this or other container, or if containers have any
620 * overlapping flowspec in common, then let the caller know this
621 * configuration has to be rejected.
623 private Status validatePortSharingInternal(List<NodeConnector> portList, List<ContainerFlow> flowSpecList) {
624 for (NodeConnector port : portList) {
625 List<String> slist = nodeConnectorToContainers.get(port);
626 if (slist != null && !slist.isEmpty()) {
627 for (String otherContainerName : slist) {
629 ContainerData other = containerData.get(otherContainerName);
630 if (flowSpecList.isEmpty()) {
631 msg = String.format("Port %s is shared and flow spec is empty for this container", port);
632 } else if (other.isFlowSpecEmpty()) {
633 msg = String.format("Port %s is shared and flow spec is empty for the other container", port);
634 } else if (!checkCommonContainerFlow(flowSpecList, other.getContainerFlowSpecs()).isSuccess()) {
635 msg = String.format("Port %s is shared and other container has common flow spec", port);
639 return new Status(StatusCode.BADREQUEST, msg);
644 return new Status(StatusCode.SUCCESS);
648 * Utility function to check if two lists of container flows share any same
649 * or overlapping container flows.
652 * One of the two lists of container flows to test
654 * One of the two lists of container flows to test
655 * @return The status of the check. Either SUCCESS or CONFLICT. In case of
656 * conflict, the Status will contain the description for the failed
659 private Status checkCommonContainerFlow(List<ContainerFlow> oneFlowList, List<ContainerFlow> twoFlowList) {
660 for (ContainerFlow oneFlow : oneFlowList) {
661 for (ContainerFlow twoFlow : twoFlowList) {
662 if (oneFlow.getMatch().intersetcs(twoFlow.getMatch())) {
663 return new Status(StatusCode.CONFLICT, String.format("Flow Specs overlap: %s %s",
664 oneFlow.getMatch(), twoFlow.getMatch()));
668 return new Status(StatusCode.SUCCESS);
672 * Return the ContainerData object for the passed container name. Given this is a
673 * backend database, the lower case version of the passed name is used while
674 * searching for the corresponding ContainerData object.
677 * The container name in any case
678 * @return The corresponding ContainerData object
680 private ContainerData getContainerByName(String name) {
681 return containerData.get(name.toLowerCase(Locale.ENGLISH));
685 * Add a ContainerData object for the given container name.
688 * The container name in any case
690 * The container data object
692 private void putContainerDataByName(String name, ContainerData sData) {
693 containerData.put(name.toLowerCase(Locale.ENGLISH), sData);
697 * Removes the ContainerData object for the given container name.
700 * The container name in any case
702 private void removeContainerDataByName(String name) {
703 containerData.remove(name.toLowerCase(Locale.ENGLISH));
707 public List<ContainerConfig> getContainerConfigList() {
708 return new ArrayList<ContainerConfig>(containerConfigs.values());
712 public ContainerConfig getContainerConfig(String containerName) {
713 ContainerConfig target = containerConfigs.get(containerName);
714 return (target == null) ? null : new ContainerConfig(target);
718 public List<String> getContainerNameList() {
720 * Return container names as they were configured by user (case sensitive)
721 * along with the default container
723 List<String> containerNameList = new ArrayList<String>();
724 containerNameList.add(GlobalConstants.DEFAULT.toString());
725 containerNameList.addAll(containerConfigs.keySet());
726 return containerNameList;
730 public Map<String, List<ContainerFlowConfig>> getContainerFlows() {
731 Map<String, List<ContainerFlowConfig>> flowSpecConfig = new HashMap<String, List<ContainerFlowConfig>>();
732 for (Map.Entry<String, ContainerConfig> entry : containerConfigs.entrySet()) {
733 List<ContainerFlowConfig> set = entry.getValue().getContainerFlowConfigs();
734 flowSpecConfig.put(entry.getKey(), set);
736 return flowSpecConfig;
739 private Status saveContainerConfig() {
740 return saveContainerConfigLocal();
743 public Status saveContainerConfigLocal() {
744 Status status = configurationService.persistConfiguration(
745 new ArrayList<ConfigurationObject>(containerConfigs.values()), CONTAINERS_FILE_NAME);
747 if (!status.isSuccess()) {
748 return new Status(StatusCode.INTERNALERROR, "Failed to save container configurations: "
749 + status.getDescription());
754 private void removeComponentsStartUpfiles(String containerName) {
755 String startupLocation = String.format("./%s", GlobalConstants.STARTUPHOME.toString());
756 String containerPrint = String.format("_%s.", containerName.toLowerCase(Locale.ENGLISH));
758 File directory = new File(startupLocation);
759 String[] fileList = directory.list();
761 logger.trace("Deleting startup configuration files for container {}", containerName);
762 if (fileList != null) {
763 for (String fileName : fileList) {
764 if (fileName.contains(containerPrint)) {
765 String fullPath = String.format("%s/%s", startupLocation, fileName);
766 File file = new File(fullPath);
767 boolean done = file.delete();
768 logger.trace("{} {}", (done ? "Deleted: " : "Failed to delete: "), fileName);
775 * Create and initialize default all resource group and create association
776 * with default well known users and profiles, if not already learnt from
777 * another cluster node
779 private void createDefaultAuthorizationGroups() {
780 allResourcesGroupName = ContainerManager.allContainersGroup;
782 // Add the default container to the all containers group if needed
783 String defaultContainer = GlobalConstants.DEFAULT.toString();
784 Set<String> allContainers = (resourceGroups.containsKey(allResourcesGroupName)) ? resourceGroups
785 .get(allResourcesGroupName) : new HashSet<String>();
786 if (!allContainers.contains(defaultContainer)) {
787 // Add Default container
788 allContainers.add(defaultContainer);
790 resourceGroups.put(allResourcesGroupName, allContainers);
793 // Add the controller well known roles, if not known already
794 if (!roles.containsKey(UserLevel.SYSTEMADMIN.toString())) {
795 roles.put(UserLevel.SYSTEMADMIN.toString(), AppRoleLevel.APPADMIN);
797 if (!roles.containsKey(UserLevel.NETWORKADMIN.toString())) {
798 roles.put(UserLevel.NETWORKADMIN.toString(), AppRoleLevel.APPADMIN);
800 if (!roles.containsKey(UserLevel.NETWORKOPERATOR.toString())) {
801 roles.put(UserLevel.NETWORKOPERATOR.toString(), AppRoleLevel.APPOPERATOR);
805 * Create and add the all containers user groups and associate them to the
806 * default well known user roles, if not present already
808 if (!groupsAuthorizations.containsKey(UserLevel.NETWORKADMIN.toString())) {
809 Set<ResourceGroup> writeProfile = new HashSet<ResourceGroup>(1);
810 Set<ResourceGroup> readProfile = new HashSet<ResourceGroup>(1);
811 writeProfile.add(new ResourceGroup(allResourcesGroupName, Privilege.WRITE));
812 readProfile.add(new ResourceGroup(allResourcesGroupName, Privilege.READ));
813 groupsAuthorizations.put(UserLevel.SYSTEMADMIN.toString(), writeProfile);
814 groupsAuthorizations.put(UserLevel.NETWORKADMIN.toString(), writeProfile);
815 groupsAuthorizations.put(UserLevel.NETWORKOPERATOR.toString(), readProfile);
820 * Until manual configuration is not available, automatically maintain the
821 * well known resource groups
823 * @param containerName
826 private void updateResourceGroups(ContainerConfig containerConf, boolean delete) {
827 // Container Roles and Container Resource Group
828 String containerName = containerConf.getContainer();
829 String groupName = containerConf.getContainerGroupName();
830 String containerAdminRole = containerConf.getContainerAdminRole();
831 String containerOperatorRole = containerConf.getContainerOperatorRole();
832 Set<String> allContainerSet = resourceGroups.get(allResourcesGroupName);
834 resourceGroups.remove(groupName);
835 groupsAuthorizations.remove(containerAdminRole);
836 groupsAuthorizations.remove(containerOperatorRole);
837 roles.remove(containerAdminRole);
838 roles.remove(containerOperatorRole);
839 // Update the all container group
840 allContainerSet.remove(containerName);
842 Set<String> resources = new HashSet<String>(1);
843 resources.add(containerName);
844 resourceGroups.put(groupName, resources);
845 Set<ResourceGroup> adminGroups = new HashSet<ResourceGroup>(1);
846 Set<ResourceGroup> operatorGroups = new HashSet<ResourceGroup>(1);
847 adminGroups.add(new ResourceGroup(groupName, Privilege.WRITE));
848 operatorGroups.add(new ResourceGroup(groupName, Privilege.READ));
849 groupsAuthorizations.put(containerAdminRole, adminGroups);
850 groupsAuthorizations.put(containerOperatorRole, operatorGroups);
851 roles.put(containerAdminRole, AppRoleLevel.APPADMIN);
852 roles.put(containerOperatorRole, AppRoleLevel.APPOPERATOR);
853 // Update the all containers resource group
854 allContainerSet.add(containerName);
856 // Update resource groups in cluster
857 resourceGroups.put(allResourcesGroupName, allContainerSet);
861 * Notify ContainerAware listeners of the creation/deletion of the container
863 * @param containerName
865 * true is container was removed, false otherwise
867 private void notifyContainerAwareListeners(String containerName, boolean delete) {
868 // Back-end World: container name forced to lower case
869 String name = containerName.toLowerCase(Locale.ENGLISH);
871 synchronized (this.iContainerAware) {
872 for (IContainerAware i : this.iContainerAware) {
874 i.containerDestroy(name);
876 i.containerCreate(name);
883 * Notify the ContainerListener listeners in case the container mode has
884 * changed following a container configuration operation Note: this call
885 * must happen after the configuration db has been updated
887 * @param lastActionDelete
888 * true if the last container configuration operation was a
889 * container delete operation
891 * if true, the notification is also sent to the
892 * IContainerLocalListener classes besides the IContainerListener
895 private void notifyContainerModeChange(boolean lastActionDelete, boolean notifyLocal) {
896 if (lastActionDelete == false && containerConfigs.size() == 1) {
897 logger.trace("First container Creation. Inform listeners");
898 synchronized (this.iContainerListener) {
899 for (IContainerListener i : this.iContainerListener) {
900 i.containerModeUpdated(UpdateType.ADDED);
904 synchronized (this.iContainerLocalListener) {
905 for (IContainerLocalListener i : this.iContainerLocalListener) {
906 i.containerModeUpdated(UpdateType.ADDED);
910 } else if (lastActionDelete == true && containerConfigs.isEmpty()) {
911 logger.trace("Last container Deletion. Inform listeners");
912 synchronized (this.iContainerListener) {
913 for (IContainerListener i : this.iContainerListener) {
914 i.containerModeUpdated(UpdateType.REMOVED);
918 synchronized (this.iContainerLocalListener) {
919 for (IContainerLocalListener i : this.iContainerLocalListener) {
920 i.containerModeUpdated(UpdateType.REMOVED);
927 private Status addRemoveContainerEntries(String containerName, List<String> nodeConnectorsString, boolean delete) {
928 // Construct action message
929 String action = String.format("Node connector(s) %s container %s: %s", delete ? "removal from" : "addition to",
930 containerName, nodeConnectorsString);
933 if (nodeConnectorsString == null || nodeConnectorsString.isEmpty()) {
934 return new Status(StatusCode.BADREQUEST, "Node connector list is null or empty");
938 ContainerConfig entryConf = containerConfigs.get(containerName);
939 if (entryConf == null) {
940 String msg = String.format("Container not found: %s", containerName);
941 String error = String.format("Failed to apply %s: (%s)", action, msg);
943 return new Status(StatusCode.NOTFOUND, msg);
947 Status status = ContainerConfig.validateNodeConnectors(nodeConnectorsString);
948 if (!status.isSuccess()) {
949 String error = String.format("Failed to apply %s: (%s)", action, status.getDescription());
954 List<NodeConnector> nodeConnectors = ContainerConfig.nodeConnectorsFromString(nodeConnectorsString);
956 // Port sharing check
959 * Check if the ports being added to this container already belong to
960 * other containers. If so check whether the the appropriate flow specs
961 * are configured on this container
963 status = validatePortSharing(containerName, nodeConnectors);
964 if (!status.isSuccess()) {
965 String error = String.format("Failed to apply %s: (%s)", action, status.getDescription());
972 status = updateContainerEntryDatabase(containerName, nodeConnectors, delete);
973 if (!status.isSuccess()) {
974 String error = String.format("Failed to apply %s: (%s)", action, status.getDescription());
979 // Update Configuration
980 status = (delete) ? entryConf.removeNodeConnectors(nodeConnectorsString) : entryConf
981 .addNodeConnectors(nodeConnectorsString);
982 if (!status.isSuccess()) {
983 String error = String.format("Failed to modify config for %s: (%s)", action, status.getDescription());
985 // Revert backend changes
986 Status statusRevert = updateContainerEntryDatabase(containerName, nodeConnectors, !delete);
987 if (!statusRevert.isSuccess()) {
989 logger.error("Failed to revert changes in database (CRITICAL)");
994 // Update cluster Configuration cache
995 containerConfigs.put(containerName, entryConf);
997 // Notify global and local listeners
998 UpdateType update = (delete) ? UpdateType.REMOVED : UpdateType.ADDED;
999 notifyContainerEntryChangeInternal(containerName, nodeConnectors, update, true);
1000 // Trigger cluster notification
1001 containerChangeEvents.put(containerName, new NodeConnectorsChangeEvent(nodeConnectors, update));
1006 private void notifyContainerChangeInternal(ContainerConfig conf, UpdateType update, boolean notifyLocal) {
1007 String containerName = conf.getContainerName();
1008 logger.trace("Notifying listeners on {} for container {}", update, containerName);
1009 // Back-end World: container name forced to lower case
1010 String container = containerName.toLowerCase(Locale.ENGLISH);
1011 boolean delete = (update == UpdateType.REMOVED);
1012 // Check if a container mode change notification is needed
1013 notifyContainerModeChange(delete, notifyLocal);
1015 notifyContainerAwareListeners(container, delete);
1018 * This is a quick fix until configuration service becomes the
1019 * centralized configuration management place. Here container manager
1020 * will remove the startup files for all the bundles that are present in
1021 * the container being deleted. Do the cleanup here in Container manger
1022 * as do not want to put this temporary code in Configuration manager
1026 // TODO: remove when Config Mgr takes over
1027 removeComponentsStartUpfiles(containerName);
1031 private void notifyContainerEntryChangeInternal(String containerName, List<NodeConnector> ncList, UpdateType update, boolean notifyLocal) {
1032 logger.trace("Notifying listeners on {} for ports {} in container {}", update, ncList, containerName);
1033 // Back-end World: container name forced to lower case
1034 String container = containerName.toLowerCase(Locale.ENGLISH);
1035 for (NodeConnector nodeConnector : ncList) {
1036 // Now signal that the port has been added/removed
1037 synchronized (this.iContainerListener) {
1038 for (IContainerListener i : this.iContainerListener) {
1039 i.nodeConnectorUpdated(container, nodeConnector, update);
1042 // Check if the Functional Modules need to be notified as well
1044 synchronized (this.iContainerLocalListener) {
1045 for (IContainerLocalListener i : this.iContainerLocalListener) {
1046 i.nodeConnectorUpdated(container, nodeConnector, update);
1053 private void notifyCFlowChangeInternal(String containerName, List<ContainerFlowConfig> confList, UpdateType update,
1054 boolean notifyLocal) {
1055 logger.trace("Notifying listeners on {} for flow specs {} in container {}", update, confList, containerName);
1056 // Back-end World: container name forced to lower case
1057 String container = containerName.toLowerCase(Locale.ENGLISH);
1059 for (ContainerFlowConfig conf : confList) {
1060 for (Match match : conf.getMatches()) {
1061 ContainerFlow cFlow = new ContainerFlow(match);
1062 synchronized (this.iContainerListener) {
1063 for (IContainerListener i : this.iContainerListener) {
1064 i.containerFlowUpdated(container, cFlow, cFlow, update);
1067 // Check if the Functional Modules need to be notified as well
1069 synchronized (this.iContainerLocalListener) {
1070 for (IContainerLocalListener i : this.iContainerLocalListener) {
1071 i.containerFlowUpdated(container, cFlow, cFlow, update);
1079 private Status addRemoveContainerFlow(String containerName, List<ContainerFlowConfig> cFlowConfList, boolean delete) {
1080 // Construct action message
1081 String action = String.format("Flow spec(s) %s container %s: %s", delete ? "removal from" : "addition to",
1082 containerName, cFlowConfList);
1085 ContainerConfig containerConfig = this.containerConfigs.get(containerName);
1086 if (containerConfig == null) {
1087 String msg = String.format("Container not found: %s", containerName);
1088 String error = String.format("Failed to apply %s: (%s)", action, msg);
1090 return new Status(StatusCode.NOTFOUND, "Container not present");
1093 // Validity check, check for overlaps on current container configuration
1094 Status status = containerConfig.validateContainerFlowModify(cFlowConfList, delete);
1095 if (!status.isSuccess()) {
1096 String msg = status.getDescription();
1097 String error = String.format("Failed to apply %s: (%s)", action, msg);
1099 return new Status(StatusCode.BADREQUEST, msg);
1102 // Validate the operation in terms to the port sharing with other containers
1103 for (ContainerFlowConfig conf : cFlowConfList) {
1104 for (Match match : conf.getMatches()) {
1105 ContainerFlow cFlow = new ContainerFlow(match);
1106 status = validateContainerFlowAddRemoval(containerName, cFlow, delete);
1107 if (!status.isSuccess()) {
1108 String msg = "Validation failed: " + status.getDescription();
1109 String error = String.format("Failed to apply %s: (%s)", action, msg);
1111 return new Status(StatusCode.BADREQUEST, msg);
1117 status = updateContainerFlow(containerName, cFlowConfList, delete);
1118 if (!status.isSuccess()) {
1119 String error = String.format("Failed to apply %s: (%s)", action, status.getDescription());
1120 logger.error(error);
1124 // Update Configuration
1125 status = (delete) ? containerConfig.removeContainerFlows(cFlowConfList) : containerConfig
1126 .addContainerFlows(cFlowConfList);
1127 if (!status.isSuccess()) {
1128 String error = String.format("Failed to modify config for %s: (%s)", action, status.getDescription());
1129 logger.error(error);
1130 // Revert backend changes
1131 Status statusRevert = updateContainerFlow(containerName, cFlowConfList, !delete);
1132 if (!statusRevert.isSuccess()) {
1134 logger.error("Failed to revert changes in database (CRITICAL)");
1138 // Update cluster cache
1139 this.containerConfigs.put(containerName, containerConfig);
1141 // Notify global and local listeners
1142 UpdateType update = (delete) ? UpdateType.REMOVED : UpdateType.ADDED;
1143 notifyCFlowChangeInternal(containerName, cFlowConfList, update, true);
1144 // Trigger cluster notification
1145 containerChangeEvents.put(containerName, new ContainerFlowChangeEvent(cFlowConfList, update));
1150 private Status addRemoveContainer(ContainerConfig containerConf, boolean delete) {
1151 // Construct action message
1152 String action = String.format("Container %s", delete ? "removal" : "creation");
1154 // Valid configuration check
1155 Status status = null;
1156 String error = (containerConfigs == null) ? String.format("Invalid %s configuration: (null config object)", action)
1157 : (!(status = containerConf.validate()).isSuccess()) ? String.format("Invalid %s configuration: (%s)",
1158 action, status.getDescription()) : null;
1159 if (error != null) {
1161 return new Status(StatusCode.BADREQUEST, error);
1164 // Configuration presence check
1165 String containerName = containerConf.getContainerName();
1167 if (!containerConfigs.containsKey(containerName)) {
1168 String msg = String.format("%s Failed: (Container does not exist: %s)", action, containerName);
1170 return new Status(StatusCode.NOTFOUND, msg);
1173 if (containerConfigs.containsKey(containerName)) {
1174 String msg = String.format("%s Failed: (Container already exist: %s)", action, containerName);
1176 return new Status(StatusCode.CONFLICT, msg);
1181 * The proposed container configuration could be a complex one containing
1182 * both ports and flow spec. If so, check if it has shared ports with
1183 * other existing containers. If that is the case verify flow spec isolation
1184 * is in place. No need to check on flow spec validation first. This
1185 * would take care of both
1188 status = validatePortSharing(containerConf);
1189 if (!status.isSuccess()) {
1190 error = String.format("%s Failed: (%s)", action, status.getDescription());
1191 logger.error(error);
1197 status = updateContainerDatabase(containerConf, delete);
1199 // Abort and exit here if back-end database update failed
1200 if (!status.isSuccess()) {
1205 * Update Configuration: This will trigger the notifications on cache
1206 * update callback locally and on the other cluster nodes
1209 this.containerConfigs.remove(containerName);
1211 this.containerConfigs.put(containerName, containerConf);
1214 // Automatically create and populate user and resource groups
1215 updateResourceGroups(containerConf, delete);
1217 // Notify global and local listeners
1218 UpdateType update = (delete) ? UpdateType.REMOVED : UpdateType.ADDED;
1219 notifyContainerChangeInternal(containerConf, update, true);
1221 // Trigger cluster notification
1222 containerChangeEvents.put(containerName, new ContainerChangeEvent(containerConf, update));
1224 if (update == UpdateType.ADDED) {
1225 if (containerConf.hasFlowSpecs()) {
1226 List<ContainerFlowConfig> specList = containerConf.getContainerFlowConfigs();
1227 // Notify global and local listeners about flow spec addition
1228 notifyCFlowChangeInternal(containerName, specList, update, true);
1230 // Trigger cluster notification
1231 containerChangeEvents.put(containerName, new ContainerFlowChangeEvent(specList, update));
1234 if (containerConf.hasNodeConnectors()) {
1235 List<NodeConnector> ncList = containerConf.getPortList();
1236 // Notify global and local listeners about port(s) addition
1237 notifyContainerEntryChangeInternal(containerName, ncList, update, true);
1238 // Trigger cluster notification
1239 containerChangeEvents.put(containerName, new NodeConnectorsChangeEvent(ncList, update));
1244 clusterServices.removeContainerCaches(containerName);
1250 public Status addContainer(ContainerConfig containerConf) {
1251 return addRemoveContainer(containerConf, false);
1255 public Status removeContainer(ContainerConfig containerConf) {
1256 return addRemoveContainer(containerConf, true);
1260 public Status removeContainer(String containerName) {
1261 // Construct action message
1262 String action = String.format("Container removal: %s", containerName);
1264 ContainerConfig containerConf = containerConfigs.get(containerName);
1265 if (containerConf == null) {
1266 String msg = String.format("Container not found");
1267 String error = String.format("Failed to apply %s: (%s)", action, msg);
1269 return new Status(StatusCode.NOTFOUND, msg);
1271 return addRemoveContainer(containerConf, true);
1275 public Status addContainerEntry(String containerName, List<String> nodeConnectors) {
1276 return addRemoveContainerEntries(containerName, nodeConnectors, false);
1280 public Status removeContainerEntry(String containerName, List<String> nodeConnectors) {
1281 return addRemoveContainerEntries(containerName, nodeConnectors, true);
1285 public Status addContainerFlows(String containerName, List<ContainerFlowConfig> fSpecConf) {
1286 return addRemoveContainerFlow(containerName, fSpecConf, false);
1290 public Status removeContainerFlows(String containerName, List<ContainerFlowConfig> fSpecConf) {
1291 return addRemoveContainerFlow(containerName, fSpecConf, true);
1295 public Status removeContainerFlows(String containerName, Set<String> names) {
1296 // Construct action message
1297 String action = String.format("Flow spec(s) removal from container %s: %s", containerName, names);
1300 ContainerConfig sc = containerConfigs.get(containerName);
1302 String msg = String.format("Container not found: %s", containerName);
1303 String error = String.format("Failed to apply %s: (%s)", action, msg);
1305 return new Status(StatusCode.NOTFOUND, msg);
1307 List<ContainerFlowConfig> list = sc.getContainerFlowConfigs(names);
1308 if (list.isEmpty() || list.size() != names.size()) {
1309 String msg = String.format("Cannot find all the specified flow specs");
1310 String error = String.format("Failed to apply %s: (%s)", action, msg);
1312 return new Status(StatusCode.BADREQUEST, msg);
1314 return addRemoveContainerFlow(containerName, list, true);
1318 public List<ContainerFlowConfig> getContainerFlows(String containerName) {
1319 ContainerConfig sc = containerConfigs.get(containerName);
1320 return (sc == null) ? new ArrayList<ContainerFlowConfig>(0) : sc.getContainerFlowConfigs();
1324 public List<String> getContainerFlowNameList(String containerName) {
1325 ContainerConfig sc = containerConfigs.get(containerName);
1326 return (sc == null) ? new ArrayList<String>(0) : sc.getContainerFlowConfigsNames();
1330 public Object readObject(ObjectInputStream ois) throws FileNotFoundException, IOException, ClassNotFoundException {
1331 // Perform the class deserialization locally, from inside the package
1332 // where the class is defined
1333 return ois.readObject();
1336 private void loadContainerConfig() {
1337 for (ConfigurationObject conf : configurationService.retrieveConfiguration(this, CONTAINERS_FILE_NAME)) {
1338 addContainer((ContainerConfig) conf);
1342 public void _psc(CommandInterpreter ci) {
1343 for (Map.Entry<String, ContainerConfig> entry : containerConfigs.entrySet()) {
1344 ContainerConfig sc = entry.getValue();
1345 ci.println(String.format("%s: %s", sc.getContainerName(), sc.toString()));
1347 ci.println("Total number of containers: " + containerConfigs.entrySet().size());
1350 public void _pfc(CommandInterpreter ci) {
1351 for (Map.Entry<String, ContainerConfig> entry : containerConfigs.entrySet()) {
1352 ContainerConfig sc = entry.getValue();
1353 ci.println(String.format("%s: %s", sc.getContainerName(), sc.getContainerFlowConfigs()));
1357 public void _psd(CommandInterpreter ci) {
1358 for (String containerName : containerData.keySet()) {
1359 ContainerData sd = containerData.get(containerName);
1360 for (Node sid : sd.getSwPorts().keySet()) {
1361 Set<NodeConnector> s = sd.getSwPorts().get(sid);
1362 ci.println("\t" + sid + " : " + s);
1365 for (ContainerFlow s : sd.getContainerFlowSpecs()) {
1366 ci.println("\t" + s.toString());
1371 public void _psp(CommandInterpreter ci) {
1372 for (NodeConnector sp : nodeConnectorToContainers.keySet()) {
1373 ci.println(nodeConnectorToContainers.get(sp));
1377 public void _psm(CommandInterpreter ci) {
1378 for (Node sp : nodeToContainers.keySet()) {
1379 ci.println(nodeToContainers.get(sp));
1383 public void _addContainer(CommandInterpreter ci) {
1384 String containerName = ci.nextArgument();
1385 if (containerName == null) {
1386 ci.print("Container Name not specified");
1389 String staticVlan = ci.nextArgument();
1390 ContainerConfig containerConfig = new ContainerConfig(containerName, staticVlan, null, null);
1391 ci.println(this.addRemoveContainer(containerConfig, false));
1394 public void _createContainer(CommandInterpreter ci) {
1395 String containerName = ci.nextArgument();
1396 if (containerName == null) {
1397 ci.print("Container Name not specified");
1400 String staticVlan = ci.nextArgument();
1401 if (staticVlan == null) {
1402 ci.print("Static Vlan not specified");
1405 List<String> ports = new ArrayList<String>();
1406 for (long l = 1L; l < 10L; l++) {
1407 ports.add(NodeConnectorCreator.createOFNodeConnector((short) 1, NodeCreator.createOFNode(l)).toString());
1409 List<ContainerFlowConfig> cFlowList = new ArrayList<ContainerFlowConfig>();
1410 cFlowList.add(this.createSampleContainerFlowConfig("tcp", true));
1411 ContainerConfig containerConfig = new ContainerConfig(containerName, staticVlan, ports, cFlowList);
1412 ci.println(this.addRemoveContainer(containerConfig, false));
1415 public void _removeContainer(CommandInterpreter ci) {
1416 String containerName = ci.nextArgument();
1417 if (containerName == null) {
1418 ci.print("Container Name not specified");
1421 ContainerConfig containerConfig = new ContainerConfig(containerName, "", null, null);
1422 ci.println(this.addRemoveContainer(containerConfig, true));
1425 public void _addContainerEntry(CommandInterpreter ci) {
1426 String containerName = ci.nextArgument();
1427 if (containerName == null) {
1428 ci.print("Container Name not specified");
1431 String nodeId = ci.nextArgument();
1432 if (nodeId == null) {
1433 ci.print("Node Id not specified");
1436 String portId = ci.nextArgument();
1437 if (portId == null) {
1438 ci.print("Port not specified");
1441 Node node = NodeCreator.createOFNode(Long.valueOf(nodeId));
1442 Short port = Short.valueOf(portId);
1443 NodeConnector nc = NodeConnectorCreator.createOFNodeConnector(port, node);
1444 List<String> portList = new ArrayList<String>(1);
1445 portList.add(nc.toString());
1446 ci.println(this.addRemoveContainerEntries(containerName, portList, false));
1449 public void _removeContainerEntry(CommandInterpreter ci) {
1450 String containerName = ci.nextArgument();
1451 if (containerName == null) {
1452 ci.print("Container Name not specified");
1455 String nodeId = ci.nextArgument();
1456 if (nodeId == null) {
1457 ci.print("Node Id not specified");
1460 String portId = ci.nextArgument();
1461 if (portId == null) {
1462 ci.print("Port not specified");
1465 Node node = NodeCreator.createOFNode(Long.valueOf(nodeId));
1466 Short port = Short.valueOf(portId);
1467 NodeConnector nc = NodeConnectorCreator.createOFNodeConnector(port, node);
1468 List<String> portList = new ArrayList<String>(1);
1469 portList.add(nc.toString());
1470 ci.println(this.addRemoveContainerEntries(containerName, portList, true));
1473 private ContainerFlowConfig createSampleContainerFlowConfig(String cflowName, boolean boolUnidirectional) {
1474 ContainerFlowConfig cfg = new ContainerFlowConfig(cflowName, "9.9.1.0/24", "19.9.1.2", "TCP", "1234", "25");
1478 public void _addContainerFlow(CommandInterpreter ci) {
1479 String containerName = ci.nextArgument();
1480 if (containerName == null) {
1481 ci.print("Container Name not specified");
1484 String cflowName = ci.nextArgument();
1485 if (cflowName == null) {
1486 ci.print("cflowName not specified");
1489 String unidirectional = ci.nextArgument();
1490 boolean boolUnidirectional = Boolean.parseBoolean(unidirectional);
1491 List<ContainerFlowConfig> list = new ArrayList<ContainerFlowConfig>();
1492 list.add(createSampleContainerFlowConfig(cflowName, boolUnidirectional));
1493 ci.println(this.addRemoveContainerFlow(containerName, list, false));
1496 public void _removeContainerFlow(CommandInterpreter ci) {
1497 String containerName = ci.nextArgument();
1498 if (containerName == null) {
1499 ci.print("Container Name not specified");
1502 String cflowName = ci.nextArgument();
1503 if (cflowName == null) {
1504 ci.print("cflowName not specified");
1507 Set<String> set = new HashSet<String>(1);
1509 ci.println(this.removeContainerFlows(containerName, set));
1513 public String getHelp() {
1514 StringBuffer help = new StringBuffer();
1515 help.append("---ContainerManager Testing---\n");
1516 help.append("\tpsc - Print ContainerConfigs\n");
1517 help.append("\tpfc - Print FlowSpecConfigs\n");
1518 help.append("\tpsd - Print ContainerData\n");
1519 help.append("\tpsp - Print nodeConnectorToContainers\n");
1520 help.append("\tpsm - Print nodeToContainers\n");
1521 help.append("\t addContainer <containerName> <staticVlan> \n");
1522 help.append("\t removeContainer <containerName> \n");
1523 help.append("\t addContainerEntry <containerName> <nodeId> <port> \n");
1524 help.append("\t removeContainerEntry <containerName> <nodeId> <port> \n");
1525 help.append("\t addContainerFlow <containerName> <cflowName> <unidirectional true/false>\n");
1526 help.append("\t removeContainerFlow <containerName> <cflowName> \n");
1527 return help.toString();
1531 public boolean doesContainerExist(String containerName) {
1532 // Test for default container
1533 if (GlobalConstants.DEFAULT.toString().equalsIgnoreCase(containerName)) {
1536 // Test for non-default one
1537 return (getContainerByName(containerName) != null);
1541 public ContainerData getContainerData(String containerName) {
1542 return (getContainerByName(containerName));
1546 public Status saveConfiguration() {
1547 return saveContainerConfig();
1550 public void _containermgrGetRoles(CommandInterpreter ci) {
1551 ci.println("Configured roles for Container Mgr:");
1552 List<String> list = this.getRoles();
1553 for (String role : list) {
1554 ci.println(role + "\t" + roles.get(role));
1558 public void _containermgrGetAuthorizedGroups(CommandInterpreter ci) {
1559 String roleName = ci.nextArgument();
1560 if (roleName == null || roleName.trim().isEmpty()) {
1561 ci.println("Invalid argument");
1562 ci.println("mmGetAuthorizedGroups <role_name>");
1565 ci.println("Resource Groups associated to role " + roleName + ":");
1566 List<ResourceGroup> list = this.getAuthorizedGroups(roleName);
1567 for (ResourceGroup group : list) {
1568 ci.println(group.toString());
1572 public void _containermgrGetAuthorizedResources(CommandInterpreter ci) {
1573 String roleName = ci.nextArgument();
1574 if (roleName == null || roleName.trim().isEmpty()) {
1575 ci.println("Invalid argument");
1576 ci.println("mmGetAuthorizedResources <role_name>");
1579 ci.println("Resource associated to role " + roleName + ":");
1580 List<Resource> list = this.getAuthorizedResources(roleName);
1581 for (Resource resource : list) {
1582 ci.println(resource.toString());
1586 public void _containermgrGetResourcesForGroup(CommandInterpreter ci) {
1587 String groupName = ci.nextArgument();
1588 if (groupName == null || groupName.trim().isEmpty()) {
1589 ci.println("Invalid argument");
1590 ci.println("containermgrResourcesForGroup <group_name>");
1593 ci.println("Group " + groupName + " contains the following resources:");
1594 List<Object> resources = this.getResources(groupName);
1595 for (Object resource : resources) {
1596 ci.println(resource.toString());
1600 public void _containermgrGetUserLevel(CommandInterpreter ci) {
1601 String userName = ci.nextArgument();
1602 if (userName == null || userName.trim().isEmpty()) {
1603 ci.println("Invalid argument");
1604 ci.println("containermgrGetUserLevel <user_name>");
1607 ci.println("User " + userName + " has level: " + this.getUserLevel(userName));
1610 public void _containermgrGetUserResources(CommandInterpreter ci) {
1611 String userName = ci.nextArgument();
1612 if (userName == null || userName.trim().isEmpty()) {
1613 ci.println("Invalid argument");
1614 ci.println("containermgrGetUserResources <user_name>");
1617 ci.println("User " + userName + " owns the following resources: ");
1618 Set<Resource> resources = this.getAllResourcesforUser(userName);
1619 for (Resource resource : resources) {
1620 ci.println(resource.toString());
1625 * For scalability testing where as of now controller gui is unresponsive
1626 * providing here an osgi hook to trigger the save config so that DT do not
1627 * have to reaply the scalable configuration each time they restart the
1630 // TODO: remove when no longer needed
1631 public void _saveConfig(CommandInterpreter ci) {
1632 Status status = new Status(StatusCode.NOSERVICE, "Configuration service not reachable");
1634 IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
1635 IConfigurationService.class, this);
1636 if (configService != null) {
1637 status = configService.saveConfigurations();
1639 ci.println(status.toString());
1643 public List<String> getContainerNames() {
1644 return getContainerNameList();
1648 public boolean hasNonDefaultContainer() {
1649 return !containerConfigs.keySet().isEmpty();
1653 public boolean inContainerMode() {
1654 return this.containerConfigs.size() > 0;