You can make an opportunity Attack when a Hostile creature that you can see moves out of your reach.
Thats the only way you can take an opportunitty attack. The rule also let you use teleportation spells like Misty Step or Far Step to leave reach of an engaged foe without provoking it.
However, there's some features that can work similarly to opportunity attacks against casters. The Mage Slayer feat from PHB is one exemple of those, that says:
When a creature within 5 feet of you casts a spell, you can use your reaction to make a melee weapon attack against that creature.
Does casting a spell provoke an opportunity attack?
Not by default no.
The opportunity attack rule say:
You can make an opportunity Attack when a Hostile creature that you can see moves out of your reach.
Thats the only way you can take an opportunitty attack. The rule also let you use teleportation spells like Misty Step or Far Step to leave reach of an engaged foe without provoking it.
However, there's some features that can work similarly to opportunity attacks against casters. The Mage Slayer feat from PHB is one exemple of those, that says:
Thanks for the help. I am used to playing the pathfinder rules on attacks of opportunity so just wanted to make sure.