If your optical drive keeps opening and closing by itself you should try one of this to fix it:
1. Use a different power connector for your drive.
2. Make sure eject button isn't stuck or pushed in.
3. Reinstall the operating system. It has an eject option in My computer and that could be acting up.
4. Try to upgrade the firmware version of your drive.
If this fails try to take it to a computer service to fix it.